Automation trigger entity_id list based on the names of entities

Hi everyone,

I have a question on how I can make adding new sensors/entities in the future easier without having to add them manually to every automation I have.

I currently have my entities set up with a naming scheme that allows me to know which room they are in. In the future I want to be able to just add new sensors to my system, name them and have them automatically included in my automations without having to go into each automation and adding them manually to the trigger list. This preferably should be done based on the room or the device type which are both in their ID name.

This example checks if a motion sensor has been off for 120 seconds and then calls a script to do something in that room. I have multiple automations similar to this, another one for example would be using the same entity_id list but calling a different script when their state changes to ‘on’:

- alias: Automation
  mode: queued
  trigger:
    - platform: state
      entity_id: 
        - binary_sensor.motion_motionsensor_room1
        - binary_sensor.motion_motionsensor_room2
      to: 'off'
      for: '120'
  action:
    - service: script.do_something
      data_template:
        target_room: >
          {% set trigger_room = trigger.entity_id.split("_")[3] %}
          {% if trigger_room is not none %}
            {{ trigger_room }}
          {% else %}
            false
          {% endif %}

First I thought I could maybe use wildcards like this to have it check all motion sensors for state changes:

  trigger:
    - platform: state
      entity_id: 
        - binary_sensor.motion*
      to: 'off'
      for: '120'

I know now that wildcards don’t work in the entity_id list of triggers, are there any other ways to have entities automatically added to a trigger based on their ID?

If that’s not possible it’d also be fine if I only had to edit that list in one place. I tried using !include motionsensors.yaml and then have the list in there but that unfortunately didn’t work either.

  trigger:
    - platform: state
      entity_id: !include motionsensors.yaml
      to: 'off'
      for: '120'

with motionsensors.yaml looking like this in the same automation folder.

- binary_sensor.motion_motionsensor_room1
- binary_sensor.motion_motionsensor_room2

As far as I can tell this already doesn’t work simply because it’s an include within an included file. Is there any other way to use a set variable to replace the same entity list in every automation?

Use a group.

Unfortunately groups wouldn’t work in my case. As far as I understand groups, using a group would result in the trigger.entity_id being the id of the group and my following automation that relies on getting the room of the single entity that changed state wouldn’t work anymore.

So this line wouldn’t use the single entity_id of the entity in the group but the id of the group itself.

{% set trigger_room = trigger.entity_id.split("_")[3] %}

Groups unfortunately also have the disadvantage that they can not differentiate between 2 or 3 of the entities in that group changing state. Group state is either ‘on’ on 1 entity being on or all entities being on. So unfortunately it wouldn’t work for most of my automations where I need to know exactly which entity turned on even if others are already on.

I haven’t coded or found a good solution to this. I’ve seen some store the entity_ids in .yaml files & reference that but that .yaml may not be dynamically produced although it could be say once a day.

The potential variation incorporating your requirement and the suggestion of groups is a hybrid approach. Use the suggestion of the group as the trigger. Then define a templated variable within your data to check which entity_id (from those in the group) caused the trigger based on state. None of this feels efficient based on knowing that a device triggered the whole process initially.

I’m still trying to grasp the most effective modular approach to much within HA & the most efficient process that minimises CPU, memory & storage utilisation etc. The basics & flexibility within HA are amazing but I do find myself having to spend a lot of time working out a good approach. Good luck!

I honestly haven’t tinkered with my automations that much anymore so I haven’t looked for a new approach. However your solutions is already much better than what I currently have. Hadn’t thought of linking a .yaml list. Will implement this for now. Thank you for answering even though my question is 3 years old :slight_smile:

edit: Just remembered that I actually tried that back then but it didn’t work. Will try again to see if it works now. Thank you :slight_smile:

https://www.youtube.com/watch?v=ey8MoQUDsRM&t=675s Slacker Labs has a good take on this. Not sure it answers the question about which device triggered the automation because it uses a combo of groups, counting devices with a state (which I use elsewhere as say Lights On count) then the trigger can be based on the count > 0. I guess horses for courses as automating simple but repeated processes this works. It still doesn’t do what you want it to.

I found your post because I’m trying to recreate what you want to do which requires me to know the entity_id of the triggering device. I was hoping to automate it because I have 3 groups of sensors with each group triggering a specific light. OK, it’s just 3 automations but I was trying to use it as a test case to improve modularity plus knowing exactly which device triggered it is useful for further calls to more scripts.