Synchronize the on/off state of 2 entities

Deprecated

A better and up to date solution can be found here :
https://community.home-assistant.io/t/link-on-off-state-of-multiple-devices/
It should fix most issues found in this blueprint.

I updated the blueprint below but can’t commit to keep it up to date in the future.

Initial post

Simple blueprint to synchronize the state of 2 entities.

I use it to synchonize the state of my lights and switches.

Get started

Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Or import this Blueprint by using the forum topic URL:

blueprint:
  name: Synchronize states
  description: 'Synchronize the on/off state of 2 entities'
  domain: automation
  input:
    entity_1:
      name: First entity
      selector:
        entity:
    entity_2:
      name: Second entity
      selector:
        entity:
mode: restart
max_exceeded: silent
variables:
  entity_1: !input 'entity_1'
  entity_2: !input 'entity_2'
trigger:
  - platform: state
    entity_id: !input 'entity_1'
    to:
      - "off"
      - "on"
  - platform: state
    entity_id: !input 'entity_2'
    to:
      - "off"
      - "on"
condition:
  - condition: template
    value_template: '{{ states(entity_1) != states(entity_2) }}'
  - 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:
  - service: homeassistant.turn_{{ trigger.to_state.state }}
    data:
      entity_id: '{% if trigger.from_state.entity_id == entity_1 %} {{ entity_2 }} {% else %} {{ entity_1 }} {% endif %}'
27 Likes

Very simple, but effective, thanks for this, just what I needed

A great simple solution, very useful! Can I suggest expanding this to also set the brightness level to match? In my case I have a lamp with two Hue bulbs that I always keep in synchronization, if I could also sync the brightness and RGB that is perfect. However, as it is, still useful and I’m using it :slight_smile: :100:

5 Likes

This is exactly what I was also looking for! Will give it a try and let you know how it worked.

Thanks!

just capture the triggering entity, use that to determine current brightness and apply that to the oposing entity.

I think when I did it the brightness had to be converted to brightness_pct to get it to work (I can’t remember exactly it was just a quick and dirty solution), But you could also do it with light groups if your platform supports that

That would need another blueprint, as this one is meant to be used with any entity that can be turned on and off, not necessarily lights.

For your use case a simpler solution would be to use a group : https://www.home-assistant.io/integrations/light.group/

I used this blueprint to synchronize a smart switch and a smart lamp, it works perfectly. I can’t help but be impressed and at the same time “well, I managed to make it work like any dumb switch works”.

Thanks for sharing!

Can this be extended to more than 2 entities? Sorry for the noob question as I am not coder.

2 Likes

I use this automation:

alias: match brightness
description: ‘entity in action matches the brightness of trigger entity’
trigger:

  • platform: state
    entity_id: light.lamp1
    attribute: brightness
    condition: []
    action:
  • service: light.turn_on
    data:
    brightness: ‘{{ states.sensor.lamp2_brightness.state }}’
    entity_id: light.tradfri_driver_5
    mode: single

Well, not necessarily. If you manually adjust a light bulb from outside HA (with a remote or the manufacturer’s app) than a blueprint can still synchronize all bulbs.

I have 2 use cases for this myself:

  1. A set of four lights built into the ceiling, near the rear wall in the living
  2. A set of three bulbs in an IKEA MAGNARP floor lamp

It woule be nice to extend this to more then two states.

1 Like

Hi!

I succeeded to extend this to Three and Four Entities if you need that.

ha_blueprints/SyncStatesFour.yml at main · johanschelin/ha_blueprints (github.com)

1 Like

Fixed if you need it below.

Looks like there’s an error if using 3 Entities :see_no_evil:

Message malformed: Missing input switch_4

Yes you need the “three-version” then. It is on my Git as well.
Didn’t have time or appropriate coding knowledge to make it work in all scenarios :grinning:

Can you elaborate on this a bit. I can’t figure out how to write that automation

1 Like

It’s doesn’t work for me, i has input switch.xxx for entity_1 and cover.xxx for entity 2,
pls help, thank!

I made an update as I had issues with flickering if a switch was clicked very rapidly.

This script is for 2 switches but is scalable to more.

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:
  switch_1: !input switch_1
  switch_2: !input switch_2

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

mode: single

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

action:
  # Set to target value
  - choose:
    - conditions:
      - '{{ trigger.entity_id != switch_1 }}'
      - '{{ trigger.to_state.state != states(switch_1) }}'
      sequence:
        - service: 'switch.turn_{{ trigger.to_state.state }}'
          entity_id: !input switch_1

  - choose:
    - conditions:
      - '{{ trigger.entity_id != switch_2 }}'
      - '{{ trigger.to_state.state != states(switch_2) }}'
      sequence:
        - service: 'switch.turn_{{ trigger.to_state.state }}'
          entity_id: !input switch_2

Hope you enjoy!

8 Likes

Hi,

With HA ver 2021.9.7 I get the following error:

Error rendering service name template: UndefinedError: ‘dict object’ has no attribute ‘to_state’

Please kindly advise

Hi @adchevrier , just wanted to chime in with an interesting side effect, since I love this blueprint, and I use it heavily, maybe I can contribute.

I’ve noticed that I get some error masseges in my logs, HA not findig service which the blueprint calls. I realised it’s due to the fact that sometimes my shelly switches become unavailable, and when this state change happens to any of the lights included in the automation based on this blueprint, it tries to set the other lights to unavailable as well :slight_smile:

Minor issue, doesn’t influence normal behaviour, just wanted to let you know.

Error while executing automation automation.XXXX: Unable to find service homeassistant.turn_unavailable
4 Likes