Synchronize the on/off state of 2 entities

Thanks for the blueprint.
I wanted to use the blueprint to sync the status of my media player with an input_boolean so that I could turn the TV on and off via google home.
Unfortunately, a media player has more states than just on and off (unavailable, playing, unknown) so that error messages keep popping up in the log.

1 Like

I have been messing with trying to make this work for two days and well dah meā€¦I should have come here first and looked for blueprintā€¦Thank you so much for this simple but yet effective blueprint!

Same here with 2021.11.1. Were you able to fix it? Tnx!

You are great!
I couldnā€™t get them to stop blinking!!!
With a few commands I integrated 3 switches that now work together!
Thank you so much

1 Like

Templates for syncing 2, 3 or 4 switches: [General] Sync switches

1 Like

Hello,
Im having an issue. Im not sure if its the blueprint or my setup. I have a Tuya 4 touch switch with the Tuya integration controlling 2 light groups (consisting of 2 bulbs each using Tuya integration) and 2 broadlinks controlling 2 A/Cs. The switches with the devices are out of sync. Seems that sometimes it gets out of sync when I use Alexa to control the devices instead of the switches, but not all the time. I can ask Alexa to turn the A/C on and the switch turns on, but not all the time. Also if Alexa turns off the light the switch can stay on and after some time the lights turn back on. Im guessing its syncing the lights to the switch and turns it on instead of turning the switch off. Any ideas? Thanks.

Hello,
I think I have figured out most of it except for the A/C part. My A/C has off and cool, not off and on. My switch has off and on. How can I translate cool to on for this to work?

Error message is:
Error while executing automation automation.synchronize_office_wall_switch_2_to_office_a_c: Unable to find service homeassistant.turn_cool

I canā€™t find that service in the documentation:

Thats because it doesnā€™t exist for what I know. I think the automation is looking for cool when it doesnā€™t exist because the A/C has off and cool as options. Is there a way to add for example, cool = on for the A/C entity on the automation? Thanks.

Well, you should be able to achieve that, but not with this blueprint I guess. You can manually modify the automation, or better, modify a copy without a reference to the blueprint.

Then you can call any service you like, for instance from the climate domain.

Hmm, Seems the lights unsynced again. Any idea why this is happening? Maybe a different solution? Thanks.

So I set this up to synchronize the lights at the top and bottom of my stairs. However, itā€™s not exactly what I want, as I only really have one switch I want to use and so I really want to unidirectionally ā€œcopyā€ state from one entity (a switch) to another (a receptacle). Also, my receptacle device is slow, and sometime it is slow enough (or maybe it just misses the state change) the old, incorrect state gets copied BACK and the light turns off! Itā€™s also overall slower than I would like. I switched to HA from ST to try and avoid cloud delays, but if anything itā€™s slower now. I often end up halfway down the stairs before the light at the top (connected to the ā€œcontrolledā€ receptacle) turns on.

I realize a blueprint to just ā€œcopyā€ a binary state is actually a pretty trivial (probably I should try to set it up directly as an automation) but it still would be pretty useful to have as a blueprint just to allow for a nicer setup UI.

If it matters both devices are Z-Wave (for now; the light at the top is an addressable LED strip, eventually to be turned into an ambient data display via MQTT probably, but for now I just want to control it by switching power on and off). Anyway, the future extension I have in mind is why I want to do this with an automation rather than Z-wave direct association etc.

Thank you for your solution. Iā€™ve tried this solution and if touching the switch so quick, they are still being flicked repeatedly.

Iā€™m guessing you added a condition to prevent flickering/loop. If you think about it, it can still go to an infinite loop if states are not updated in real time. Personally I found that the only way to truely prevent a loop is to add a 0.5-1 second delay to one switch/light triggering the other. So it goes like this: If light A is turned on/off, then immediately turn light B on/off. If light B is turned on/off FOR ONE SECOND, then turn light A on/off. Let me know your thoughts. In my use case the delay is not a big deal and preventing the loop really gives me peace of mind

1 second delay is too long to my liking. It also have the side effect of not registering if the switch was turned ON then OFF in less than 1s.
Iā€™ve been runing this for almost a year and never experienced flickering. I do agree that it could still happen in some edge cases.

One thing you may test is adding a condition on the context, to block state change if it was triggered by the automation itself. I tested it briefly and it seems to do the trick:

condition: template
value_template: >-
    {{ trigger.to_state.context.parent_id is none or 
      (trigger.to_state.context.id         != this.context.id and 
       trigger.to_state.context.parent_id  != this.context.id) }}

I tested this one extensively this morning, and worked very well for me.
I have versions of this with up to 5 switches and fast clicking did not resulted in flickering.

Please let me know if it works for you.

blueprint:
  name: Synchronize 2 States
  description: Bind two switches together to act in unison
  domain: automation
  input:
    switch_1:
      name: Switch 1
      selector:
        entity:
          domain: switch
    switch_2:
      name: Switch 2
      selector:
        entity:
          domain: switch

variables:
  switches: 
    - !input switch_1
    - !input switch_2

trigger:
  - platform: state
    entity_id: 
      - !input switch_1
      - !input switch_2


mode: queued
max_exceeded: silent

condition:
  - condition: template
    value_template: '{{ trigger.to_state.state != trigger.from_state.state }}'

  - condition: template
    value_template: >
      {{ trigger.to_state.context.parent_id is none or 
      (trigger.to_state.context.id         != this.context.id and 
       trigger.to_state.context.parent_id  != this.context.id) }}

action:

  # Set to target value
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    target: 
      entity_id: '{{ expand(switches) | selectattr("entity_id", "!=", trigger.entity_id) | map(attribute="entity_id") | list }}'  

3 Likes

I have some recessed lights that are in the ceiling 2 next to each other. Unfortunately some lights come on without being triggered. Always on by the way, never off or any other change.
Could I use this template to check each pair and if 1 is of, to also switch the other off?
How would I do this?

Appreciate this Blueprint very much. It works perfectly for my needs! Thank you for your solution.

Thank you for your sharing. It seems to be better but still not cover all the cases.
Iā€™ve tested your updated blueprint and if still touch button 1 very fast, there is still a loop (I test 3 times and got 1 time loop).

Beside that, if touch button 1 very fast then touch button 2 only 1 time, the button 1 is not synced with button 2.

I tested with the zigbee switch via zigbee2mqtt

This doesnt work well, some weired flipflop, not many, one or 2 flipflop and it will stop. Sometime stop at the wrong stateā€¦Need to refine. Not usable for now.