- alias: Device updated with each device
trigger:
platform: state
entity_id: device_tracker.hub,device_tracker.ipcam1,device_tracker.mba,device_tracker.range_extender,device_tracker.chromecast,etc...
action:
service: notify.ha_telegram
data_template:
message: "{{ trigger.to_state.attributes.friendly_name }} just changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}"
this doesn’t work
- alias: Device updated with all_devices
trigger:
platform: state
entity_id: group.all_devices
action:
service: notify.ha_telegram
data_template:
message: "{{ trigger.to_state.attributes.friendly_name }} just changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}"
@Rich_Paul could you share a snippet of your automation ?
The important thing to understand about using a group in a state trigger is that the group’s state only changes when it goes from “all off/not_home/etc.” to “one or more on/home/etc.”, and vice versa. Going from “one on/home/etc.” to “two on/home/etc.”, or any other transition where there are one or more on/home/etc. to still one or more on/home/etc., does not cause a state change (because the group’s state will continue to be on/home/etc.), and hence, will not trigger the automation.
Next, if you have a list of entities for a state trigger, the automation will trigger when the state of any of those entities transitions as defined. (If you don’t specify from or to then any state transition will cause a trigger.) If one of those entities (or the only entity specified) is a group, then the group’s state has to change (which may or may not happen when the state of a single entity in the group changes, due to what I described above.)
So, bottom line, if you want an automation to trigger when any entity in a group changes state, you can’t use the group in the trigger; you have to use the individual entities.
For what it’s worth, I’ve been trying to find a way to cause an automation to trigger when any item in a group changes. So far all attempts I’ve tried have failed. I’ve even been paying attention to other topics where this is discussed, and so far I haven’t seen any solutions. But it just occurred to me of one potential way to do it. I.e., create a sensor whose state is a count of the number of entities in a group that are on (or home or whatever.) I know how to do this with a template sensor. Then use that as the trigger. Also use a condition to qualify. E.g., if you want to trigger whenever someone comes home, the condition would be trigger.to_state.state > trigger.from_state.state, because someone coming home would cause the count of entities home to increase. Hmm, I’ll have to give this a try…
So here’s how to create a template sensor whose state is the number of people home. In this example I’m using the automatic device tracker group group.all_devices and an input boolean I use to indicate that there are guests staying in the house (which I use to effectively disable some automations):
sensor:
- platform: template
sensors:
n_people_home:
friendly_name: Number of People Home
value_template: >
{{ states|selectattr('entity_id','in',state_attr('group.all_devices','entity_id'))
|selectattr('state','eq','home')|list|count +
(1 if is_state('input_boolean.guests','on') else 0) }}
The first part filters down the list of all states in the State Manager to just those that correspond to entities in the automatic device tracker group, and then further filters those down to just the ones of those whose states are home. Then it filters that into a count of those state objects. The result is the number of entities in group.all_devices that are home.
Next I add one to that if input_boolean.guests is on.
I haven’t yet tried this in my automations, but the basic idea is, if you want to trigger when someone comes home (or if input_boolean.guests is turned on), then it might look something like this:
automation:
- alias: Trigger when someone comes home
trigger:
platform: state
entity_id: sensor.n_people_home
condition:
condition: template
value_template: >
{{ trigger.to_state.state|int > trigger.from_state.state|int }}
action:
...
nice! though in my setup the result is 39… many devices using home… now how to filter out those, instead of simply using group.family (which has max 6 entity_id’s in our household…)
Not exactly sure what you’re asking, but basically, if you have a group (or you can create one) that contains the entities you care about in some particular scenario, and you want to get the count of the entities in that group that are in some particular state, then you can adjust the template accordingly. I.e., change 'group.all_devices' to whatever group you’re using, and change 'home' to whatever state you’re interested in. And, of course, you can delete the + (1 if ... else 0) part.
using this template to do exactly what your trying to do above, count or show who is home:
- platform: template
sensors:
who_is_home:
friendly_name: Who is home
value_template: >
{% if is_state('group.family', 'home') %}
{{ dict((states|selectattr('entity_id', 'in', state_attr('group.family', 'entity_id'))
|list)|groupby('state'))['home']|map(attribute='name')|list|join(', ') }}
{%else%}
Nobody home
{%endif%}
the template works fine in the dev-tools, but in the frontend is showing only correct at reboot. Why doesnt this trigger automatically following the states of the devices?
refreshing the page, nor reloading the core help. only restarting Hassio.
sure, i understood as much, maybe was merely wondering if this was a real life scenario template or just for educational purposes. Really appreciate though, my next step in templating! so thanks
Hmm, good question. My “solution” of a count template sensor might suffer from the same problem. I only tested it by changing input_boolean.guests. Let me do some more testing by actually changing one of the device_tracker entities and see if it updates…
Darn! It doesn’t work either. I think the issue is the template processing code only sees the group entity (besides the input_boolean), so only updates when the group’s state changes, which is the same problem that started this topic. The only way I can see to solve this is to list all the entities of the group in the optional entity_id parameter of the template sensor. E.g.,
sensor:
- platform: template
sensors:
n_people_home:
friendly_name: Number of People Home
entity_id:
- device_tracker.name1
...
- device_tracker.nameN
- input_boolean.guests
value_template: >
{{ states|selectattr('entity_id','in',state_attr('group.all_devices','entity_id'))
|selectattr('state','eq','home')|list|count +
(1 if is_state('input_boolean.guests','on') else 0) }}
Not much better than just listing the individual entities in the automation trigger itself. But, I suppose, if you had many automations based on this, or had other uses for the count, then it might be worth it. I’ll give this a try and report back for completeness.
Listing the entity_id’s in the template sensor makes it work. Now whenever any of the individual device_tracker entities change the sensor is updated.
So there are certainly some trade-offs here. Using this technique requires creating a new entity, and although it simplifies the automation trigger, it requires an additional condition. Also, if any device_tracker entities are added or removed, it still requires updating accordingly. Still, I think it might be useful, especially as I said, if you have multiple automations based on this or could use the count elsewhere. I think I’ll switch over my automations to use it. If I run into any unexpected problems, I’ll be sure to reply here again with an update.