I have this automation setup and running fine but I want to add a 40min limit on light, if e.g door is left open, in that case light should turn off after 40mins . How should I add that?
alias: Bathroom door light
description: "Turn on and off the bathroom light using the aqara contact sensor. "
triggers:
- entity_id:
- binary_sensor.door_sensor_2_opening_2
from: "off"
to: "on"
id: first opening
trigger: state
conditions: []
actions:
- metadata: {}
data: {}
action: light.turn_on
target:
entity_id: light.sonoff_zbminir2
- wait_for_trigger:
- entity_id:
- binary_sensor.door_sensor_2_opening_2
from: "off"
to: "on"
id: "wait for door to open again. "
trigger: state
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 5
- metadata: {}
data: {}
action: light.turn_off
target:
entity_id: light.sonoff_zbminir2
mode: single
For 40 minutes, I would not make a long running automation with a delay.
You can either do a check if the door is open for 40 minutes then turn off the lights, but that may be triggered too much. You could add in a condition that the light has been on for almost that time too.
Another option is to create a timer helper in the helper section (with the option set to restore the timer on reboot). You can then start the timer and abort it at whatever events you think are appropriate. When the timer runs out you turn the light off. You can abort the timer e.g. when the door closes, when the light is operated manually, etc.
alias: Bathroom door light
description: "Turn on and off the bathroom light using the Aqara contact sensor, with a 40-minute auto-off timer."
trigger:
- platform: state
entity_id: binary_sensor.door_sensor_2_opening_2
from: "off"
to: "on"
id: first_opening
condition: []
action:
- service: light.turn_on
target:
entity_id: light.sonoff_zbminir2
# Optional: Wait for door to open again before turning off (your logic may vary)
- wait_for_trigger:
- platform: state
entity_id: binary_sensor.door_sensor_2_opening_2
from: "off"
to: "on"
id: wait_for_door_to_open_again
- delay:
minutes: 40
- service: light.turn_off
target:
entity_id: light.sonoff_zbminir2
mode: single
With a for, a trigger will happen when the described state is that way for exactly the given time. So closing the door and opening it again will reset it, so will going unavailable. If it was already longer, nothing happens. For conditions, it needs to be at least the given time.
Your automation seems okay to me, but I will change the mode from single to restart, so that whenever an event triggers, the automation starts from scratch.
if you close door / restart the automation you restart the timer as well. I strongly suggest you use node red as you can easily get more technical without an essay of code
Not proclaiming this is the best way, I have a number of automation that I limit the runtime in this way. Using a Timer that is started on the initial action i.e opening a door, switching on a light / fan. This automation will also reset if that action is reversed within the time the timer is running, so someone switch off the light etc. while the timer is still active.
Why use a timer, well the major reason it survives a reboot of HA or a reloading of the automations
alias: Automation - Bathroom - EnSuite - Extractor - Timer
description: ""
triggers:
- type: turned_on
device_id: 39f485edaff7aeb63520fe5f119d0ddf
entity_id: 6f01e3010dddbee598b07cd1d1f7b2d4
domain: switch
trigger: device
id: Ensuite Fan On
- type: turned_off
device_id: 39f485edaff7aeb63520fe5f119d0ddf
entity_id: 6f01e3010dddbee598b07cd1d1f7b2d4
domain: switch
trigger: device
id: Ensuite Fan Off
- event_type: timer.finished
event_data:
entity_id: timer.ensuite_extractor_fan
id: Ensuite Fan Timer Complete
trigger: event
conditions: []
actions:
- choose:
- conditions:
- condition: trigger
id:
- Ensuite Fan On
- condition: state
entity_id: timer.ensuite_extractor_fan
state: idle
sequence:
- metadata: {}
data:
duration: "0:30:00"
target:
entity_id: timer.ensuite_extractor_fan
action: timer.start
- conditions:
- condition: trigger
id:
- Ensuite Fan Off
sequence:
- target:
entity_id:
- timer.ensuite_extractor_fan
data: {}
action: timer.cancel
- conditions:
- condition: trigger
id:
- Ensuite Fan Timer Complete
sequence:
- type: turn_off
device_id: 39f485edaff7aeb63520fe5f119d0ddf
entity_id: 6f01e3010dddbee598b07cd1d1f7b2d4
domain: switch
mode: single
You will notice the triggers are not really included e.g. the door sensor switching on what ever, that I left in a separate automation if required, but in my case I have 99% smart-switches, so just tracking their state is enough to trigger the timer automation.