Coding problem "all lights in a room on/off"

I have a button installed in my dashboard to switch on/off all 3 lights in my living room.
The button is connected to a boolean helper.
Then I wrote a code / automation:
Tap on button:
if no lights on = switch all lights on
if 1 or more lights are switched on → switch all lights off

This works pretty well. This is my code:

alias: Wohnzimmer alles ein/aus
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.wohnzimmer_alles_ein_aus
condition: []
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: device
                type: is_on
                device_id: ab847b7665a907fa0b71097e0553e4f0
                entity_id: 01f0e6f78b449e0268bb1636d9c5a8ab
                domain: switch
              - condition: device
                type: is_on
                device_id: 26de39eb504f6428444b743d42a3bdb8
                entity_id: 156403216d8e647aebd46192400b76c1
                domain: light
              - condition: device
                type: is_on
                device_id: 7a48fb8074706d3bad472831746dead5
                entity_id: c3c99bfef0a275b78ca374b98ecb2c50
                domain: light
        sequence:
          - type: turn_off
            device_id: ab847b7665a907fa0b71097e0553e4f0
            entity_id: 01f0e6f78b449e0268bb1636d9c5a8ab
            domain: switch
          - type: turn_off
            device_id: 26de39eb504f6428444b743d42a3bdb8
            entity_id: 156403216d8e647aebd46192400b76c1
            domain: light
          - type: turn_off
            device_id: 7a48fb8074706d3bad472831746dead5
            entity_id: c3c99bfef0a275b78ca374b98ecb2c50
            domain: light
    default:
      - type: turn_on
        device_id: ab847b7665a907fa0b71097e0553e4f0
        entity_id: 01f0e6f78b449e0268bb1636d9c5a8ab
        domain: switch
      - type: turn_on
        device_id: 26de39eb504f6428444b743d42a3bdb8
        entity_id: 156403216d8e647aebd46192400b76c1
        domain: light
      - type: turn_on
        device_id: 7a48fb8074706d3bad472831746dead5
        entity_id: c3c99bfef0a275b78ca374b98ecb2c50
        domain: light
mode: single

But now I have the problem, that the button does not display correctly if the lights are on or off.

So I created another automation to check if all lights are off → boolean = 0. Works.

But now I want to display the button as “on” if just one or more lights are turned on. But this actually triggers the first automation to switch on ALL lights.

Is there an easier way to display the button as “on” even if there is just f.e. ONE light turned on?

Yes. Put the lights in a group - in the helpers section.
If no lights are on - the group is off. If one light or more is on, the group is on.

1 Like

That sounds awesome. Now I have the problem, that I need to decide for a group like “light” or “switch” or “sensor”.

Actually I need a group where I can put in lights AND switches, because one light is connected to a plug (=switch).

Then you want the helper “switch as x” which lets you change a switch to a light.