- alias: 'Living lights off when no motion'
id: 'Living lights off when no motion'
# initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.auditorium_motion_sensor
to: 'on'
- platform: state
entity_id: binary_sensor.auditorium_motion_sensor
to: 'off'
condition:
condition: template
value_template: >
{{ trigger.to_state.state in ['on','off'] }}
action:
- service: script.turn_off
entity_id: script.switch_off_lights_delay
- condition: template
value_template: >
{{ is_state('binary_sensor.auditorium_motion_sensor', 'off') }}
# {{ trigger.to_state.state == 'off' }}
- service: script.switch_off_lights_delay
works perfectly if I use the conditional value_template in the action part. Trying to make it more generic, I replaced it with the now commented {{ trigger.to_state.state == 'off' }} but that simple won’t work. Why would that be? @petro@pnbruckner do you spot the error ?
Its all the more strange since the condition in the condition part uses the same trigger, which works just as expected.
that’s what I had before, but inthis post, under the EDIT__UPDATE is explained why it had to be ‘complicated’…
the motion sensor has many attributes that trigger the automation unwantedly
this has become a study in how to switch off lights when motion has stopped after a humidity dependable delay/switch off time… and build in a restart if motion is noticed during that delay.
its the same as with presence automation based on state change, where the devices battery and gps coordinates also cause the state to change, while the state.state is the same…
Really, I’ve tested extensively: without these constraints the motion_sensor triggers the automation on each light_level change and several other attributes:
it does so, even when the motion_sensor itself is switched off.
- alias: 'Living lights off when no motion'
id: 'Living lights off when no motion'
# initial_state: 'on'
trigger:
- platform: state
entity_id: binary_sensor.auditorium_motion_sensor
to: 'on'
- platform: state
entity_id: binary_sensor.auditorium_motion_sensor
to: 'off'
condition:
condition: template
value_template: >
{{ trigger.to_state.state in ['on','off'] }}
action:
- service: notify.notify
data_template:
message: >
{{ trigger.to_state.state }}
- service: script.turn_off
entity_id: script.switch_off_lights_delay
- condition: template
value_template: >
{{ is_state('binary_sensor.auditorium_motion_sensor', 'off') }}
# {{ trigger.to_state.state == 'off' }}
- service: script.switch_off_lights_delay
- service: notify.notify
data_template:
message: >
Again: {{ trigger.to_state.state }}
give me an ‘on’ on motion detection, and then nothing, which is correct. when motion detection switches to off, I receive an ‘off’, followed by an Again: off, indicating the script has been called.
A similar thing happened to me today. I had an automation with conditions based on trigger.to_state.state that never triggers. I simply modified the automation adding an action outside the conditions and then deleted the action and now it works