Logic Help | physical light switch as a light group

Hey all
Asking for this great community help as i got stack with this automation for too long :disappointed_relieved:.

The goal

I am tying to set a physical(hardware) light switch as an existing home assistant light group - meaning:

  1. The status of the physical light switch will reflect the light group state (it is red when on, blue when off)
  2. When pressing on the physical button it will act as if the light group was toggled (if any light is on it will close all lights, if all off it will turn all lights off)

The issues i have encountered

  1. When the group changes the state it requires to sync the state of the switch without triggering the switch action == light group toggle
  2. When the light group is off and a light in that group changes from state unknown to state on my logic triggered the light switch to toggle the light group == all lights turn on
  3. Current logic have a delay, and is not 100% stable or consistent

My configuration

  • In the entrance to my house i have 2 physical light switches that are not connected to any physical light.
  • I want one button to act as group light.all_kitchen_lights that consistent of light.light1 and light.light2
  • The second button to act as group light.all_lights_switch light.light1, light.light2, light.light3 and light.light4

This is the automation for the ā€œall lightsā€ and i have the same code for ā€œall kitchen lightsā€ as well, so far this is the closest i could get to my goal with the issues i described above.

  alias: All Lights Button
  description: ''
  trigger:
  - platform: state
    entity_id: light.all_lights_switch
    from:
    - 'on'
    - 'off'
    to:
    - 'on'
    - 'off'
    id: switch_state_change
  - platform: state
    entity_id: light.all_lights
    from: 'off'
    to: 'on'
    id: lights_on
  - platform: state
    entity_id: light.all_lights
    from: 'on'
    to: 'off'
    id: lights_off
  condition:
  - condition: template
    value_template: '{{ now() - state_attr(''automation.all_lights_button'', ''last_triggered'')
      > timedelta(seconds=1) }}

      '
    enabled: true
  action:
  - choose:
    - conditions:
      - condition: trigger
        id: switch_state_change
      sequence:
      - service: homeassistant.turn_{{ trigger.to_state.state }}
        target:
          entity_id: light.all_lights
    - conditions:
      - condition: trigger
        id: lights_on
      sequence:
      - service: homeassistant.turn_{{ trigger.to_state.state }}
        target:
          entity_id: light.all_lights_switch
        data: {}
    - conditions:
      - condition: trigger
        id: lights_off
      sequence:
      - service: homeassistant.turn_off
        target:
          entity_id: light.all_lights_switch
        data: {}
  mode: single

All you need is a template switch. Set your value template to be the light group and then write the turn_on/turn_off service calls.

1 Like

Edit - this did not achieved the defined goal
@code-in-progress Thank you very much!!! so simple and elegant.
In case others might see this thread & learn from my wrong assumption that this issue requires to actively change the button. this template is passively taking care of the physical switch state and the actions it triggers

- platform: template
  switches:
    kitchen_lights:
        value_template: "{{ is_state('light.all_kitchen_lights', 'on') }}"
        turn_on:
            service: switch.turn_on
            target:
                entity_id: switch.all_kitchen_lights
        turn_off:
            service: switch.turn_off
            target:
                entity_id: light.all_kitchen_lights
1 Like

@code-in-progress looks like i got excited too soon and the logic was working because my old automation was mistakenly still enabled :face_with_hand_over_mouth:

Unless i am missing something the switch templates creates new switch entity and choose what will happen if we turn on or off that virtual switch + only set the state to that virtual switch.
So in the code i showed above when light.all_kitchen_lights changes to on then the new virtual switch = switch.kitchen_lights changes to on but no service is triggered and the desired entity = switch.all_kitchen_lights does not change itā€™s state

Hmmmm, ok, so to make sure I understandā€¦ when the light group all_kitchen_lights gets turned on by any means, the switch all_kitchen_lights isnā€™t changing its status as well? Where is that switch defined? Is it a hardware switch or virtual?

Sorry for all the questions. Iā€™m mobile atm, so reading comprehension is slightly lower. lol

Questions are a good thing :muscle: & thank you for responding so quickly, ill try to explain myself better:

  • The switch is hardware switch (no wires connected to any light though)
  • The light group state (all_kitchen_lights in this example) should be synced to the hardware switch
  • The hardware switch physical button should behave the same as the dashboard button for this light group would == toggle the light group entity state.

a couple more questions. that may help @code-in-progress get all the info he needsā€¦

does the physical button have an on-off state of itā€™s own? (ie, if the light turns on via the dashboard, do you need to turn the phyical lightā€™s state to ā€œonā€)? or is that physical button just a press button with no state?

whatā€™s the entity name of that button? i donā€™t see it in all of the code above.

if itā€™s a physical toggle button with no state, iā€™m not clear on why the automation isnā€™t just triggering off the physical button event and calling toggle.

if it does have state, then perhaps a blueprint like this?

1 Like

@armedad hi and thanks.

  1. The physical button have on off state - this is a normal smart switch. (Without automations a press will toggle the state on the wall&home assistant entity )
  2. The switch entity was converted to light entity with the name light.all_lights_switch - see the original automation
  3. I did see and try this blueprint but sadly it does not apply to my case (2 groups that 1 include the others entities each group have a dedicated switch)

In thinking about this more, I think you just need to toggle the hardware switch from your automation AND add the hardware switch to your triggers. What Iā€™m not sure about is if that will create a loop when the lights change and toggle the switch. Iā€™m trying to think of an easy way to keep that from happening. Maybe two automations? One for the light group and one for the hardware switch?

:thinking: I need to think about this a little bit and see what I can come up with.

Those loop problems were getting me time after time when i started building this automation.
The code i uploaded is technically working as expected but have the issues i described.
Where the most problematic issue is when 1 light change from non off state (I.E. unknown) to on which cases all the lights in the house to turn on.

oh ok I think I get it now.

@code-in-progress I think the issue differs depending on what approach.

create 2 sets. one switch and all lights
One switch with lights 1&2.

Make 2 light groups. Do not sync lights directly. only sync the light group.

Use the blueprint to sync one switch to one light group. Other switch to the other group. So use the blueprint twiceā€¦

The key thing here is using the light group so that it doesnā€™t force all the lights to switch but to recognize that itā€™s considered ā€˜onā€™ already if one is on

hopefully I understood all of that right. If this doesnā€™t work please let us know what behavior is broken.

using the light group breaks the feedback cycle I think.

I tried again to use the updated ā€œsynchronize ā€¦ 2 entitiesā€ and what happens is - when turning - Light1 (part of kitchen_lights group) on then all the lights in the light group turns on. from traces it looks like the targets for turn on are each of the lights in the group + the light group itself

ah . that one expands the group. let me find one that does not. or if I canā€™t find one, itā€™s easy enough to write

ok, give this a try:

create 2 of these. one as is (please check that iā€™ve got all your entities right)
and one that you replace the all_lights and the all_lights_switch with the all_kitchen_lights and all_kitchen_lights_switch (or whatever you call it).
change the alias name too so that you donā€™t have a duplicate.

i wrote this free handā€¦ iā€™m not at home to actually test it, so apologies if there are minor nits, but i think this gets it.

someone will perhaps observe that my ā€œifā€ check is unnecessary, in that turning something on thatā€™s on is a noop. however iā€™m doing that just for precaution that i donā€™t want to turn on all your lights and in case, in the future, the behavior for a group changes.

alias: Test light groups sync
trigger:
  - platform: state
    entity_id:
      - light.all_lights
      - light.all_lights_switch
    to: "on"
    id: "on"
  - platform: state
    entity_id:
      - light.all_lights
      - light.all_lights_switch
    to: "off"
    id: "off"
condition: []
action:
  - service: homeassistant.turn_{{ trigger.id }}
    target:
      entity_id: light.all_lights_switch
  - if:
      - condition: template
        value_template: |
          {{ states('light.all_lights') != trigger.id }}
    then:
      - service: homeassistant.turn_{{ trigger.id }}
        target:
          entity_id: light.all_lights

@armedad sorry for the delay, work got in the way. :sweat_smile:

I did try the script on both lights in it does work but with a big delay of state sync, about 5+ seconds and had few glitches where the status did not change.

Adding my use case in case it will help to understand better:
I have a light switch in the entrance to my house witch is also the kitchen area.

In the kitchen - I have 3 separate lights 1 normal smart switch + 2 smart plug connected to a LED light strip - the group switch helps me to turn all of them ON or OFF, without canceling the option to turn just a single light + the ability to close all of them at one button, instead of hoping between 3 different button locations.
All lights button - let me know if i have any of the lights still turned ON before leaving the house and to turn all of them OFF if i did forget 1.

these switches and lights that you haveā€¦ how are they connected? they wouldnā€™t be zigbee would they?

also can you be extremely specific.

in the kitchen:
1 smart switch connected to light1? letā€™s call this switch1
2 smart plugs connected to light2 & light3?
1 kitchen light group - for my solution to work, this should NOT include light1. it must only include light2 & light3. - if you want a group that includes all 3, thatā€™s ok, but donā€™t use that for this automation. create a separate group with just light2 & light3 for this automation.

you didnā€™t say anything about the other light set. iā€™m guessing you have something like:
1 - another light switch (normal smart switch) thatā€™s connected to light4 thatā€™s not in the kitchen? letā€™s call this switch2.
1 or more additional lights that are not part of the kitchen?
1 light group that you want to represent all lightsā€¦ this set plus the kitchen? again, this light group should NOT include the kitchen normal smart switch1. NOR should it include normal smart switch2. this group should include all the rest of the lights.

is that the setup you have? thatā€™s the setup that my code is intended to work with.

1 Like

@armedad Thank you very much for the dedication & sorry - it looks like i am not clear with my explanation so ill try to explain in a different way.
Hope i was clear that your code WORKS!!! :tada: but need a bit finetuning ā†’ the issues i see with your code are:

  1. big delay in light switch status (5+ sec) did not happen with old logic so probably due to automation logic.
  2. some inconsistency of that logic once every few presses, like status does not change press of the button does not toggle the state of the groupā€¦ (maybe due to those delays?).

All devices connected VIA WiFi
switch number | alias | switch type.

Kitchen lights:

  • Switch1 (alias - sink LED) - Smart Plug
  • Switch2 (alias - stove LED) - Smart Plug
  • Switch3 (alias - Main Kitchen light) - Smart light switch
  • Switch4 (this should represent the all_kitchen_lights group) - Smart light switch

The rest of the house:

  • Switch5 (alias - office light) - Smart light bulb
  • Switch6 (alias - bedroom light) - Smart light bulb
  • Switch7 (this should represent all_lights group) - Smart light switch

Groups:

  1. All kitchen light group == all_kitchen_lights - configured by yaml as light entity group with switch1-3
  2. All lights group == all_lights - configured by yaml as light entity group with all_kitchen_lights + switch5-6

could you post the latest and complete code that you are using? you said it needed a few tweaks, so iā€™d like to see it complete and verbatim.
iā€™m confused a bit by the 5 second delay. iā€™m not sure whatā€™s causing thatā€¦ oddā€¦

let me repeat back in my own words to make sure i get it:

all 7 switches (including 4 & 7) are physical switches, but 4 and 7 physically connect to no lights. 4 should conceptually turn on/off switches 1-3. switch 7 is phyically connected to no lights. but conceptually you want it to turn on/off switches 1-3, 5 and 6.

you said thereā€™s a delay in switch status. does this mean that when you flip switch 4 or 7, the proper lights come on immediately, but the on/off status on switch 4 or 7 (a mini led status on the switch? or the status of switch 4/7 in home assistant) takes 5 seconds to turn on? or are you saying that it take 5 seconds for the lights (switches 1-3, 5,6[as appropriate]) to turn on?

sorry for being a bit pedantic. itā€™s a little atypical and i want to make sure i really understandā€¦ on the posiitive side, iā€™m glad at least the code works!! feels like weā€™re close :slight_smile:

1 Like