That’s fine with the second automation, because there is no waiting. The problem with the first automation is that if you are still adding new automations or editing existing ones, every time you save - ALL automations are reloaded, and any timers, including ones that are waiting for an entity or attribute to be in a specified state for a period of time - get reset. But they won’t start immediately counting down the timer again after the reload, because the state of the entity has to change from whatever state it is currently in, to the state desired before the timer will start again. Thus if the sensor is already off, and you have done anything that causes all automations to be reloaded, then the sensor first needs to go ON and then back OFF and be OFF for 20 minutes before the automation will run.
This is the reason I prefer to use actual timers, and then I just listen for the timer finished event in my trigger.
trigger:
- type: opened
platform: device
device_id: 1840d0e06367e8bd14385b058e0d7bf7
entity_id: binary_sensor.bathroom_door_contact
domain: binary_sensor
id: door_open
- platform: device
type: turned_on
device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
entity_id: light.bathroom_light
domain: light
id: light_on
for:
hours: 0
minutes: 0
seconds: 5
milliseconds: 0
- platform: event
event_type: timer.finished
id: timer_finish
event_data:
entity_id: timer.bathroom_light
- platform: device
type: turned_off
device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
entity_id: light.bathroom_light
domain: light
id: light_off
As you can see here, I listen for the light being turned on, the light being turned off, the door being opened AND the timer finishing.
Now for the important part:
sequence:
- type: turn_on
device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
entity_id: light.bathroom_light
domain: light
- service: timer.cancel
target:
entity_id: timer.bathroom_light
- service: timer.start
data:
duration: '00:20:00'
target:
entity_id: timer.bathroom_light
When the door is opened, the timer is first cancelled, in case it was already running, and then started with a 20 minute duration.
- conditions:
- condition: trigger
id: timer_finish
sequence:
- service: light.turn_off
target:
entity_id: light.bathroom_light
When the timer finishes (matched by the timer_finish id in the trigger) then I turn the light off.
Here is the full automation so you can see the other stuff it does:
alias: 'Light: Bathroom Light'
description: ''
trigger:
- type: opened
platform: device
device_id: 1840d0e06367e8bd14385b058e0d7bf7
entity_id: binary_sensor.bathroom_door_contact
domain: binary_sensor
id: door_open
- platform: device
type: turned_on
device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
entity_id: light.bathroom_light
domain: light
id: light_on
for:
hours: 0
minutes: 0
seconds: 5
milliseconds: 0
- platform: event
event_type: timer.finished
id: timer_finish
event_data:
entity_id: timer.bathroom_light
- platform: device
type: turned_off
device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
entity_id: light.bathroom_light
domain: light
id: light_off
condition: []
action:
- choose:
- conditions:
- condition: and
conditions:
- condition: trigger
id: door_open
- condition: or
conditions:
- condition: state
entity_id: binary_sensor.day_night_time
state: 'off'
- condition: numeric_state
entity_id: sensor.lux_sensor_illuminance_lux
attribute: illuminance_lux
below: '1500'
sequence:
- type: turn_on
device_id: 9fe2e0f2f64e8e167f80d7ae8773b25b
entity_id: light.bathroom_light
domain: light
- service: timer.cancel
target:
entity_id: timer.bathroom_light
- service: timer.start
data:
duration: '00:20:00'
target:
entity_id: timer.bathroom_light
- conditions:
- condition: trigger
id: light_on
- condition: state
entity_id: input_boolean.house_in_night_mode
state: 'off'
sequence:
- service: timer.cancel
target:
entity_id: timer.bathroom_light
- service: light.turn_on
target:
entity_id: light.bathroom_light
data:
transition: 20
brightness_pct: 50
- service: timer.start
data:
duration: '00:20:00'
target:
entity_id: timer.bathroom_light
- conditions:
- condition: trigger
id: light_on
- condition: state
entity_id: input_boolean.house_in_night_mode
state: 'on'
sequence:
- service: timer.cancel
target:
entity_id: timer.bathroom_light
- service: light.turn_on
target:
entity_id: light.bathroom_light
data:
transition: 20
brightness_pct: 8
- service: timer.start
data:
duration: '00:10:00'
target:
entity_id: timer.bathroom_light
- conditions:
- condition: trigger
id: timer_finish
sequence:
- service: light.turn_off
target:
entity_id: light.bathroom_light
- conditions:
- condition: trigger
id: light_off
sequence:
- service: timer.cancel
target:
entity_id: timer.bathroom_light
default: []
mode: restart