Automation trigger on any member of a group, is it possible?

Hi!
I’ve got for example

- alias: 'Device updated'
  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 }}"

I would expect it to be triggered all the time for any state change but it never triggers, any idea why?

You need to add to the trigger:

from: ‘off’
to: ‘on’

You’ve said it’s a state trigger, but what state?

Or, just have it set as:

to: ‘on’

Thanks but I was curious to get any state change.
The documentation says that from/to are optional.

I think that my issue is the usage of a group…

Remember a group is basically an OR so the entire group needs to be off to have the group go off. If any single device is on the group will be on.

Not correct.

If any entity of that groups is on, the state of the group is also on.

All entities don’t have to be on for the group state to update…

I use this scenario with my playstation games group. If the group is on (ie any game been turned on) my automations fire.

I don’t care what is on within that group, just that something IS on

That’s what the OR implies. If any of the group members are on the group will be on. Only way for it to be off is if all the members are off.

thanks @jwelter I think that you’re right

this works

- 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 ?

  - alias: Ps4 group on - PS4 input, TV Lights on
    trigger:
      - platform: state
        entity_id: group.games
        to: 'on'
    action:
      - service: homeassistant.turn_on
        entity_id: switch.ps_input,light.tv_lights,switch.ps4_power
      - condition: template
        value_template: "{{ not is_state('sensor.virgin_status', 'COMMAND_TIMEOUT') }}"
      - service: switch.turn_on 
        entity_id: switch.virgin_power

That’s not a group?

I’d put all your device trackers in a single group (group.trackers etc)

  - alias: Ps4 group on - PS4 input, TV Lights on, Virgin off
    trigger:
      - platform: state
        entity_id: group.games
        to: 'on'
    action:
      - service: homeassistant.turn_on
        entity_id: switch.ps_input,light.tv_lights,switch.ps4_power
      - condition: template
        value_template: "{{ not is_state('sensor.virgin_status', 'COMMAND_TIMEOUT') }}"
      - service: switch.turn_on 
        entity_id: switch.virgin_power

OK so

  • a separe list of devices in entity_id in trigger : one of them match the rule -> action is triggered
  • a group in entity_id in trigger : all of them match the rule -> action is triggered
  • a group in entity_id in action : all of them are actioned on

Correct ?

Not quite.

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…

4 Likes

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:
      ...
1 Like

nice! though in my setup the result is 39… :wink: 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.

related probably:

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.

1 Like

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.

2 Likes

cool, glad its not me :wink:

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.