How can I have an automation listen to all entities of a certain device class?

I have an automation that is triggered by doors opening/closing. I want to change it so that I don’t have to edit the trigger every time I add a new door sensor and instead have it trigger by all doors without specification.

I can achieve what I want to do in node red, but getting everything working after that is too much trouble, and I already have an automation working perfectly, just needs the trigger part changed.

I have tried an event trigger and then adding a condition but I really don’t like that that means the automation is constantly being triggered by all events. I’d rather have it only trigger when it’s actually a door. I also tried a template trigger that checks that the device_class attribute is door, but quickly realized that the way I have it set up I’m just listening for an entity to change it’s device class to door lol.

Most likely you just need a Template-based trigger, but you will need to provide more details about what you mean by

Generally speaking, you can limit a template to a specific domain by using the state object format. For doors, use binary_sensor then apply filters to get your desired result. The following example could be used as a template sensor to trigger on every opening or closing of a door:

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

Edited for clarity

1 Like

Yeah, the current automation trigger is

platform: state
entity_id:
  - binary_sensor.garage_door
  - binary_sensor.front_door
  - binary_sensor.bedroom1_door
  - binary_sensor.bedrom2_door

And so it will trigger any time any one of these doors state changes.

That template just returns a count of doors that are currently opened. Even if I change it to select attributes on and off, would that still work as a trigger? Or should I just get rid of the list and count flags?

Sorry, I didn’t explain that coherently… Use the template in a template sensor, then trigger off the state of that sensor. The sensor’s state will update every time you open or close a door. If you need to only trigger on openings or closing you can use trigger variables in your conditions.

Ah, gotcha. Unfortunately that wouldn’t work for my setup. In my automation I pull details about the specific door that changed, like it will send me an alert if I’m not home about what door opened and other things like that.

Those are the kind of details that I mentioned in my first post… It’s a lot easier for the rest of us to be helpful if you share your automation or at least a complete description of what you are trying to do.

Sorry about that, I definitely should have included it in my original post. Here’s the full automation as it currently stands.

alias: Announce Doors Opened/Closed
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.garage_door
      - binary_sensor.front_door
      - binary_sensor.bedroom1_door
      - binary_sensor.bedroom2_door
condition:
  - condition: template
    value_template: >-
      {{ "unavailable" not in [states("trigger.from_state"),
      states("trigger.to_state")] }}
action:
  - choose:
      - conditions:
          - condition: not
            conditions:
              - condition: state
                entity_id: person.him
                state: home
        sequence:
          - service: notify.mobile_app_him
            data:
              title: Door changed
              message: >-
                {{ trigger.to_state.attributes.friendly_name }} is {% if
                trigger.to_state.state == 'on' %}
                  open
                {% elif trigger.to_state.state == 'off' %}
                  closed
                {% else %}
                  unavailable
                {% endif %}. Door was {{ trigger.from_state.state }}
    default: []
  - service: tts.google_cloud_say
    data_template:
      entity_id: |-
        {% if states.person.him.state == 'home' %}
          {% if states.automation.nighttime_lights.state == 'on' %}
            media_player.minis
          {% else %}
            media_player.office_speaker
          {% endif %}
        {% else %}
          media_player.minis
        {% endif %}
      message: >-
        {{ trigger.to_state.attributes.friendly_name }} is {% if
        trigger.to_state.state == 'on' %}
          open
        {% elif trigger.to_state.state == 'off' %}
          closed
        {% else %}
          unavailable
        {% endif %}.
mode: single
max: 10

From what I can see, you’ve already set your trigger up the most efficient way possible. Currently, state trigger don’t accept templates. I think you could get most of the data you’re using by completely restructuring you automation to work without using trigger variables, but I wouldn’t bother with it. There are only so many doors or in a home. If I had to listen to a tts announcement every time a door was opened or closed I’d probably end up smashing my smart speakers.

I definitely get your point, but I also only plan to add sensors to key points across the house.