I will! cool.
A quick glance though shows the value_template in your overrides, that resemble the beginning of this thread though… It’s always the finetuning when automatic takeover causes us to be bothered by exceptions
to finalyze this thread, thanks to the OP @Jonde I now have this little but effective combination added to my setup, which will easily be translated to a broader set of lighting scene in my use-case:
automation:
- alias: 'Living lights off when no motion'
id: 'Living lights off when no motion'
# initial_state: 'on' #use restore state for this
trigger:
platform: state
entity_id: binary_sensor.auditorium_motion_sensor #use the new Hue CC creating binary_sensors for the motion_sensor
condition: [] # took these out since I don't need that
action:
- service: script.turn_off
entity_id: script.switch_off_lights_delay
- condition: template
value_template: >
{{ is_state('binary_sensor.auditorium_motion_sensor', 'off') }} #need only to check for state 'off' now
- service: script.switch_off_lights_delay
and script:
switch_off_lights_delay:
alias: Switch off lights with delay
sequence:
- delay:
minutes: >
{{ states('input_number.lighting_timer' )|int }} # I use another template, but that is of no importance, as long as the templates evaluates to a correct number ;-)
- service: light.turn_off
entity_id: light.living # further development can use variables here, to make it more generic and re-usable with other lights and scenarios
this is new:
input_number:
lighting_timer:
name: Lighting timer
icon: mdi:timer
initial: 20
min: 0
max: 60
step: 10
and getting back to the OP’s needs, this should do it:
automation:
- alias: 'Bathroom lights off when no motion'
id: 'Bathroom lights off when no motion'
initial_state: 'on'
trigger:
platform: state
entity_id: sensor.bathroom_motion_sensor
condition:
condition: time
after: '07:00:00'
before: '00:00:00'
action:
- service: script.turn_off
entity_id: script.switch_off_lights_delay
- condition: template
value_template: >
{{ is_state('sensor.bathroom_motion_sensor', 'off') }}
- service: script.switch_off_lights_delay
script:
switch_off_lights_delay:
alias: Switch off lights with delay
sequence:
- delay:
minutes: >
{{ '2' if states('sensor.zha_01ddaf89_1_1029' ) | int >= 50 else '1' }}
- service: light.turn_off
entity_id: light.bathroom
__EDIT__UPDATE
since this automation is now working very promptly, I can see it being triggered to run the script more often than motion explains. And I should have anticipated that… sorry.
triggers with platform state trigger in each change in the state, not necessarily the state.state, but also the attributes changes. Since this is a motion_sensor with many attributes that change constantly:
we need to narrow down the triggering to only trigger on to: 'on'
and to: 'off'
Ive changed the automation accordingly, adding also an extra condition that seems superflous but ensures for 100% the action won’t take place if condition isn’t met:
- 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
I would ultimately use the {{ trigger.to_state.state == 'off' }}
condition, but somehow this doesn’t pass, I don’t understand why just yet, because I use that in many automation elsewhere in the setup…