Thank you for sharing. Your solution works well if turn on/off the light of switch slowly. I just tried to turn on or off the touch switch quickly, both of the switches will repeat to be On & Off forever.
I’m looking at doing this exact thing (linking the state of two switches together). My electrician wired two separate light circuits when I’d really rather just have one… so it should be easy enough to have two smart switches in sync so either switch turns on both switches, and either switch turns off both switches.
Unfortunately, I am having trouble with an infinite loop as was mentioned above… if I accidentally hit both switches at the same time and turn one on and one off then they just keep flipping each other back and forth… did anyone find a way to prevent this? I will try the delay mentioned above but it seems dubious that simply adding a delay would prevent the infinite looping… it seems like it would just cause them to turn on and off less quickly. What’s the logic behind adding the delay? Is there a more foolproof way to do it?
I have the same “infinite loop” issue with the provided answer above.
I “know” how the issue can be resolved (in terms of logic flow) but I don’t know how to write this in automation rule…
Maybe someone can turn this into a working rule:
So we keep the main logic by setting a mirror switch value to the “currently set” value. But we must set a flag on the mirror switch which will mean that the next assigned value will be from an automation rule (so we can ignore this).
Step by step:
switch A gets clicked (off -> on)
switch B sets custom “from_auto” flag to “1”
switch B sets state to “on” (same as switch A)
[mirroring is “done” at this point. everything below is what causes the loop to happen when states were changed too quickly (when not using “from_auto”)]
automation is fired for switch B as it’s state was changed (off -> on)
automation condition must check if switch B has a “from_auto” attribute set to “1”
[automation] IF no, then do switch.turn_on as usual mirroring flow…
[automation] IF yes, then do not try to “mirror” this state (do no run switch.turn_on for mirroring to another switches)
[automation] change switch B (current trigger.from_state.entity_id) attribute “from_auto” to “0” to return to the initial state
thats all… from_auto will control if the new state must be sent to “other” switches or not.
I hope someone can turn this into an automation rule.
I have a “virtual” bedside light that is actually a switch (wifi wall plug) and can also be switched via a 433MHz RF switch (connected to the Sonoff RF bridge). So it’s three things to sync for me: The light, the switch, and the state of the RF switch.
I use a more “brute-force” method by setting all three of the entities to whatever state I get when any changes:
# Synchronize switches for bedside light
- alias: Sync bedside light switches
trigger:
platform: state
entity_id:
- light.bedside # the light (which is actually switch3)
- switch.switch3 # the physical switch
- switch.rfbridge1_2 # RF switch
action:
service_template: >
{% if trigger.to_state.state == "on" %}
homeassistant.turn_on
{% elif trigger.to_state.state == "off" %}
homeassistant.turn_off
{% endif %}
entity_id:
- light.bedside # the light (which is actually switch3)
- switch.switch3 # the physical switch
- switch.rfbridge1_2 # RF switch
This has been working without side effects for over a year now, and proved a good strategy for any entity that is to be controlled by more than one actuator (like my hallway lights that have 5 switches, one next to each door).
Hi @Delphir, not sure if you have solve it. I understand what you mean by infinite loop. This happens when you flick the switch multiple time too fast, it can happens also when the network is slow and switch2 delay in action causing the out of sync. So they trigger each other indefinitely.
I manage to find something that work though, see if it helps you.
I am more primitive, i use only GUI automation. If you use code, you can use this concept and use your own code if you like.
I use 4automations entries… on1trigger_on2, off1trigger_off2, on2trigger_on1, off2trigger_off1, the important thing is to add a condition to the action switch, make sure the action switch is in the correct state.
example for the 1st automation
trigger : switch1 on
condition: switch2 is off
action: turn on switch2.
You try this, it will not go into infinite loop… Convert this concept into code, as i only know how to use GUI…let me know if it works for you…
the loop happens not because of the wrong states, but because of the delay itself
look at this:
switch2 current state = off
switch1 on (trigger delayed [A])
switch1 off (trigger delayed [B]) <<<<<<<< our expected final value
trigger automation for switch1 on [A] = switch2 on (will fire an automation too [C])
trigger automation for switch1 off [B] = switch2 off (will fire an automation too)
trigger automation for switch2 on [C] !!! = switch1 on <<<<<<<<<< the actual final value and looping then
…
and so on… here they start looping fast for the infinite amount of time. and each time the state of the another switch has an “opposite” value, so your “condition” will not solve the problem.
it would be handy to have a condition “NOT from automation” - this would solve the whole issue
Delphir, like i said in my post, i know the root cause is the delay, please check. It is the delay causing the 2switches out of sync, and always in opposite state, and they start triggering each other to change state without first checking the opp switch for their state. I know it is not the condition that cause this problem. I am saying by adding a condition to check the opp switch before firing the action, it does prevent the infinite loop. I am not just giving a hypothetical theory, i tried it in my automation. It prevent them from infinite toggling…I physicaly flick the switch onoff more than 10time in succession, it doesnt happen anymore… so i place my sucessful trial here and hopefully someone need this piece of information. I am not sure if you have tried it before you conclude. peace…
By the way i am not good at code, i use 4automations to pair 2 switches. And I also have infinite loop issue like you, causing by network delay. Now I am all settled after i add in the condition. hope it help…
Sorry, maybe i didnt explain properly, i try again just in case you didnt understand…
switch2 current state = off
switch1 current state = off
switch1 trigger on but check condition of switch2 first.
if condition of switch2, is off, then fire the action to turn on switch2
My solution uses two automations. Both switches are MQTT with physical switches. I’ve tried flipping the physical switches as fast as I can and I never get out of sync or have blinking lights.
My other automation is the same as this, but with the switch names reversed.
- alias: "Family North Sync" # Synchronize north and south lights
trigger:
- platform: state
entity_id: switch.family_recessed_north
action:
- choose:
- conditions:
- condition: state
entity_id: switch.family_recessed_north
state: 'on'
sequence:
- service: switch.turn_on
entity_id: switch.family_recessed_south
default:
- service: switch.turn_off
entity_id: switch.family_recessed_south
Hi I am new to HA. Can you tell me where I should put this code? automation.yaml or configuration.ymal? How is the automation id get generated? Many thanks,