ESPHome 3ch relay and switch

Hi Guys,

i using NodeMCU with 3 ch relay with external wall switch control and temperature sensor connected to it with Tosmata on it. I have used following GPIOS.

Relay 1 GPIO4 D2
Relay 2 GPIO12 D6
Relay 3 GPIO14 D5

Switch 1 GPIO5 D1
Switch 2 GPIO3 RX
Switch 3 GPIO13 D7

DHT11 GPIO01 TX

It works very well, all the switches connected to external wall switch which acts a 2 way switch.

  • Retains power state on boot
  • Control via existing wall switches as toggle and update status correctly on home assistant
  • Running on MQTT

Only problem with tosmata is everyday it stops responding and i need to restart them to work.
i have tried setting rule in tosmata but its still stop responding. Is there a way i can get this fixed? i will be more than happy if someone could help me out here.

if that’s not fixed then:

I am planning to move them to ESPhome but i have very limited knowledge in coding. i have managed to get the below code.

I have tried searching online but couldn’t get much information about how to proceed. I need the same features how its configured in tosmata. Please help if anyone know the coding.

esphome:
  name: living_room_esp
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "xxxxxxxxx"
  password: "xxxxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Living Room Esp Fallback Hotspot"
    password: "wo6sAy9HS7WY"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "esphome"

ota:
  password: "esphome"

switch:
  - platform: gpio
    name: "Relay1"
    pin: D2
  - platform: gpio
    name: "Relay2"
    pin: D6
  - platform: gpio
    name: "Relay3"
    pin: D5
esphome:
  name: living_room_esp
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "xxxxxxxxx"
  password: "xxxxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Living Room Esp Fallback Hotspot"
    password: "wo6sAy9HS7WY"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "esphome"

ota:
  password: "esphome"

switch:
  - platform: gpio
    name: "Relay1"
    pin: D2
    id: relay1
  - platform: gpio
    name: "Relay2"
    pin: D6
    id: relay2
  - platform: gpio
    name: "Relay3"
    pin: D5
    id: relay3

  - platform: restart
    name: "Device Restart" # optional device restart switch, change the name

binary_sensor:
  - platform: gpio
    name: 'Switch 1'
    restore_mode: RESTORE_DEFAULT_OFF # Attempt to restore state and default to OFF if not possible to restore. Can also be RESTORE_DEFAULT_ON
    pin:
      number: D1
      mode: INPUT_PULLUP # if needed
    on_press:
      then:
        - switch.turn_on: relay1
    on_release:
      then:
        - switch.turn_off: relay1
  - platform: gpio
    name: 'Switch 2'
    restore_mode: RESTORE_DEFAULT_OFF
    pin:
      number: GPIO3
      mode: INPUT_PULLUP # if needed
    on_press:
      then:
        - switch.turn_on: relay2
    on_release:
      then:
        - switch.turn_off: relay2
  - platform: gpio
    name: 'Switch 3'
    restore_mode: RESTORE_DEFAULT_OFF
    pin:
      number: D7
      mode: INPUT_PULLUP # if needed
    on_press:
      then:
        - switch.turn_on: relay3
    on_release:
      then:
        - switch.turn_off: relay3

  - platform: status
    name: "Device Connection Status" # optional useful sensor, change the name

sensor:
  - platform: dht
    pin: GPIO01
    model: DHT11
    temperature:
      name: "Whatever Temperature"
    humidity:
      name: "Whatever Humidity"
    update_interval: 60

  - platform: wifi_signal # optional useful sensor, change the name
    name: "WiFi Signal Sensor"
    update_interval: 60s

1 Like

Thanks Tom for the quick reply.

I tried pasting this code but there seems to be some issue on line it give a red cross.
Errors on line number:
47, 59, 71, 93
error

This

update_interval: 60

Should be this:

update_interval: 60s

Not sure how that happened, I copied it directly from the ESPHome example. Must have just missed the last character.

What are the errors on lines 47, 59, and 71?

EDIT: never mind, I see it. I put the restore option in the binary sensors instead of the relays, try this:

esphome:
  name: living_room_esp
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "xxxxxxxxx"
  password: "xxxxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Living Room Esp Fallback Hotspot"
    password: "wo6sAy9HS7WY"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "esphome"

ota:
  password: "esphome"

switch:
  - platform: gpio
    name: "Relay1"
    restore_mode: RESTORE_DEFAULT_OFF # Attempt to restore state and default to OFF if not possible to restore. Can also be RESTORE_DEFAULT_ON
    pin: D2
    id: relay1
  - platform: gpio
    name: "Relay2"
    restore_mode: RESTORE_DEFAULT_OFF
    pin: D6
    id: relay2
  - platform: gpio
    name: "Relay3"
    restore_mode: RESTORE_DEFAULT_OFF
    pin: D5
    id: relay3

  - platform: restart
    name: "Device Restart" # optional device restart switch, change the name

binary_sensor:
  - platform: gpio
    name: 'Switch 1'
    pin:
      number: D1
      mode: INPUT_PULLUP # if needed
    on_press:
      then:
        - switch.turn_on: relay1
    on_release:
      then:
        - switch.turn_off: relay1
  - platform: gpio
    name: 'Switch 2'
    pin:
      number: GPIO3
      mode: INPUT_PULLUP # if needed
    on_press:
      then:
        - switch.turn_on: relay2
    on_release:
      then:
        - switch.turn_off: relay2
  - platform: gpio
    name: 'Switch 3'
    pin:
      number: D7
      mode: INPUT_PULLUP # if needed
    on_press:
      then:
        - switch.turn_on: relay3
    on_release:
      then:
        - switch.turn_off: relay3

  - platform: status
    name: "Device Connection Status" # optional useful sensor, change the name

sensor:
  - platform: dht
    pin: GPIO01
    model: DHT11
    temperature:
      name: "Whatever Temperature"
    humidity:
      name: "Whatever Humidity"
    update_interval: 60s

  - platform: wifi_signal # optional useful sensor, change the name
    name: "WiFi Signal Sensor"
    update_interval: 60s
1 Like

Wow thanks again.

Everything works fine but just on power failure its goes to OFF state, its not restoring the last state on power failure.

I read something about this but don’t know how to add it.

Yeah that’s the issue. As long as you have read the warning and don’t expect the device to lose power that often:

esphome:
  name: living_room_esp
  platform: ESP8266
  board: nodemcuv2
  esp8266_restore_from_flash: true ### <-----Add it here ###
1 Like

Great!!! its working like charm. Thank you Tom.

Here is the final code. I modified the toggle action.

esphome:
  name: living_room_esp
  platform: ESP8266
  board: nodemcuv2
  esp8266_restore_from_flash: true
wifi:
  ssid: "xxxx" # Enter the SSID here
  password: "xxxx" # Enter your password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Living Room Esp Fallback Hotspot"
    password: "wo6sAy9HS7WY"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "esphome"

ota:
  password: "esphome"

switch:
  - platform: gpio
    name: "Relay1"
    restore_mode: RESTORE_DEFAULT_OFF # Attempt to restore state and default to OFF if not possible to restore. Can also be RESTORE_DEFAULT_ON
    pin: D2
    id: relay1
  - platform: gpio
    name: "Relay2"
    restore_mode: RESTORE_DEFAULT_OFF
    pin: D6
    id: relay2
  - platform: gpio
    name: "Relay3"
    restore_mode: RESTORE_DEFAULT_OFF
    pin: D5
    id: relay3

  - platform: restart
    name: "Device Restart" # optional device restart switch, change the name

binary_sensor:
  - platform: gpio
    name: 'Switch 1'
    pin:
      number: D1
  #    mode: INPUT_PULLUP # if needed
    on_press:
      then:
        - switch.toggle: relay1
    on_release:
      then:
        - switch.toggle: relay1
  - platform: gpio
    name: 'Switch 2'
    pin:
      number: GPIO3
  #    mode: INPUT_PULLUP # if needed
    on_press:
      then:
        - switch.toggle: relay2
    on_release:
      then:
        - switch.toggle: relay2
  - platform: gpio
    name: 'Switch 3'
    pin:
      number: D7
    #  mode: INPUT_PULLUP # if needed
    on_press:
      then:
        - switch.toggle: relay3
    on_release:
      then:
        - switch.toggle: relay3

  - platform: status
    name: "Device Connection Status" # optional useful sensor, change the name

sensor:
  - platform: dht
    pin: GPIO01
    model: DHT11
    temperature:
      name: "Whatever Temperature"
    humidity:
      name: "Whatever Humidity"
    update_interval: 60s

  - platform: wifi_signal # optional useful sensor, change the name
    name: "WiFi Signal Sensor"
    update_interval: 60s
1 Like

Can I ask why?

This has the possibilty of getting out of sync. Before you were guaranteed that the relay would turn on when the binary sensor was on, and off when the binary sensor is off.

If you are using momentary switches instead of toggle switches you can simplify the code to just this for the binary sensors:

binary_sensor:
  - platform: gpio
    name: 'Switch 1'
    pin:
      number: D1
    on_press:
      then:
        - switch.toggle: relay1
  - platform: gpio
    name: 'Switch 2'
    pin:
      number: GPIO3
    on_press:
      then:
        - switch.toggle: relay2
  - platform: gpio
    name: 'Switch 3'
    pin:
      number: D7
    on_press:
      then:
        - switch.toggle: relay3

I am using the following button.

switch

Its not a push button. It will stay in same state until we switch it back and when the circuit is closed it needs to toggle and when its released.