Automation for switch toggle, ensure all lights are same state

I have several rooms with groups of 4x lifx bulbs wired to constant power controlled by the input sensor on shellies hooked up to my light switches. I use the MQTT lifx bulb toggle command for my automation but every now and then one bulb might not register the state change. The natural instinct is to flip the switch again but then the other bulbs toggle and then they are stuck out of phase. Is there any automation logic i could add that would ensure all four bulbs in the group are the same state at the end of the automation? I don’t know of any way to ask the bulb what state it is in via MQTT.

shoot, thought this would be an easy one for you guys

Can you share your automation? Then we can dig into the logic you already have and what you can add to achieve the desired result. I assume you have to use MQTT (I’m not real familiar with the LIFX bulbs). Looking at the docs, there is a set_state service that might be useful: LIFX - Home Assistant (home-assistant.io)

Possibly after toggling, you can use the set_state service to make sure all the bulbs are in the proper state.

- id: '1559180528790'
  alias: Kitchen Light Switch
  trigger:
  - entity_id: switch.kitchen_lights
    platform: state
    to: 'on'
  - entity_id: switch.kitchen_lights
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      entity_id: group.klights
    service: light.toggle

Set state could work, but I need a way to query the bulbs to see what state they are currently in. The other option would be to abandon the toggle logic and just use set on vs set off. However I have read that this can be problematic most people recommend the toggle.

This looks pretty simple from an automation standpoint. When you look at the lights in developer/states, or there attributes that show the brightness? For example, here’s what one of my Hue light looks like:


Then I can extract the brightness attribute in a template by (in theis example I’m turning on light.hue_color_lamp_1 with a brightness that matches light.hue_color_lamp_2):

  action:
  - service: light.turn_on
    data:
      entity_id: light.hue_color_lamp_1
      brightness: >
        {{state_attr('light.hue_color_lamp_2') }}

You’re using an MQTT broker, so this may not work through that… But this is the typical method to extract the brightness attribute from a light that is on

What kind of switch do you use? That may be my ignorance speaking if it’s something built into the LIFX lights…

Thanks Kirby, I checked my lifx bulbs and they look very similar. So rather than getting the brightness state I should just be able to check the state to see if its “ON” right? Or is there a reason you are checking brightness rather than just the state?

You can absolutely check just the state! And looking at it, I realize I forgot to include the brightness attribute in the state_attr statement. That should be:

{{state_attr('light.hue_color_lamp_2','brightness') }}

You can check or set the other attributes in the same way - just use the exact name that you find in the developer/states panel.

To check if a light is on, you can do something like this (this basically slaves light.hue_color_lamp_1 to light.hue_color_lamp_2):

  - service: >
      {% if is_state('light.hue_color_lamp_2','on') light.turn_off
      {% else %}                                    light.turn_on
      {% endif %}
    data:
      entity_id: light.hue_color_lamp_1
      brightness: >
        {{state_attr('light.hue_color_lamp_2','brightness') }}

I may be a little off on what you’re hoping to accomplish, so let me know. Good luck!

Digging up that old thread because I have come up with a solution for something similar.

I have this wireless Aqara switch toggling 2 bulbs : TV light and sofa light.
If for some reason, either one is On (and the other Off), a single press to toggle doesn’t cut it because you can’t switch them on or off both :crazy_face:

Here is my improved automation to address this issue :
I first check if either light is On : if so, I turn it off.
The next single press can then start fresh and do a regular toggle of both lights.

alias: Aqara H1 Toggle TV & Sofa lights (advanced)
description: Turn off both if either one is off else toggle
trigger:
  - platform: event
    event_type: deconz_event
    event_data:
      id: aqara_switch_living_room
      unique_id: 54:ef:44:10:00:5b:6f:c3
      event: 2002
      device_id: 27a83c2f0c73aeda8ba26ef3cb6937a5
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.tv
            state: "on"
          - condition: state
            entity_id: light.sofa
            state: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.tv
      - conditions:
          - condition: state
            entity_id: light.tv
            state: "off"
          - condition: state
            entity_id: light.sofa
            state: "on"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.sofa
      - conditions: []
        sequence:
          - service: light.toggle
            data:
              brightness_pct: |
                {% if is_state('sun.sun', 'above_horizon') %}
                  40
                {% else %}
                  70
                {% endif %}
              kelvin: |
                {% if is_state('sun.sun', 'above_horizon') %}
                  3500
                {% else %}
                  2700
                {% endif %}
            target:
              entity_id:
                - light.tv
          - service: light.toggle
            data:
              brightness_pct: |
                {% if is_state('sun.sun', 'above_horizon') %}
                  40
                {% else %}
                  70
                {% endif %}
              kelvin: |
                {% if is_state('sun.sun', 'above_horizon') %}
                  3500
                {% else %}
                  2700
                {% endif %}
            target:
              entity_id:
                - light.sofa
mode: single

Yes, I could have probably used the double press or long press to achieve this but I wanted to keep it family-friendly.

1 Like

Thank you for this code! It has giving me a starting point to try and work on a similar automation.

I was trying to figure out how to control 2 lamps on smart switches using an Aqara button on each one. I plan on using single press to toggle the the lamp that the button is on, double press for the other lamp, and hold to toggle both on or off at the same time (but together). My issue is that I haven’t figured out what to do when the states don’t match. I have no experience in coding and I couldn’t figure out the syntax to check the conditions… but this gives me a direction to head in.

So thanks again!

I stumbled across that post that gives 2 smart approaches on how to deal with mixed-states bulbs

I need to try the Proportional toggle one! :exploding_head:
…
Done. Here is my working example with 3 lights :ok_hand:

description: Toggle sofa lights and TV light proportionally
trigger:
  - platform: event
    event_type: deconz_event
    event_data:
      id: aqara_switch_living_room
      unique_id: 54:ef:44:10:00:5b:6f:c3
      event: 2002
      device_id: 27a83c2f0c73aeda8ba26ef3cb6937a5
condition: []
action:
  - variables:
      count_on: >
        {{ expand(state_attr('light.sofa_tv', 'entity_id')) |
        selectattr('state','eq', 'on') | list | count }}
      count_off: >
        {{ expand(state_attr('light.sofa_tv', 'entity_id')) |
        selectattr('state','eq', 'off') | list | count }}
      light_action: "{{ iif( count_on >= count_off, 'off', 'on' ) }}"
  - service: light.turn_{{ light_action }}
    metadata: {}
    data: {}
    target:
      entity_id: light.sofa_tv
mode: single

Alternative method:

action:
  - service: >
      {% set lights = state_attr('light.sofa_tv', 'entity_id')|map('states')|list %}
      {% set count_on = lights|select('eq','on')|list|count %}
      {% set count_off = lights|select('eq','off')|list|count %}
      light.turn_{{ iif(count_on >= count_off,'on','off') }}
    target:
      entity_id: light.sofa_tv
1 Like