I’m using this automation to turn off the Air Purifier when I open the window and turn back on when I close it. But it’s triggering even when the window is closed all the time.
alias: Air Purifier - Turn On/Off when the Window is closed/open for 30s
mode: single
trigger:
- platform: state
entity_id: binary_sensor.livingroom_window
for: '00:00:30'
condition: []
action:
- entity_id: fan.air_purifier
data: {}
service: >-
{% if is_state('binary_sensor.livingroom_window', 'on') %}
fan.turn_off
{% elif is_state('binary_sensor.livingroom_window', 'off') %}
fan.turn_on
{% endif %}
So you should have included that in your initial post (obviating the need to be rude to @m0wlheld)
It says triggered by the entity, so the way your automation is written it could be any attribute of that entity that causes the trigger.
You can either double up on the trigger (both with opposing from and to’s)
Or simpler insert a condition to ensure that trigger_to.state != trigger_from.state
If that fixes your problem you are laughing, otherwise you will at least have more information.
What type of sensor is it.?
Does the triggering actually do harm? It the purifier on or off when undesired? As already mentioned, state changes other then “to on” or “to off” can trigger this automation (state changes to “unavailable”), but the if-then-else-condition in your automation’s action should prevent unwanted results.
Thanks for all the replays.
I’m currently trying the condition from @Burningstone and for now it works fine.
My question was, why is triggering when there is no change (open/closed) from the sensor.
But probably the trigger was any other change like battery percentage drop, or linkquality.
Because the state trigger reacts to any changes to the state object, which includes attributes. As you didn’t specify any to or from for the ‘state’ itself, this would include things like battery level updates, signal strength updates etc.