Hey all
Asking for this great community help as i got stack with this automation for too long .
The goal
I am tying to set a physical(hardware) light switch as an existing home assistant light group - meaning:
The status of the physical light switch will reflect the light group state (it is red when on, blue when off)
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
When the group changes the state it requires to sync the state of the switch without triggering the switch action == light group toggle
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
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_switchlight.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.
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
@code-in-progress looks like i got excited too soon and the logic was working because my old automation was mistakenly still enabled
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
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?
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 )
The switch entity was converted to light entity with the name light.all_lights_switch - see the original automation
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?
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.
@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
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
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.
@armedad sorry for the delay, work got in the way.
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.
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.
@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!!! but need a bit finetuning ā the issues i see with your code are:
big delay in light switch status (5+ sec) did not happen with old logic so probably due to automation logic.
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
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