How to link 2 switches together, so state of one mirrors state of second

Hello!
Despite the title, my ultimate goal is to connect a sonoff switch set on Home Assistant with virtual switch on Samsung Smartthings. My idea is following (if you have idea to do it better, I;d love to hear!). To do this, I’ve managed to:

  • Setup sonoff as mqtt switch1 on home assistant.
  • Setup smartthings virtual switch as mqtt switch2 on home assistant with mqtt bridge.

The only thing left is to link switch1 with switch2, so that always when switch1 is turned on, then switch2 is turned on. The other way around as well. My question - how to do it?

here is the config for 2 switches:

	- platform: mqtt
		entity_id: "light_living_01"
		name: "Living Room main light 1"
		command_topic: "cmnd/light-l1/power"
		state_topic: "stat/light-l1/POWER"
		qos: 1
		payload_on: "ON"
		payload_off: "OFF"
		
	  - platform: mqtt
		entity_id: "smartthings_living_01"
		name: "Smartthings Living Room main light 01"
		command_topic: "smartthings/Living room main 1/switch"
		state_topic: "smartthings/Living room main 1/switch/stat"
		qos: 1
		payload_on: "on"
		payload_off: "off"
		retain: true

Can you please format your code according to the instructions at the top of the page.

Here is how I keep two Sonoff plugs in sync with an automation:

- alias: XMas Lights Mirror
  trigger:
    - platform: state
      entity_id: switch.sonoff_s31_01, switch.sonoff_s31_02
      from: 'on'
      to: 'off'
    - platform: state
      entity_id: switch.sonoff_s31_01, switch.sonoff_s31_02
      from: 'off'
      to: 'on'
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      switch.turn_on
      {% elif trigger.to_state.state == "off" %}
      switch.turn_off
      {% endif %}
    data_template: 
      entity_id: >
        {% if trigger.from_state.entity_id == "switch.sonoff_s31_01" %}
        switch.sonoff_s31_02
        {% elif trigger.from_state.entity_id == "switch.sonoff_s31_02" %}
        switch.sonoff_s31_01
        {% endif %}
7 Likes

Works perfectly, thank you!

Well actually there is a problem in this solution. When I quickly turn on and off one light (within half a second), then two light go into feedback loop, when when one changes before receiving info, that the other changed to the opposite one. That causes lights to flash on and off repeatedly. Can this be handled in any way? IS there a way to detect if light changed because of the automation?

Why would you have the need to do this in the first place?

I got shot down for suggesting this before, but isn’t this a perfect application for Rules?
The following rule is on my deck light (a Sonoff Basic), and when the deck light is on, so is the garden light.

rule1 on Power1#state=1 do publish cmnd/Garden/POWER on endon on Power1#state=0 do publish cmnd/Garden/POWER off endon
rule1 1
1 Like

This leads to the purr-fact opportunity to refer to one of my favorite English proverbs.

Sometimes it just happens, when you f.e. walk into the room, turn on the light with a switch but then realise you want to have the other light on instead. So you turn on the light and then off almost immediately.

1 Like

Understood - I tried to do that through the web interface and it was basically impossible to replicate.
Tried with the switch/button on the HW itself (was a little easier, still, the timing had to be exactly right) and I got to the same point as you did - it was a nice alternate blinking effect for my XMas lights, though :grinning:

Only way to get rid of that I can think of would be to put a stats sensor on one of the switches and if it counts, e.g. 4 changes in 5 seconds, it turns the syncing automation off for 3 seconds and then back on again (or stops it for a very short time in another way).

FYI.

I added small delay to fix this issue.

Hi Jerry, I was trying to put that delay on my own and didn’t work for me, could you please post it? Thanks a lot

Just add

    - delay: 00:00:01

as last action.

Guy’s throw away your guns, this is a perfect solution! Thanx!!! :slightly_smiling_face:

This solution is not perfect for me. I use it for a long time and am not sure anymore if it ever was like this, but when I switch the light, it will not come back to it’s previous brightness. I have it linked to a wall switch.
It doesn’t matter if I control using philips Hue, the light, or the switch, it will always start at the lowest brightness whenever this automation is active.

Anyone knows how to solve?

Alright,

Now it works… I have removed the transition: 0 and it seems to be okay. Adding delay does not work for me at all.

1 Like
 I have removed the transition: 0 and it seems to be okay. Adding delay does not work for me at all.

Can you suggest the detail code here?

Thank you very much.

This is generated using the UI

- id: wcd_3_light
  alias: 'WCD 3: Bank stalamp'
  hide_entity: false
  trigger:
  - platform: state
    entity_id: light.bank_stalamp
  - platform: state
    entity_id: light.wand_3
  action:
  - data_template:
      entity_id: '{% if trigger.from_state.entity_id == "light.wand_3" %} light.bank_stalamp
        {% elif trigger.from_state.entity_id == "light.bank_stalamp" %} light.wand_3 {%
        endif %}

        '
    service_template: '{% if trigger.to_state.state == "on" %} light.turn_on {% elif
      trigger.to_state.state == "off" %} light.turn_off {% endif %}

      '
2 Likes

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.

Here that’s not the case. The animation of the Hue lights already takes a few seconds, which might act as a delay.

1 Like

thank you for posting this… could you point me in the right direction to replicate automation using 3 switches??

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?