Synchronize two "peer" switches: race conditions

Hi All,

I’ve started playing with HA for the last couple of weeks (amazing platform!) and eventually I got to a point where I couldn’t find any solution online.

I have two switches (Xiaomi wall switches, Zigbee) that I want to synchronize (they should copy each other). The reason for that is that they both switch on the lights in the corridor and since Xiaomi doesn’t produce 3 way switches, I had to put them in parallel and sync their status via software.

Unfortunately my code is causing race conditions, so whenever the automation is triggered, the switches start going on and off forever.

- alias: Sync Corridor Downstairs Switches
  trigger:
    - platform: state
      entity_id: switch.wall_switch_left_158d00014dd9e1
    - platform: state
      entity_id: switch.wall_switch_left_158d00014dd40d
  condition:
    - condition: template
      value_template: "{{ states('switch.wall_switch_left_158d00014dd9e1') != states('switch.wall_switch_left_158d00014dd40d') }}"
  action:
   - service_template: switch.turn_{{trigger.to_state.state}}
     entity_id: switch.wall_switch_left_158d00014dd9e1
   - service_template: switch.turn_{{trigger.to_state.state}}
     entity_id: switch.wall_switch_left_158d00014dd40d

I read about copy switch, but if I understand correctly it works only in a master-slave paradigm, in my case I want that, whatever is the switch I trigger, the other follows.

I have also tried to create two automations (one per switch, to avoid to set again the switch that I triggered and is acting as “master”) but it didn’t work.

Is there any smart construct that I ignore? I was considering to add a sleep but it is a very dirty solution.
The condition != wasn’t enough to prevent races.

Thanks!

Hi Rex,

have you found any solution to this problem?

Cheers,
Jacek

no Jacek. Eventually I’ve decided to use the Xiaomi “automations” in order to support 3 ways switches (and focus my energies on other issues :)).