Synchronizing Two Switches in Home Assistant

Synchronizing Two Switches in Home Assistant

In a smart home setup, there are situations where you might want two switches to work in harmony. For example, you may want one switch in your living room and another in your hallway to control the same light. When you turn on the light with the first switch, the second switch should recognize that the light is on, and if you use the second switch, it should turn the light off. Synchronizing these switches ensures that they work together seamlessly, providing a cohesive smart home experience.

Using a Blueprint to Synchronize Switches

To achieve this synchronization in Home Assistant, we can use a blueprint. Blueprints are reusable templates that allow you to add complex functionality without writing the code from scratch every time. By using the sync two switches blueprint, you can easily synchronize two switches with minimal effort.

Installing the Blueprint

Installing the blueprint is straightforward. Simply click on the button below to import it directly into your Home Assistant:

If for any reason the button doesn’t work, you can manually import the blueprint by following these steps:

  1. Go to Settings -> Automations & Scenes -> Blueprints -> Import Blueprint.
  2. Enter the following URL:
    https://github.com/moryoav/sync_2switches/blob/main/sync_2switches.yaml
  3. Confirm the import.

Once imported, this blueprint will add the synchronization functionality to your Home Assistant, allowing you to sync two switches effortlessly.

Understanding the Code

Here’s a brief overview of the YAML code used in the blueprint, if you’re interested in what’s happening under the hood:

blueprint:
  name: Sync two switches
  description: Turn on a switch when the other one is turned on and vice versa
  domain: automation
  input:
    switch_entity1:
      name: Switch 1
      selector:
        entity:
          domain: switch
    switch_entity2:
      name: Switch 2
      selector:
        entity:
          domain: switch
variables:
  switch_entity1: !input switch_entity1
  switch_entity2: !input switch_entity2
trigger:
  - platform: state
    entity_id: 
      - !input switch_entity1
      - !input switch_entity2
    from: 'on'
    to: 'off'
  - platform: state
    entity_id: 
      - !input switch_entity1
      - !input switch_entity2
    from: 'off'
    to: 'on'
condition: "{{ trigger.to_state.context.parent_id == none }}"
action:
  - wait_template: "{{ (as_timestamp(now()) - as_timestamp(this.attributes.last_triggered | default(0)) | int > 3) }}"
    timeout: 3
  - service: >
      {% if trigger.to_state.state == 'on' %}
      switch.turn_on
      {% elif trigger.to_state.state == 'off' %}
      switch.turn_off
      {% endif %}
    data: 
      entity_id: >
        {% if trigger.entity_id == switch_entity1 %}
        {{ switch_entity2 }}
        {% elif trigger.entity_id == switch_entity2 %}
        {{ switch_entity1 }}
        {% endif %}

This blueprint monitors the state of two switches. When one switch changes its state (from on to off or vice versa), the other switch is automatically updated to match the corresponding state. This synchronization is achieved using triggers that monitor the state changes and conditions that ensure the automation doesn’t create a loop. The action part of the code determines which switch to update based on the initial trigger.

Using the Blueprint

To use this template in Home Assistant:

  1. Navigate to Settings -> Automations -> Create Automation.
  2. Instead of selecting Create New Automation, choose the Sync two switches option from the list.
  3. On the configuration screen, select the IDs of the two switches you want to synchronize.

The entire process runs behind the scenes, so there’s no need to interact with the code directly.

Conclusion

Synchronizing two switches in Home Assistant is a simple process thanks to blueprints. With just a few clicks, you can ensure that your switches work in unison, making your smart home setup more efficient. This blueprint can be easily adapted to sync lights or even more devices, offering a versatile solution for various automation needs. If you’re interested in exploring more advanced blueprints, feel free to reach out, and I’ll help you take your smart home to the next level.


Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply