A checkbox is required in the automation UI to prevent unexpected triggers when state changes from or to “unknown” or “unavailable”.
Example: Toggle light when switch state changes
Using the great UI automation editor you would usually add a Trigger “State” and select the entity “Bathroom light switch” and the Action “Call Service” and select “Light Toggle” with the entity “Bathroom lamp”.
This trigger only works as expected when the state changes from “on” to “off” or “off” to “on”.
But you wouldn’t usually expect that the lamp will also switch on a restart of homeassistant or when the “Bathroom light switch” changes from “unknown” or “unavailable” to “off”.
My suggestion is to add a checkbox that will prevent those unexpected triggers.
This checkbox could be default for new automations and experts can uncheck to trigger on “unknown” or “unavailable” states.
Here is what the UI automation editor will create as yaml:
alias: Toggle light when switch state changes
description: Unexpected behavior because automation will also trigger on state changes from or to "unknown" or "unavailable"
mode: single
trigger:
- platform: state
entity_id:
- binary_sensor.bathroom_light_switch
action:
- service: light.toggle
data: {}
target:
entity_id: light.bathroom
mode: single
If you want the expected behavior to trigger only on changes between “on” and “off” you need a workaround.
As a workaround in the UI you can make sure that only on/off state changes trigger by adding two triggers:
alias: Toggle light when switch state changes
description: Unexpected behavior because automation will also trigger on state changes from or to "unknown" or "unavailable"
mode: single
trigger:
- platform: state
entity_id:
- binary_sensor.bathroom_light_switch
from: "off"
to: "on"
- platform: state
entity_id:
- binary_sensor.bathroom_light_switch
from: "on"
to: "off"
action:
- service: light.toggle
data: {}
target:
entity_id: light.bathroom
mode: single
Another workaround is a condition to check for unwanted changes:
alias: Toggle light when switch state changes
trigger:
- platform: state
entity_id:
- binary_sensor.bathroom_light_switch
condition:
- "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}"
- "{{ trigger.to_state.state not in ['unknown', 'unavailable'] }}"
- "{{ trigger.from_state.state != trigger.to_state.state}}"
action:
- service: light.toggle
data: {}
target:
entity_id: light.bathroom
mode: single
A checkbox in the UI to prevent this unexpected behavior will help users to quickly reach their automation goals.
Thanx to the developers for this great work and this great UI editor for automations.
Would be great to see this feature in future releases.