Automation: how to check if alight is on before to run the automation

hello,

sorry for the most likely simple/silly question… but as beginner I need to start somewhere :stuck_out_tongue: no?

I have set an automation, that with a Xiaomi Zigebee switch, if I click it once, I will toggle a group of lights…

as of not, I have listed all 4 lights in the automation (should I somehow group them?) and I have the challenge that if one of them is already on, when I clink the button, I will turn on the other 3, and off the one already on…

the first thing my automation should do is: check if any of the light is on (or more that one is no, e.g. 3 out of 4) and if anything is already on (BUT not all of them!) turn all of them on… how do I do this?

below my current automation

alias: Ambient Light in Livingroom
description: ''
trigger:
  - platform: device
    domain: mqtt
    device_id: 00071fab1dc818c6a71bc5ac931ffc6f
    type: action
    subtype: single
    discovery_id: 0x00158d0005484270 action_single
condition: []
action:
  - type: toggle
    device_id: 5dbd80e13444fa17c3579c6ef749a2eb
    entity_id: switch.light_1
    domain: switch
  - type: toggle
    device_id: a5393818e2fe4fbfe43b075576bc4700
    entity_id: light.light_2
    domain: light
  - type: toggle
    device_id: 05b575f72df2faf7c360c5f0d4a84ca5
    entity_id: light.light_3
    domain: light
mode: single

Thank you so much

Putting these in a group wouldn’t help in this aspect as the group would be on in any light was on and off otherwise.
The “problem” is that “toggle” will turn a switch/light on when it’s off and off when it’s on. What you need instead is “turn_on” and “turn_off” actions.
But then you have to decide whether you’re turning lights on or off. There needs to be a single condition that would decide that. You could use a “helper” input_boolean that you would toggle on/off and then automate one set of actions for on and another for off. That would look like this:

action:
    - service: switch.turn_on
      target:
        entity_id: switch.light_1
    - service: light.turn_on
      target:
        entity_id: light.light_2
        entity_id: light.light_3

or the equivalent turn_off services. I believe you don’t need both device_id and entity_id as both refer to the same entity.

1 Like