Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 106251

Automation trigger entity_id list based on the names of entities

$
0
0

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?

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 106251

Trending Articles