Toggle service call toggles individual devices in group

As the topic title says, when using the homeassistant.toggle (or switch.toggle) service call to operate a group as a whole, it doesn’t actually use the group’s status but the individual items within.

For example, I’ve got all my bedroom lights (2 lights and one switch) in a group called group.bedroomlights to have them all switch on and off simultaneously whether using HA interface or Alexa. I’ve also got a Xiaomi wireless switch next to my bed which I use to control the lights.

I’ve got the following automation:

- alias: "Bedroom Light Switch"
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001209892
      click_type: single
  action:
    service: homeassistant.toggle
    entity_id: group.bedroomlights

Now this works quite well most of the time, but for example when I switch one of the lights in the group off manually (while the group is on), then when I use the wireless button, HA doesn’t use the state of the group to toggle the devices on or off, but instead it actually toggles the states of each device individually, so the on devices turn off, and the off devices turn on.

Am I doing something wrong? Or is this an oversight in the programming?

That’s by design.

If you want the behavior you are looking for, it can be quite complicated.

- alias: "Bedroom Light Switch"
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001209892
      click_type: single
  action:
    - service_template: >
        {% if is_state('group.bedroomlights','off') %}
          homeassistant.turn_on
        {% else %}
          homeassistant.turn_off
        {% endif %}
    - data_template:
        entity_id: "{{ state_attr('group.bedroomlights','entity_id') | join(', ') }}"
3 Likes

Thanks for that, I wasn’t aware that it was designed that way, I just assumed that it would control the group as a whole instead of individual items within it.

I guess using a service template is all I need to change to make it work like I want to, since the entitiy_id is always the same (group.bedroomlights).

Yes, that will always work if you are always toggling. If you try to make a switch template though, you’ll want to specify the entities inside the group because turning a group on will do nothing if the group is already on. That’s why I typically do the ‘entity’s in the group’.

Ahhhh, I see what you mean, very good point!

Thankfully I don’t need it to go into that much detail, but will give it a shot.

Hi all
Would it be possable to do the same with sonoff switches/entities
I have two sonoff wall switches. One at the top of the stairs and one at the bottom. Is it possible to link these two together As it stands if I turn one on then walk upstairs and try to turn the light of it does not work as the switch downstairs has control and visa-versa.

So is it possible to link them all the time or group them so that activating one will always activate the other.