How to toggle a group of switches as a whole?

After a reorganization of lightening, all of the Sonoff Basic I had are now defined as switches (they used to be defined as lights).

This brought it the issue of a group being toggled by going though all entries separately and toggling their state. This means that if I have switches A B and C, and A and B are off of but C is on, toggling the group will yield A-on, B-on, C-off.

When searching for solutions, I found this brilliant identical question from the past (How to access a card toggle as an entity?) - it happens to be mine :). The answer was "create a light group). But I have switches.

Toggle service call toggles individual devices in group also addresses this and the answer is “this is by design”. I wanted to have a confirmation before jumping into coding a “toggle for a group of switches” (that would check if any of the switches are on and then go though the list of entities) - this is quite surprising because the switch does exist from Loveralce (so I do not understand why not porting this to services, as the logic already exists)

You could convert your switches to lights with the light switch integration.

I had exactly that but reverted back to switches as switches because the light switch integration disables the possibility to manage devices via the frontend

What do you mean it disables possibility to manage it in the front end?? I can control them just fine.

Manage in the sense of individual entities. You cannot change their names, etc.

It is always possible to do that through yaml but then you end up with some devices/entities managed via integrations, and some via files.

Since there seems not to be a solution, I used the following code in AppDaemon. It works for me, YMMV.

def toggle_group(self, group):
        state = self.hass.get_state(entity_id=group, attribute='all')
        operation = self.hass.turn_on if state['state'] == 'off' else self.hass.turn_off
        for entity in state['attributes']['entity_id']:
            operation(entity)

I call it when assessing the entities to toggle:

if target.startswith("group."):
    self.toggle_group(target)

Glad that you got something that worsk for you. You could have used the solution from the link you posted as well without involving AppDaemon.

You mean Toggle service call toggles individual devices in group?

If so, this requires I believe a static definition for each group (I do not know automations in HA, I use AppDaemon). I have quite a lot of dynamism in my automations and keeping track of one entry per group would quickly be unmaintainable.

All this if i correctly understood the solution of course (if not I would be glad to learn how to make it a template for all groups of switches)

What do you mean with this? That you can’t add/remove entities to the group and the automation will take it into consideration? The template from petro doesn’t care about the members of the group, it loops through all entities of the group.

Sorry I was not clear.

I have about 20 groups so each of them would require a specific entry such as the one in the . Each time I add (or remove) a group I need to update that list.

Now, I have plenty of (wall) switches that send a message which is the interpreted to switch on, off or toggle an entity (single entity, list of entities or group). So the trigger section of the automation gets complicated as well.

In AppDaemon I have a YAML file that defines which actions are expected for each (wall) switch

devices:
    # Xiaomi
    salon_interrupteur_canape:
      single:
        - group.salon_avant
      double:
        - group.salon,off
    salon_interrupteur_mur: # main salon wall
      single:
        - group.salon
      double:
        - group.michael,off
        - group.martin,off
        - group.parents,off
      hold:
        - group.salon_avant
(...)

and each time they send a message as part of the event attributes (single for instance), the relevant function gets called. Before, when my switch was mapped to light via the switch platform I could just toggle and it worked. Now I have to use a function that analyzes the current state.

Still, this is just one function, common to all groups.