How to use {% For entity_id in group %} in automation

HI,

I use several automations that check the state-change of the entity-id’s of a group and then call an action.

Right now, i have to state all individual entities, and cant just state the group (cause the automation than uses the group.state)
Can’t i somehow use a template with a for-statement?
for entity_id in group, check state change, action…

Example automation:

# Call Summary after Lights change
- alias: 'Sense Lights change'
  id: '1511601488008'
#  hide_entity: True
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: 'light.dining_corner,light.dining_table_lamp_1,light.dining_table_lamp_2,
                light.dining_table_lamp_3,light.kist,light.cabinet,light.home_theater,
                light.corridor_cabinet,light.bedside_table,light.lounge_chair_long,
                light.drawer,light.gamer_den,light.table,light.bureau,light.inside,
                light.outside,light.hue_go_1'
  condition: []
  action:
    - service: python_script.anything_on
    - delay: 00:00:02
    - service: python_script.summary

All the above lights are in group.all_lights, so i’d rather use something along the lines of:

# Call Summary after Lights change
- alias: 'Sense Lights change'
  id: '1511601488008'
#  hide_entity: True
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: {{states.group.all_lights_only.attributes.entity_id}}
  condition: []
  action:
    - service: python_script.anything_on
    - delay: 00:00:02
    - service: python_script.summary

Which would make it so more robust, and even more importantly , would never need the automation to change, if I change the group members…

Ive checked everywhere i can think of in the doc’s but no mentioning of this at all.

trying to get this working…

{% for attr in states.group.all_lights_only.attributes.entity_id %}
      {{ states.{{attr}}.state }}
{%endfor%}

Please have a look? Thanks!
Marius

1 Like

Trigger is limited on what you can use as a value_template. Unfortunately, you cannot use a value template in entity_id for trigger. But to answer your question about the loop:

{% for entity_id in states.group.all_lights_only.attributes.entity_id %}
{{- entity_id }}
{% endfor %}

thanks!

couldn’t it be put under the condition like this:

id: testnotification
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id: group.anwesenheit_alle
    to: 'off'
  condition:
  - condition: template
    value_template: >-
      {% for item in states.group.fenster_und_tueren.attributes.entity_id if states(item) == 'closed' or states(item) == 'off' %}
        {% if loop.first %}
          {{loop.length}} < 13
        {% endif %}
      {% endfor %}

and then try to check on state change for each {{- entity_id}} ?
from: on to: off
or
from: off to:on

somehow…

this:

{% for entity_id in states.group.all_lights_only.attributes.entity_id %}
states.{{- entity_id }}.state
{% endfor %}

gives all the correct states definitions but not the values on or off…

yeah, because thats not the correct syntax. you can’t use {{}} inside a string and expect it to work. Jinja is annoying like that. This would be one option:

{% for entity_id in states.group.all_lights_only.attributes.entity_id %}
{% set domain, device = entity_id | split(’.’) %}
{{ states[domain][device].state }}
{% endfor %}

unfortunatly gives:

Error rendering template: TemplateSyntaxError: unexpected char '’' at 115

change it to a single quote, in your text editor, don’t copy it from here. it changes into a different type of singe quote.

also, the middle line is actually:

{% set domain, device = entity_id.split(’.’) %}

yes that works, thank you very much.

can you think of a way to get this into a condition checking the state changes per entity_id?

Dear Guys,

how can i apply this solution now in the trigger: part?

Basically i want to have something like that, i change sensor sets and its rather annyoing not to work with groups in the entity_id section :frowning: and iterate through them to get the result, where i have just to replace the sensor in the group instead of in each automation which uses this group.

Thanks for your help.

That’s not possible. You always have to list out the entities for triggers.

Okay then back to my idea of the dirty workaround :frowning: --> iterate through groups with every hass restart and create a file, include that file in the entity_id section :frowning: like i did manually already.

alias: Intruder detected Armed Away
trigger:
  - platform: state
    entity_id: !include ../../alarm_sensors.yaml
    to: 'on'

the groups could be sooooo helpful, would safe a lot of work.

Looking for same solution, bummed HA can’t support triggering on groups, more precisely entity_id changes in group

Great hack/idea @DuckY1987 … wouldn’t have thought of this and will put it in my back pocket if i get desperate. But not excited about having to do this

If you get this working, mind posting code how to build YAML list?

would this be of any help:

- alias: 'State changed Active'
  id: 'State changed Active'
#  hide_entity: True
#  initial_state: 'on'
  trigger:
    platform: event
    event_type: state_changed
  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id in state_attr('group.binary_sensors_active_template','entity_id') }}
  action:
    - delay: 00:00:02
    - service: python_script.summary

of course, change the group, condition, and action to your liking…

- condition: template
  value_template: >
      {{trigger.to_state.state in ['on','off'] }}

or

- condition: template
  value_template: >
      {{trigger.to_state.state =='on' }} in this case

the lights automation I need above is like this in the new format:

- alias: 'State changed Light'
  id: 'State changed Light'
  trigger:
    platform: event
    event_type: state_changed
  condition:
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id in state_attr('group.all_lights_only','entity_id') }}
  action:
    - delay: 00:00:02
    - service: python_script.whats_on
    - delay: 00:00:02
    - service: python_script.summary
1 Like

@fryguy04

I’m not happy that its soooo complicated either…, but yes my workaround is implemented as shell_command, and that you can call whenever you want, manual, or after each system restart or before each shutdown:

update_alarm_group: /bin/bash -c "echo -e '{% for entity_id in states.group.alarm.attributes.entity_id %}- {{ entity_id }}\n{% endfor %}' > /path-to-group/alarm_group.yaml" 

I didn’t test it yet, but you should reload your automations too, after you executed this.

Lets be honest, i lost track where i use my entities, so thats why i want to have them in groups too :slight_smile: Yes i can search in my gitlab repo, but thats really awkward, i want to have it dynamic and automatically updated when i replace a sensor.

1 Like

You can simplify this:

update_alarm_group: /bin/bash -c "echo -e \"{{ \"- \" + states.group.alarm.attributes.entity_id | join(\"\n- \") }}\" > /path-to-group/alarm_group.yaml"
1 Like

thanks i might just use this!

I wonder if you can try using a template sensor for this by expanding the group and comparing the current state of each entity to the trigger.from_state.state of each entity, then use that as the trigger in your automation.

Are you trying to just use a groups entity as the trigger without specifying a list? This thread is really really old and many things have changed.

That is how I read what the poster was trying to do. I found this thread as I am doing something similar but using a template sensor as the trigger and iterating the group entities within the template sensor.

Make your template sensor output the count of the entities in the group that match the state you want. Your other option is to make the value of the sensor binary based on the sensor states as well.

E.g.

{{ expand('group.xyz') | map(attribute='state') | map('replace', 'on', 'a') | map('replace', 'off', 'b') | join }}

That will have a unique identifier for each state that you have in the group. Keep in mind, it would be limited to a group of 254 entities.