Only trigger group members that need to be triggered

Hello,

I have noticed a weird problem pertaining to the group:

let’s say I have 5 lights in a group and only one of them is turned on. If I turn off the group, it appears that turn off command gets sent to all five lights in the group, not just the one lone light that is turned off. Since my lights are z-wave, which is not really known for its speed, this causes significant slowdowns in automations that rely on groups.

Is there a way to change this behavior? I only want commands to be sent to the group entities that need changing.

I have exactly the same issue…

For example I have a few air conditioners in a group, only one is turned on (I hid the ones which are turned off):
image

As soon as I toggle the group switch that one turns off but the others turn on.

With the lights I face exactly the same issue. They’re all RF controlled via a Broadlink, because it sends the command to like 5-6 lights they turn off in sequence so it adds quite a lot of a delay. I have a few Broadlink IR remotes and I have load balanced them but there’s still quite a lot of delay, at least 5s in the worst cases.

My only solution is to use scripts like the ones below or use a template switch:

      bedroomairconditioner:
        value_template: "{{ is_state('binary_sensor.door_window_sensor_158d00028ed109', 'on') }}"
        turn_on:
          - service: broadlink.send
            data_template:
              packet: >
                {% if not is_state('binary_sensor.door_window_sensor_158d00028ed109', 'on') %}
                  "JgCWAAABQgABQQABQQABQZdSCw0MHwohCw0LIAsOCg0NCwsNDh0LDQsODB4LDA8LCiELDQ0LCwwPCwoOCg4KDgoOCw0KDg0LCyALDQsNDQsMDQwMCg4KDgoOCiALDQ4LCg4LDQoODAwLDQ0eCw4LDQoOCiALDgogCw4KDg0eCw0LDQsKEB4LIAsgCyALDgoOCg4KAAKcmAANBQAA="
                {% endif %}
              host: 192.168.0.131
        turn_off:
          - service: broadlink.send
            data_template:
              packet: >
                {% if not is_state('binary_sensor.door_window_sensor_158d00028ed109', 'off') %}
                  "JgCWAAABQgABQQABQQABQZdSCw0MHwohCw0LIAsOCg0NCwsNDh0LDQsODB4LDA8LCiELDQ0LCwwPCwoOCg4KDgoOCw0KDg0LCyALDQsNDQsMDQwMCg4KDgoOCiALDQ4LCg4LDQoODAwLDQ0eCw4LDQoOCiALDgogCw4KDg0eCw0LDQsKEB4LIAsgCyALDgoOCg4KAAKcmAANBQAA="
                {% endif %}
              host: 192.168.0.131
  aclroff: 
        sequence:
          - condition: state
            entity_id: switch.livingroomairconditioner
            state: 'on'
          - service: switch.turn_off
            entity_id: switch.livingroomairconditioner
            
  acbroff: 
        sequence:
          - condition: state
            entity_id: switch.bedroomairconditioner
            state: 'on'
          - service: switch.turn_off
            entity_id: switch.bedroomairconditioner

Be aware that you NEED to be able to check the state of the switch otherwise it cannot be done. In my case for my lights I am screwed, but I can do it for my AC.

Here’s just some templates I’m using in my automations for similar situations. For inspiration.

This value template counts the number of lights in a group that are turned on (or off if you change it just a tiny bit). You can use this in a condition of an automation:

      value_template: >
        {{ states | selectattr('entity_id', 'in', state_attr('group.colortemplights', 'entity_id')) | selectattr('state', 'equalto', 'on') | map(attribute='entity_id')|list|count > 0}}

In the action part of the automation, you can create a comma separated list of just the lights you want to trigger:

    - service: light.turn_on
      data_template:
        entity_id: "{{ states | selectattr('entity_id', 'in', state_attr('group.colortemplights', 'entity_id')) | selectattr('state', 'equalto', 'on') | map(attribute='entity_id') | join(', ')}}"  # comma separated list of entities that are currently turned on
        color_temp: 300
        transition: 3

I arrived at these templates because the Tradfri hub is quite prone to crashing when I feed it too many calls in a short time period. So trying to prevent any unnecessary call.

7 Likes

That’s a great template @Emphyrio. Thanks.

As of version 0.96, you can use the expand function to streamline your template:

    - service: light.turn_on
      data_template:
        entity_id: "{{expand('group.colortemplights') | selectattr('state', 'equalto', 'on') | map(attribute='entity_id') | join(', ')}}"
        color_temp: 300
        transition: 3

I recall suggesting it to someone who wanted to dim lights that are on:

Uh-oh.

What happens if no entities are selected by the template?

Doesn’t home assistant apply the service to all the entities (and generate a warning that the value “all” should be used instead of not specifying an entity_id?

This would have the complete opposite effect of what was intended.

Then it will, as you’ve indicated, produce an empty string for entity_id thereby causing the automation to act on all lights (along with a warning message indicating a blank entity_id has been deprecated in favor of using all).

One hack would be to prepend a non-existent light entity so that if there are no lights on within the group, the template will return a minimum of one entity. Home Assistant will not (currently) complain about non-existent entities.

{{'light.none, ' ~ (expand('group.whatever') | selectattr('state', 'equalto', 'on') | map(attribute='entity_id') | join(', ')) }}
1 Like

It is why I added the condition in the automation, at least one light in the group has to be on for the action to trigger.

1 Like

This might be a potential solution to my isse (Control to adjust the brightness only of lights that are already on).

Is it possible to attach dynamic entities like this to a light card - i.e. something with a slider control?