Replies: 1 comment 5 replies
-
Yes, it's not possible to directly catch exceptions in the trigger expression. I don't think having a default that applies to all exceptions is a good idea - there could be exceptions that you do want reported as errors. One solution is to put any logic you want, including a A simpler approach is to just make your expression more robust to the various values the entity can take. If @state_trigger("sensor.nas1_temperature == 'unavailable' or float(sensor.nas1_temperature) >= 60") if you do want to trigger when it's @state_trigger("sensor.nas1_temperature != 'unavailable' and float(sensor.nas1_temperature) >= 60") |
Beta Was this translation helpful? Give feedback.
-
I have a sensor that normally returns a value, but once in an while is ´unavailable´. Therefore I get Exceptions errors in my log like the one here:
Exception in <file.nas1.NAS_drive_temperature @state_trigger()> line 1: float(sensor.nas1_drive_1_temperature) >= 60 or float(sensor.nas1_drive_2_temperature) >= 60 ^ ValueError: could not convert string to float: 'unavailable'
The goal is to watch for over temperature, so nothing breaks, but it ends up in my log file.
In HA the template float and int conversion do have a default value, that would be nice to have in pyscript, but this might be difficult to implement.
Instead, the current state_trigger function can catch any ValueError exception, and the new default value will be taken. The new code could look like this:
@state_trigger("float(sensor.nas1_temperature) >= 60", default=False)
In this case any failed conversion will result in False, and thus no triggering.
In case you define default = True, the state_trigger is triggered and you have to deal with this in your code.
Is this doable? Maybe we should also get access to what exception was thrown?
Beta Was this translation helpful? Give feedback.
All reactions