Better way to add entity_id list to automation trigger

So, I’ve been using the below for a while now…

...
trigger:
  - platform: state
    entity_id: !include ../triggers/triggers.yaml

triggers.yaml is a list of entity id’s…

- binary_sensor.sensor1
- binary_sensor.sensor2

I did the above so that I would have to track down a specific automation to add a new entity_id to. Just add it to the file and reload automations.

My question… Is there a better way to do this now?

Thanks in advance for input :slight_smile:

I assume you meant to say you are doing this so you do not have to find all automations that use the same set of entities (in a State Trigger). Your strategy is to centralize them in a file and simply !include the file wherever needed.

You could use YAML anchors and aliases but it amounts to the same thing (using a placeholder to represent information that is defined elsewhere) just without using a dedicated file. I don’t think it would be an improvement over what you have done.

BTW, how many automations do you have that effectively have the exact same State Trigger? I feel that might represent an opportunity to consolidate them into fewer automations.

Hi,
I don’t know of a better way but just wanted to say, that’s way cool. Had not even thought of doing something like that.
I’m curious, in that scenario does the trigger act like an OR or AND for the sensors? ‘sensor1 OR sensor2’ or ‘sensor1 AND sensor2’.
Cheers
Nick

The State Trigger’s documentation explains that it will trigger for any of the listed entities (logical OR).

State Trigger

The first example in the docs includes a State Trigger with multiple entities presented as a YAML list. What GAHA is doing is employing the !include directive which makes the YAML processor include information stored elsewhere.

I really just use it so I don’t have to edit the actual automation.

For example… I have an automation for doing various things when a motion sensor is triggered.

Then, I just append the new motion sensor to the motions.yaml trigger file.

Would be super useful if trigger entity_id’s accepted wildcards or regex as I have a standard naming convention and use the friendly name to distinguish what’s what.

What @123 said

There’s the recently introduced trigger_variables feature but, in its current form, it’s very limited (basically the variable only accepts a constant value).

The reason I mentioned consolidation is because if the exact same set of entities is used in many triggers then there’s potential for combining them into a single automation. Then there’s just one place where they all exist. If it cannot be reduced to a single automation then your strategy continues to be more convenient (i.e. centralized management of the State Trigger’s entities).

@123

I’m pretty good at writing, should I dare say ‘optimized’ automations using trigger data, choose, etc…

Thought about doing it this way, but I’m not crazy about the automation firing on every event :grimacing:

Thoughts?

- alias: "Dynamic Entity Id Automation"
  trigger:
    platform: event
    event_type: state_changed
  condition:  "{{ trigger.event.data.entity_id.startswith('binary_sensor.motion_')  }}"
  action: 
    ... 

Yeah… I saw the trigger_variables in one of the previous release notes. Would be pretty cool when it’s not so ‘limited’.

Agreed, I would avoid using an Event Trigger listening for every state-change.

If you have the time, can you give an example of what kind of automations are using the same State Trigger but cannot be consolidated into a single automation?

All of my automations are single automations. I use → python venv, all yaml, and automation: !include_dir_merge_list automation/ in configuration.yaml. I don’t like the gui stuff.

I use a standard naming convention and the !include …/triggers/some_file.yaml

In my automations I then set variables using trigger data.

So, I loved seeing the !include in the entity_id of a state trigger because I am currently using a script to write a list of entities to a file so I can copy it into an automation.

But, when I try the !include in the automation, I get an error trying to save it. Now, I am sad.

I know this is a couple years old. Has that ability been removed?

Show the yaml you’re using?

@jeffcrum

alias: 'Test'
trigger:
  - platform: state
    to: 'on'
    entity_id: !include ../triggers/test.yaml # move from automation dir to triggers dir

triggers/test.yaml

- binary_sensor.test_one
- binary_sensor.test_two

Thanks for the reply.

Here is what I have:

alias: Adjust Frozen
description: ""
trigger:
  - platform: state
    entity_id: !include triggers/frozen_food.yaml

/config/triggers/frozen_food.yaml

- input_number.freezer_breakfast_biscuits_and_gravy
- input_number.fridge_breakfast_biscuits_and_gravy
- input_number.freezer_breakfast_sausage_egg_cheese_biscuit
- input_number.fridge_breakfast_sausage_egg_cheese_biscuit

I have tried

    entity_id: !include triggers/frozen_food.yaml
    entity_id: !include ../triggers/frozen_food.yaml
    entity_id: !include /config/triggers/frozen_food.yaml

All the same result.

I am sure I am missing something.

Humour me and try:

["input_number.freezer_breakfast_biscuits_and_gravy", "input_number.fridge_breakfast_biscuits_and_gravy", "input_number.freezer_breakfast_sausage_egg_cheese_biscuit", "input_number.fridge_breakfast_sausage_egg_cheese_biscuit"]

I am getting the error above when trying to save the automation with the !include. It is the HA automation editor that won’t let me.

So, at this point, it doesn’t seem like it matters what is in the included file.

Let’s try something different, since the issue is with !include itself.

Put this in secrets.yaml (or however it’s configured via the UI):

some_entities: ["input_number.freezer_breakfast_biscuits_and_gravy", "input_number.fridge_breakfast_biscuits_and_gravy", "input_number.freezer_breakfast_sausage_egg_cheese_biscuit", "input_number.fridge_breakfast_sausage_egg_cheese_biscuit"]

And then reference it like this:

entity_id: !secret some_entities

That didn’t work either. The automation editor does not like the ‘missing’ entities.

So, I made the change in the automations.yaml file and put the include in there. Now, it does build the automation with the entities in /config/triggers/frozen_food.yaml.

I expect that if I need to make a change to that automation, I’ll have to do the same thing otherwise it will save with the entities again.

This is very cool though. Now, when I add another food, I just have to add it to the script that builds my input_number file and that trigger file. On restart, they are all picked up.

Thanks again for everyone’s assistance.

1 Like