Get entity in group automation trigger

I have a group of binary_sensors (group.sensors) that I use in an automation trigger:

automation:
  trigger:
    platform: state
    entity_id: group.sensors
  action:
    - service: notify.email
      data_template:
        title: >
          {{ trigger.entity_id }} {{ trigger.to_state.state }}
        message: >
          entity_id: {{ trigger.entity_id }}
          to_entity: {{ trigger.to_state.entity_id }}
          to_object: {{ trigger.to_state.object_id }}
          to_state: {{ trigger.to_state.state }}

All the trigger fields are for group.sensors. Can I get at the individual entity that caused the trigger?

If I can’t use a group to do this, would I have to enumerate all 17 sensors in a trigger in order to tell which sensor triggered?

subscribing…

Did you ever get this sorted?

No, I haven’t

I stumbled across this looking for something else and I’m not sure if I understand your issue completely but, I use the following automation to find out which of my garage doors was left open:

automation/notifications.yaml:

- alias: Garage Doors Left Open
  trigger:
    - platform: state
      entity_id: group.garage_door_jc, group.garage_door_ac
      from: 'off'
      to: 'on'
      for:
        minutes: 10
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.automation_override
        state: 'off'
  action:
    - service: notify.all
      data_template:
        data:
          sound: mechanical
          priority: 1
        message: >
          {{ trigger.from_state.attributes.friendly_name }} has been open
          for > 0:10.

group/people.yaml: ``` garage_door_jc: name: John's Garage Door entities: - binary_sensor.nw_garage_door_sensor_36_0 ```

Hopefully this will help.

1 Like

Looking for a solution to this question as well… Just need to know how to get the actual triggering component out of the group of possible ones.

Not possible. You have to put all of the entities in the trigger in order to get the name in the action.

1 Like

Ok, thanks for the clarification

Did you receive any solution for this, i am also in the same situation, I want to have trigger based on one of the sensor in the group I have around 26 sensor but want message say which sensor trigged this automation flow.

The only way to do it is to include each entity in the trigger of the automation. For any other solution, you would have to custom develop something…maybe AppDaemon?

Would script work? how do I access the trigger data in the action block?
Like by the time my script executes, the door may be closed so i cant check the status of all door in the action block.

I created a Workaround

Add a shellcommand:

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

And include the file in the trigger block:

trigger:

  • platform: state
    entity_id: !include …/…/alarm_group.yaml
    to: ‘on’

You can either manual execute the shellcommand to update the group, or build an automation.

1 Like

I found this post while looking for a solution to this exact same question, since I couldn’t find a solution I started throwing guesses at the dev-template tool in HA and arrived at this one solution that works well for me:

- alias: 'Notify if door/window opened for 2 minutes'                                                                                                   
  trigger:
    - platform: state
      entity_id: group.doors, group.windows
      to: 'on'
      for:
        hours: 0
        minutes: 2
        seconds: 0
  action:
    - service: notify.jardi_smtp
      data_template:
        title: 'Door/Window Opened'
        message: >-
          {% for entity in trigger.to_state.attributes.entity_id %}
            {% if states(entity) == 'on' %}
              {{ state_attr(entity, 'friendly_name') }} has been opened for 2 minutes
            {% endif %}
          {% endfor %}
12 Likes

need this for if anything is turned “off” which i cant seem to get working, when i use this

To bad this is not working (HAOS 6.6)

It’s keeping me all the time the first entity in the group. Would love to see a script that works…
Its so much nicer to have a group instead of a whole list of entities …

That’s what this template will do. It’ll tell you all the ones in the group that are on, if your message can’t handle multiple lines, then its a problem with your notification device.

It can be done like this:

alias: Security | Alarm - profile away
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.group_00_00_voordeur
      - binary_sensor.group_00_01_woonkamer
      - binary_sensor.group_00_02_garage
      - binary_sensor.group_01_00_slaapkamer
      - binary_sensor.group_01_01_werkkamer
      - binary_sensor.group_02_00_zolder
    from: 'off'
    to: 'on'
condition: []
action:
  - service: notify.mobile_app
    data:
      message: >-
        Security | Alarm triggered


        Group: 

        - {{trigger.to_state.attributes.friendly_name}}

        Sensor(s): 

        - {{ expand(trigger.to_state.attributes.entity_id)|selectattr('state',
        'eq', 'on')|map(attribute='name')|list|join(', - ') }}
mode: single

See “Sensor(s)” part.
Since the group is triggering the automation, that trigger state cannot be used to show the entity that triggered the group. However when checking the group for sensors with state = on immediately after the automation was triggered, the only entity with ‘on’ state from that group is the one that triggered the whole thing.
Even if there are more sensors with state = on, the statement will show a list of entities.

4 Likes