[Solved] 2 Way light switch using mqtt

(Edit, go to the third post for an alternative method to getting 2 way switching working. 2 way switching is quite popular in the UK. Complete with examples. Although to use these Sonoff T1 Light switches you may need to alter your wiring…)

I have been trying to copy the setup from the post Avoiding Loops when copying light status

What I want is two wall switches to control one light.
What I have is two Sonoff T1 wall switches. One is connected to the light (sonoffLM) and the other in only powered (sonoffLS).
So when I switch ON the sonoffLS the other switch with the light attached sonoffLM will switch ON as well.

I am using the Sonoff-Tasmota firmware

I can control the individual switches on the HA web interface. So I know my mqtt setup is working and I can see the command in the terminal log on the switch.

But I just can’t get the two way switching to work as per the above example.

Can anyone see anything obvious with my code? As I am finding yaml quite a challenge.

light:
  - platform: mqtt
    name: "sonoffLM"
    state_topic: "stat/sonoffLM/POWER"
    command_topic: "cmnd/sonoffLM/POWER"
    availability_topic: "tele/sonoffLM/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

  - platform: mqtt
    name: "sonoffLS"
    state_topic: "stat/sonoffLS/POWER"
    command_topic: "cmnd/sonoffLS/POWER"
    availability_topic: "tele/sonoffLS/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

  - platform: group
    name: LivingRoomLights
    entities:
    - light.sonoffLM
    - light.sonoffLS

automation:
  # Control LivingRoom_lights
  - alias: "sonoffLM"
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: light.sonoffLM
    condition: 
      - condition: template
        value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.sonoffLS.attributes.last_triggered | default(0)) | int > 2)}}'    
    action:
      service_template: >
        {% if trigger.to_state.state == "on" %}
        light.turn_on
        {% elif trigger.to_state.state == "off" %}
        light.turn_off
        {% endif %}
      entity_id: light.LivingRoomLights

  - alias: "sonoffLS"
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: light.sonoffLS
    condition: 
      - condition: template
        value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.sonoffLS.attributes.last_triggered | default(0)) | int > 2)}}'    
    action:
      service_template: >
        {% if trigger.to_state.state == "on" %}
        light.turn_on
        {% elif trigger.to_state.state == "off" %}
        light.turn_off
        {% endif %}
      entity_id: light.LivingRoomLights

Thanks for any pointers you guys can provide.

I have been playing around with this all afternoon.
I am sure I am missing something simple!

I tried to copy a more simple form Help to set 2 way switch via mqtt

I ended up removing the group and changing my code to the following:

automation:
  # Control LivingRoom_lights
  - alias: "sonoffLM"
    trigger:
      platform: state
      entity_id: light.sonoffLM  
    action:
      service_template: >
        {% if trigger.to_state.state == "on" %}
        light.turn_on
        {% elif trigger.to_state.state == "off" %}
        light.turn_off
        {% endif %}
      entity_id: light.sonoffLS

  - alias: "sonoffLS"
    trigger:
      platform: state
      entity_id: light.sonoffLS   
    action:
      service_template: >
        {% if trigger.to_state.state == "on" %}
        light.turn_on
        {% elif trigger.to_state.state == "off" %}
        light.turn_off
        {% endif %}
      entity_id: light.sonoffLM

This did not work either.

One thing that I have noticed is in the original mqtt bit for each light is that the command to turn on / off is POWER:ON / POWER:OFF which I can see in the terminal if I log into the light it’s self through a browser.

  - platform: mqtt
    name: "sonoffLM"
    state_topic: "stat/sonoffLM/POWER"
    command_topic: "cmnd/sonoffLM/POWER"
    availability_topic: "tele/sonoffLM/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

Could it be that instead of turn_on / turn_off I need a power equivalent?
Instead of the following?

  service_template: >
    {% if trigger.to_state.state == "on" %}
    light.turn_on
    {% elif trigger.to_state.state == "off" %}
    light.turn_off
    {% endif %}
  entity_id: light.LivingRoomLights

I have finally resolved the issue by following what @icaman004 posted in the following thread Sudo 2 way how to setup

I had trouble initially as I put all of the code into the configuration.yaml file.
So if it helps anyone else that is new here, this is exactly how I have it setup.

configuration.yaml

light:
  - platform: mqtt
    name: "sonoffLM"
    state_topic: "stat/sonoffLM/POWER"
    command_topic: "cmnd/sonoffLM/POWER"
    availability_topic: "tele/sonoffLM/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

  - platform: mqtt
    name: "sonoffLS"
    state_topic: "stat/sonoffLS/POWER"
    command_topic: "cmnd/sonoffLS/POWER"
    availability_topic: "tele/sonoffLS/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

groups.yaml

LivingRoomLights:
  view: no
  name: 'Living Room Lights'
  entities:
    - light.sonoffLM
    - light.sonoffLS

automations.yaml

- alias: Living Room Lights On
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: group.LivingRoomLights
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: group.LivingRoomLights

- alias: Living Room Lights Off
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: light.sonoffLM
      to: 'off'
    - platform: state
      entity_id: light.sonoffLS
      to: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: group.LivingRoomLights
1 Like

Someone might find this 2022 post useful, since it’s a pretty similar scenario: Tips to configure a Zigbee 4 gang switch (4 channel) thru MQTT