From what I have learned from studying numerous posts, it appears that I can not natively trigger an automation based on the state of an individual entity that is part of a group. But, that I can create a sensor first that can be used as a trigger to do something similar?
template:
- sensor:
- name: People
state: >-
{{ expand('group.people') | list | count }}
How do I use this sensor in an automation? I would like to use the notify service to message the friendly_name and state of the entity that changed. i.e. John is Home
I feel like this must have been covered somewhere, but it’s been challenging to find a post since it appears the ‘expand’ functionality was updated in the past year to simplify this process? More likely, I can’t see the forest through the trees?
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.
Thank you, @123. This looks like what I am looking for. It’s difficult for me to test right at this very minute since it requires the arrival or departure of someone in my people group. But if I simply remove the selectattr() filter, will this still work by giving me ALL state changes, home and not_home?
template:
- sensor:
- name: Latest Person State Change
state: >-
{% set x = expand('group.people') | 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 '' }}
Yes, if you remove the selectattr filter, the template will report the entity_id of the latest person entity to have changed its state value. The second line in the template ensures it restricts it to state-changes that have occurred within the last 3 seconds.
For future reference, it’s the custom of this community forum to add the Solution tag to the first post that identifies the problem and/or supplies the solution. In this case, you marked your own post as the Solution but it simply applies the solution that was suggested to you. For more information, refer to guideline 21 in the FAQ.