Link state of 3 switches together

Hi

Trying to link 3 switches states together.
Having used the script as found on How to link 2 switches together that works great on 2 switches I tried to expand it to 3 switches with mixed results.
It is not working are expected.

- alias: Living Room Light
  trigger:
    - platform: state
      entity_id: switch.sonoff_100064f420_3, switch.sonoff_10007ecf5a_2, light.sonoff_10009951b8_1
      from: 'on'
      to: 'off'
    - platform: state
      entity_id: switch.sonoff_100064f420_3, switch.sonoff_10007ecf5a_2, light.sonoff_10009951b8_1
      from: 'off'
      to: 'on'
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      switch.turn_on
      {% elif trigger.to_state.state == "off" %}
      switch.turn_off
      {% endif %}
    data_template: 
      entity_id: >
        {% if trigger.from_state.entity_id == "switch.sonoff_100064f420_3" %}
        switch.sonoff_10007ecf5a_2, light.sonoff_10009951b8_1
        {% elif trigger.from_state.entity_id == "switch.sonoff_10007ecf5a_2" %}
        switch.sonoff_100064f420_3, light.sonoff_10009951b8_1
        {% elif trigger.from_state.entity_id == "light.sonoff_10009951b8_1" %}
        switch.sonoff_10007ecf5a_2, switch.sonoff_100064f420_3
        {% endif %}

What is the unexpected behaviour ?

The switches don’t sync together.
I tweaked around and 2 switches would sync, but would exclude the 3rd.

What you are wanting is if one of the three entities is turned on, they are all turned on, and vice versa ?

Is the light entity the one being excluded ?
If so, it will be because your service call is for switch.turn_on / off, yet one of your entities is a light

Some changes to your automation:

  1. You want to trigger on ANY state change, so don’t need to reference to: or from:
  2. Since 0.115 you no longer have to specify service / data_template, it is assumed
  3. Replace switch.turn_on / off with homeassistant.turn_on / off to cater for the mix of entities
  4. Remove the entity_id template. If the switch / light is already on / off, no big deal
- alias: Living Room Light
  trigger:
    - platform: state
      entity_id: switch.sonoff_100064f420_3, switch.sonoff_10007ecf5a_2, light.sonoff_10009951b8_1
  action:
    service: >
      {% if trigger.to_state.state == "on" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: light.sonoff_10009951b8_1, switch.sonoff_10007ecf5a_2, switch.sonoff_100064f420_3

1 Like

Thanks
Works like a dream.