Synchronize the on-off state and brightness of two lights

Inspired by this:

I created a new blueprint that will synchronize the on-off state and brightness of two lights. Note that if one of the lights becomes unavailable, the other one will turn off. This is a good default for me, but it might not suit your use case.

Would be easy to extend to synchronizing other features as well, but these are the ones that I actually use.

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

  name: Synchronize brightness 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: {}
  source_url: https://gist.github.com/fireboy1919/997c80db37de30da76f67c5daeaba27b
mode: restart
max_exceeded: silent
variables:
  entity_1: !input entity_1
  entity_2: !input entity_2
trigger:
- platform: state
  entity_id: !input entity_1
- platform: state
  entity_id: !input entity_1
  attribute: brightness
  
- platform: state
  entity_id: !input entity_2
- platform: state
  entity_id: !input entity_2
  attribute: brightness
condition: []
action:
- choose:
    - conditions:
        condition: template
        value_template: "{{ is_state(trigger.to_state.entity_id, 'on') }}"
      sequence:
        - service: light.turn_on
          data:
            brightness: '{{ state_attr(trigger.to_state.entity_id, "brightness") }}'
            entity_id: '{% if trigger.from_state.entity_id == entity_1 %} {{ entity_2 }} {% else %} {{ entity_1 }} {% endif %}'
  default:
  - service: light.turn_off
    target:
      entity_id: '{% if trigger.from_state.entity_id == entity_1 %} {{ entity_2 }} {% else %} {{ entity_1 }} {% endif %}'
5 Likes

Thank you so much for this. Really missed this kind of functionality from smart lighting on smartthings, and this one is concise and easy to understand.

1 Like

Thank you!

Great blueprint, thanks for sharing!

One question. Have you considers a second entry in the choose section with a condition
"{{ is_state(trigger.to_state.entity_id, 'off') }}"
Using that rather than a default ensures that nothing happens to the target entity if the trigger entity becomes unavailable.

And another suggestion, include synchronization of the dimmer state?

1 Like

Is there a way to add a delay, becuase i have a dimmer light switch in my bedroom and a light bulb I want it to synchronize with. If i change the dimmer to quickly on the light switch, the light bulb and the light seitch start basically arguing and it looks like the light is flickering. Does anyone have a solution for this?

1 Like

Another suggestion will be to add also the colors synchronization :slight_smile:

2 Likes

I kept having an issue where this would seem to “fall out of sync” between the lights. I realized that it would work if I tested it quickly (while the light was still on). If the second light was off, this automation would not turn it back on. I realized that there needs to be a separate “turn on” command before sending a “turn on” command with a brightness attribute. This now works as intended for me.

You can find my gist here: Synchronize States Between Two Lights · GitHub

I created a version that also syncs rgb. (Color)

Test it out and let me know if it works for you. Works in my initial testing.

1 Like

I have the same issue. Another fix would be to make one light the ‘master’ light, that the other follows

fork of the code from @cdchris12

blueprint:
  name: Synchronize brightness states, slave delayed
  description: Synchronize the on/off state of 2 entities, with delay of 10s for the salve
  domain: automation
  input:
    entity_1:
      name: Master entity
      selector:
        entity:
          multiple: false
    entity_2:
      name: Slave entity
      selector:
        entity:
          multiple: false
  #source_url: https://gist.github.com/cdchris12/b3c3546bf446c265adb681088b2623e5#file-light-state-sync-yaml
mode: restart
max_exceeded: silent
variables:
  entity_1: !input entity_1
  entity_2: !input entity_2
trigger:
  - platform: state
    entity_id: !input entity_1
  - platform: state
    entity_id: !input entity_1
    attribute: brightness
  - platform: state
    entity_id: !input entity_2
    for:
      hours: 0
      minutes: 0
      seconds: 10
  - platform: state
    entity_id: !input entity_2
    attribute: brightness
    for:
      hours: 0
      minutes: 0
      seconds: 10
condition: []
action:
  - choose:
      - conditions:
          condition: template
          value_template: "{{ is_state(trigger.to_state.entity_id, 'on') }}"
        sequence:
          - service: light.turn_on
            data:
              entity_id:
                "{% if trigger.from_state.entity_id == entity_1 %} {{ entity_2
                }} {% else %} {{ entity_1 }} {% endif %}"
          - service: light.turn_on
            data:
              brightness: '{{ state_attr(trigger.to_state.entity_id, "brightness") }}'
              entity_id:
                "{% if trigger.from_state.entity_id == entity_1 %} {{ entity_2
                }} {% else %} {{ entity_1 }} {% endif %}"
      - conditions:
          condition: template
          value_template: "{{ is_state(trigger.to_state.entity_id, 'off') }}"
        sequence:
          - service: light.turn_off
            data:
              entity_id:
                "{% if trigger.from_state.entity_id == entity_1 %} {{ entity_2
                }} {% else %} {{ entity_1 }} {% endif %}"
    default:
      - service: light.turn_on
        target:
          entity_id:
            "{% if trigger.from_state.entity_id == entity_1 %} {{ entity_2 }}
            {% else %} {{ entity_1 }} {% endif %}"

3 items with colour temperature

Not sure what this adds over using the built in functionality of ‘groups’ under the helper section. There you can already create groups and control multiple lights are the same time.

@HexaDroid What if you want to control both lights from a physical switch?

So I used a completely different approach and let Boolean logic do the work.

  1. I created TWO groups for the same set of lights: “Light Group AND” with “All entities” selected and “Light Group OR” with “All entities” deselected.

  2. I then created an “On” automation that turns on the AND group when the OR group changes state from off to on (i.e., turn them all on if/when they’re all off and any one turns on).

  3. And an “Off” automation that turns off the OR group when the AND group changes state from on to off (i.e., turn them all off if/when they’re all on and any one turns off).

Seems to work great.

I have a fan control wall switch hooked up to nothing physical. And I have a sonoff ifan04 flashed with esphome.

I have been trying tirelessly to connect the two through automation without success. I simple needed the wall switch to control the ifan.

I did a find and replace on “light” for “fan”, and on “brightness” for “percentage”.

Now this is linking two fans and they work perfectly in sync.

Thanks for this blueprint!