Trigger an automation based on a group's individual entity state change

Actually, there is a way of doing it and it requires the use of a Template Sensor that reports the entity_id of the most recent group member that has changed its state (to a desired value).

Here’s an example where the group contains light entities and the Template Sensor reports the latest member whose state changed to on. In your case, it would be person entities changing to a desired state.

template:
  - sensor:
      - name: Latest Light On
        state: >
          {% set x = expand('group.my_lights') | selectattr('state', 'eq', 'on') | sort(attribute='last_changed', reverse=true) | list %}
          {{ (x[0].entity_id if now() - x[0].last_changed < timedelta(seconds=3) else '') if x | count > 0 else '' }}

The automation relies on a State Trigger to monitor the Template Sensor and a condition that rejects empty values.

alias: 'TEST: Tell which light bulbs turned on'
description: ''
trigger:
  - platform: state
    entity_id: sensor.latest_light_on
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state != '' }}"
action:
 ... etc ...
... trigger.to_state.state contains the entity_id of the light that just turned on ...

Let me know if you need help adapting the Template Sensor to work with person entities.

1 Like