So I’m trying to pin down an automation that turns on a light when one of five doors is opened, and stays on until the same door is closed. I don’t want the light to turn off if a different door is opened and then closed, for example, while the initial door is still open.
I’m able to get the light to turn on with the automation, but I’m having trouble with the wait trigger to turn it off. Currently, I’m trying to look for the “off” state (or at least that’s what I think I’m doing!) and then continue the automation to turn the light off, but that doesn’t seem to work in my current setup. I’m not super familiar with variables or templates and presumably, that’s where my issue lies.
Any help on how to fix or a better solution is greatly appreciated!
Your timeout is zero, so you’re not waiting at all. You’re continuing immediately.
Do away with the device triggers. Just use state triggers. The following won’t work unless you do.
Make two automations:
The first trigger on any of the doors being opened (similar to what you have now). Save the one that triggered using trigger.entity_id and save that value to an input_text helper.
The second automation has the same triggers, but for changing to the closed state. It also gets a condition to check whether the one closed matches the value saved in the helper.
The following example turns on when the first door is opened. The door’s entity_id is recorded in the Binary Sensor’s previous attribute. When the same door is closed, the Binary Sensor turns off. Its simple on/off action makes it easy to trigger an automation controlling a light.
template:
- trigger:
- platform: state
entity_id:
- binary_sensor.door1
- binary_sensor.door2
- binary_sensor.door3
to:
- 'on'
- 'off'
- platform: state
entity_id:
- cover.garage1
- cover.garage2
to:
- 'opened'
- 'closed'
binary_sensor:
- name: First open door
unique_id: first_open_door
state: >
{% set current = trigger.entity_id | default(none) %}
{% set previous = this.attributes.previous | default(none) %}
{{ (previous is none and current is not none) or
(previous is not none and current != previous) }}
attributes:
previous: >
{% set current = trigger.entity_id | default(none) %}
{% set previous = this.attributes.previous | default(none) %}
{{ current if previous is none else previous if current != previous else none }}
I tested the example in my home, using three doors, and it worked correctly. However, the ultimate test will be in your environment. Let me know if you encounter any problems.
The following automation simply turns on the light when the Binary Sensor is on and turns it off when the Binary Sensor is off.