Template Trigger for any door opened

I am trying to write an automation with a template trigger to catch when any of the external doors at my property are opened. I would like the trigger to evaluate to the entity_id of the door that has opened. The automation can then call services to change the state of other devices in the area allocated to the door in question.

So far, I have managed to write the template so it triggers if any one door is opened. However if a second door is opened before the first is closed it won’t be caught by my template since it just returns the count of doors in the open state.

{{states.binary_sensor | selectattr('attributes.device_class','eq','door')|rejectattr('state','eq','off')| list|count }}

Since any positive value is treated as ‘true’ the state of the trigger doesn’t change when more than one door is opened.
Can anyone suggest template code that will trigger when any door is opened, regardless of how many are already in the open state??

I’d suggest using a state trigger listing out each of the binary sensors looking for to: 'on'. Can’t have too many doors to do that, surely?

{{ trigger.entity_id }}

will then refer to your just-opened door.

exactly my scenario, and works beautifully

ill post my template triggers which have a bit of history too:

template:

  - trigger:

      - platform: state
        entity_id:
          - binary_sensor.achter_deur
          - binary_sensor.gang_deur
          - binary_sensor.voor_deur
          - etc etc
        from: 'off'
        to: 'on'

    sensor:

      - unique_id: last_time_door_triggered
        state: >
          {{as_local(states[trigger.entity_id].last_changed).strftime('%X')}}
        attributes:
          history: >
            {%- set previous = this.attributes.history|default([]) %}
            {%- set new = [this.attributes.last_area|default('nowhere') ~
              ': ' ~ this.state] %}
            {{previous[-4:] + new}}
          last_area: >
            {{area_name(trigger.entity_id)}}

      - unique_id: last_door_triggered
        state: >
          {{state_attr(trigger.entity_id,'friendly_name').split(' deur')[0]}}
        attributes:
#           history: >
#             {%- set what = states[trigger.entity_id].name %}
#             {%- set at = states[trigger.entity_id].last_changed.strftime('%X') %}
#             {%- set previous = this.attributes.history | default([]) %}
#             {%- set new = [what + ': ' + at] %}
#             {{ new + previous[:4]}}
          last: >
            {% set stamp =
               as_local(states[trigger.entity_id].last_changed).strftime('%X') %}
            {% set id =
               state_attr(trigger.entity_id,'friendly_name').split(' deur')[0] %}
            {{id ~ ': ' ~ stamp}}
          history_1: >
            {{this.attributes.last|default('Nog niet bepaald')}}
          history_2: >
            {{this.attributes.history_1|default('Nog niet bepaald')}}
          history_3: >
            {{this.attributes.history_2|default('Nog niet bepaald')}}
          history_4: >
            {{this.attributes.history_3|default('Nog niet bepaald')}}
          history_5: >
            {{this.attributes.history_4|default('Nog niet bepaald')}}
          entity_id: >
            {{trigger.entity_id}}
          last_triggered: >
            {{trigger.to_state.last_changed.isoformat()}}
          last_area: >
             {{area_name(trigger.entity_id)}}

btw, if you merely want a sensor to check which are open:

  - sensor:

    - unique_id: sensor_deuren
      state: >
        {{expand('binary_sensor.deuren')|selectattr('state','==','on')|list|count}}
      icon: >
        mdi:door-{{'open' if this.state|int(-5) > 0 else 'closed'}}
      attributes:
# experimental using custom-ui plugin
        icon_color: >
          {{'var(--alert-color)' if this.state|int(-5) > 0  else 'var(--primary-color)'}}
        deuren: >
          {%- set count = this.state|int(0) %}
          {% set phrase = 'deur staat' if count == 1 else 'deuren staan' %}
          {%- if count > 0 %}
          {{count}} {{phrase}} open: {{expand('binary_sensor.deuren')
                      |selectattr('state','==','on')
                      |map(attribute='name')|join(', ')}}
          {%- else %} Alle deuren zijn dicht.
          {%- endif %}
1 Like

Did you try with simple solution like use of a group helper ?