Good morning from someone who’s bedroom lights turned on last night at 3:50 o’clock. But let me explain:
I have various Shelly switches throughout the house and I have learned that for a switch, there are generally 3 states to be considered in automations:
on
off
unavailable
‘Unavailable’ will happen from time to time due to wifi stability. It’s short but it will still trigger something if I do my automations like that:
The numbers are for temperature, and the "trigger.from_state.state != "unavailable" with "!=" means that it should not trigger if state.from.state is unavailable.
unavailable and unknown are legitimate state values. They’re not what one would call nominal states like on or off because they report problems encountered while attempting to communicate with the device.
If you don’t want to to trigger on state-changes to/from non-nominal states, simply constrain the State Trigger to listen for nominal states. In other words, exactly in the way shown in your example:
platform: state
entity_id: binary_sensor.bedroom_shelly_switch
from: "off"
to: "on"
You can also use not_from or not_to (for situations where you want to be very specific):
platform: state
entity_id: binary_sensor.bedroom_shelly_switch
not_from: "unavailable"
I wouldn’t use the Template Condition example you posted (employing the trigger variable to check the binary_sensor’s previous and current states) simply because it’s a long-winded way of achieving what a State Trigger’s to and from options do very concisely.
Interesting - I didn’t know “not_from” and “not_to” so far. My most often wanted usecase is to capture when the switch is being toggled and so I would need 2 triggers (which I want to avoid):
# trigger1
platform: state
entity_id: binary_sensor.bedroom_shelly_switch
from: "off"
to: "on"
# trigger2
platform: state
entity_id: binary_sensor.bedroom_shelly_switch
from: "on"
to: "off"
But given the not-condition I could probably do this:
platform: state
entity_id: binary_sensor.bedroom_shelly_switch
not_from: "unavailable"
not_to: "unavailable"
…in only one trigger which would be the cleanest way (if it works).
I’ve used various variables in templates before but it never occurred to m e that the trace provides a convenient list of available variables. Will use that!
Damn it - you can put a list there?? Awesome. That will come in handy at other places! Thx!
Now if only the graphical editor would support it (but it doesn’t support “not_from” and “not_to” as well, so that doesn’t matter)
Refer to the second example (showing the use of a list) in the documentation for State Trigger.
For future reference, the Automation Editor, in visual mode, is meant for novices. It’s been improved a great deal but it doesn’t offer access to all features available in Home Assistant’s scripting language. I imagine what is/isn’t shown in visual mode may be driven by a desire to avoid overwhelming the user with too many choices and to display only the most common features (i.e. don’t display advanced scripting features).