Turn lights off if light group is 'on'

Hi guys,

I use a Aqara switch to turn off the lights. Now I have a automation, which toggles a light group. The issue is, that sometimes not all lights turn off (or someone turns off one light manually).

Now I have something like:

  • Light 1: on
  • Light 2: on
  • Light 3: off

When I now press the button, the automation toggles the lights and I have the following:

  • Light 1: off
  • Light 2: off
  • Light 3: on

So I never get out of this situation with only the button. I need to toggle the wrong light, so the automation works correctly again.

I now noticed, that the group, which contains all the lights is on when one of the lights is on.

So now I want to create a script, that works as following:

if (group == 'on') {
   group.turn_off
} else {
   group.turn_on
}

What is the best way to do that?

Thanks!

action:
  service_template: >
    {% if is_state('group.YOUR_GROUP' , 'on') %} homeassistant.turn_off
    {% else %} homeassistant.turn_on {% endif %}
  entity_id: group.YOUR_GROUP
1 Like