Indicator light if one of multiple doors is opened until same door is closed

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!

alias: Alerts - Door Open Indicator Light
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 3ee0a7c4b5cedecb06bd5e58867fce8a
    entity_id: 16aa209f9474ee9a9a44afba2f64d098
    domain: binary_sensor
  - platform: device
    device_id: 9f2879c01fb84cac3265ecfe73040df7
    domain: cover
    entity_id: e5aa42c24785a98bd9018397ea39f42f
    type: opened
  - platform: device
    device_id: 9504759e8a85b410f41424a39e4a7e70
    domain: cover
    entity_id: 40404f6e0e07a8e571e9a394179ac25d
    type: opened
  - type: opened
    platform: device
    device_id: 9baf4abbaa337fb8809c2eaf9e7f161b
    entity_id: 6866f66371bc16f1f9842448223c6e25
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: 9bb3a77608dcd49f031b8afbe40eeb85
    entity_id: 5c6f43c04ff725bff50d0669b19cec72
    domain: binary_sensor
condition: []
action:
  - service: light.turn_on
    data:
      brightness_pct: 100
      color_name: red
    target:
      entity_id: light.ezm
  - wait_for_trigger:
      - platform: template
        value_template: "wait_template: \"{{ is_state('trigger.entity_id', 'off') }}\""
    timeout:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
    continue_on_timeout: true
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.ezm
mode: single

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.

Great thanks. Makes sense and I think I can manage it - appreciate the help!

A Trigger-based Template Binary Sensor is a good fit for your application.

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.

alias: example
trigger:
  - platform: state
    entity_id: binary_sensor.first_open_door
    from:
      - 'off'
      - 'on'
    to:
      - 'on'
      - 'off'
condition: []
action:
  - if: "{{ trigger.to_state.state == 'on' }}"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 100
          color_name: red
        target:
          entity_id: light.ezm
    else:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.ezm
mode: single

EDIT

Correction. Typo in previous attribute’s template. Replaced first is with if.
Correction. Typo in automation’s if statement. Replaced = with ==

2 Likes

Nice use of attributes for this.

1 Like