EspHome sonoff 2 way switch functionality (3 way US) automation

Ok,
so I thought I would add a quick post, to possibly help someone trying to create a 2 way (i.e 2 switches 1 light ) (or 3 way) depending on which side of the pond you are, using Sonoff T1’s and EspHome . I did ask the question in this thread here and I got a great answer from @Petrica on using home assistant to automate the 2 way switch .

Through a little trial and error I managed to incorporate this automation in the Sonoff configs and utilise the homeassitant service call to do the switching for me.

esphome:
  name: hallway
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: '*************'
  password: '*************'

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Sonoff Hallway Touchpad 1"
    on_press:
      - switch.toggle: relay_1
  - platform: gpio
    pin:
      number: GPIO9
      mode: INPUT_PULLUP
      inverted: True
    name: "Sonoff Hallway Touchpad 2"
    on_press:
      then:
        - homeassistant.service:
            service: switch.toggle
            data: {"entity_id": "switch.sonoff_landing_relay"}
          
  - platform: status
    name: "Sonoff Hallway Status"

switch:
  - platform: gpio
    name: "Sonoff Hallway Relay 1"
    pin: GPIO12
    id: relay_1
  - platform: gpio
    name: "Sonoff Hallway Relay 2"
    pin: GPIO5
    id: relay_2

output:
  # Register the blue LED as a dimmable output ....
  - platform: esp8266_pwm
    id: blue_led
    pin: GPIO13
    inverted: True

light:
  # ... and then make a light out of it.
  - platform: monochromatic
    name: "Sonoff Hallway Blue LED"
    output: blue_led
    internal: true
    id: blue_led_light

this uses the second gang touchpad of the 2 gang switch in the hallway to utilise the homeassistant service call and toggle the landing light relay.

esphome:
  name: landing
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: '****************'
  password: '***************'

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Sonoff Landing Touchpad"
    on_press:
      - switch.toggle: relay_1
  - platform: status
    name: "Sonoff Landing State"
  - platform: gpio
    pin:
      number: GPIO12
    name: "Sonoff Automation toggle"
    on_state:
      then: 
        - homeassistant.service:
            service: switch.toggle
            data: {"entity_id": "switch.sonoff_hallway_relay_2"}

switch:
  - platform: gpio
    name: "Sonoff Landing Relay"
    pin: GPIO12
    id: relay_1

output:
  # Register the blue LED as a dimmable output ....
  - platform: esp8266_pwm
    id: blue_led
    pin: GPIO13
    inverted: True
    
light:
  # ... and then make a light out of it.
  - platform: monochromatic
    name: "Sonoff Landing Blue LED"
    output: blue_led
    internal: true
    id: blue_led_light

this then uses the state change of the gpio that the relay on landing uses and toggles the switch in the hallway this is essential so that they are both in sync.

I hope this helps someone

@Ottowinter I don’t know if this is something that you would add to your site if you find it useful .

2 Likes

I’ve just used your addition to the config and it works a treat!
Many thanks!

Your very welcome :blush:

Hello! If so, if Home Assistant is offline would it work?

If home assistant is offline you will lose the 2 way functionality, however the switches will function as normal on their own . The switches have no ability to send and receive between themselves they require a 3rd party (i.e home assistant) to communicate between devices.

Regards

1 Like

No as in the code calls a home assistant function.
Hope this helps!
V

I have a tiny problem, the 2 gang status switch leds are inverted. When i turn the switches on in dinning room, status switch leds in the hallway turn off and vice versa.

Automation is working fine just i cannot get the leds to light up correctly in hallway.

IP *.71 is in the dinnig room and the light is wired to this switch.
IP *.79 is in the hallway and is just for switching dinning room relays (device ip *.71)

Code can be found here: https://pastebin.com/LurX0N7T

Help would be greatly appreciated

When the lights are on but the switches show off , when you look at integrations in the configuration tab what does the relay state, under entities , show for the dining room ?
Because it just toggles the switch instead of sending off or on , if the relay is on when the touchpad is pressed it will toggle and the relay will now be off but the lights on .
So you need to make sure relay state is off when lights are off , so when the automation toggles they are in sync .

It looks like this

Dinning room is ON, light is ON, led status ON (which is correct); hallway led status/relay is OFF (but should be ON)

Dinning room is off, light is Off, led status Off (which is correct):, hallway leds status/relay is ON (but should be OFF)

I have no clue how to invert the relay at hallway

Just touch the relay entity and turn it off, it should have a toggle switch when you touch the entity.
It should all be in sync then

So simple :sweat_smile:. Tocuhed it remotly and it looks synched now, later on i will try at home :innocent:

Thank you!

No problem at all .