Logic Help | physical light switch as a light group

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

@armedad after a long debug session and some modification for your code i think we have a winner, documenting here what i noticed for anyone who might see this thread and have similar issues.
TLDR - probably due to usage of Change device type of a switch from switch to light.
All the switches above are configured VIA HACS LocalTuya integration which works flawlessly for me up until this automation flow, no delay or glitches. however while running the code above my light entities (switch converted to light by helper) == 4&7 become unavailable \ unknown which caused the various time delays, and due to the mode single configuration of the automation any new change would not trigger the automation.
I resolved it by removing the helpers and static the switch entities instead.
FYI:

  1. Your description is correct.
  2. The delay was in the light switch LED status but also caused the next automation not to run which was my bigger issue.
  3. knowledge is power, i am a big advocate in having all the information as well - THANK YOU!!!

Adding the current code only running 1± day, so too soon to state the stability:

alias: Sync Switch To Light Group
description: ""
trigger:
  - platform: state
    entity_id:
      - light.all_kitchen_lights
      - switch.all_kitchen_lights
    from:
      - "on"
      - "off"
    to:
      - "on"
      - "off"
condition:
  - condition: template
    value_template: >-
      {{ states.light.all_kitchen_lights.state !=
      states.switch.all_kitchen_lights.state }}
    enabled: true
action:
  - variables:
      trigger_entity: |-
        {%- if trigger.from_state.entity_id  == 'light.all_kitchen_lights' %}
          switch.all_kitchen_lights
        {% elif trigger.from_state.entity_id  == 'switch.all_kitchen_lights' %}
          light.all_kitchen_lights
        {% endif %}
  - service: homeassistant.turn_{{ trigger.to_state.state }}
    target:
      entity_id: "{{ trigger_entity }}"
    enabled: true
mode: single