Need help with configuring 8 Channel Relay Module with ESP32 in HA with manual switches

Hi,
I know this topic has been covered in other posts, but I am really unable to understand YAML and keep encountering errors.
I have set up AI Thinker Esp32-S variant with 8 channel Relay Module. Now, I want to connect manual switches (dumb Wall Switches) as a fail-safe in case esp32 does not have wi-fi or in case I do not have my phone handy, in which case, the relays would trigger with switches as well.

I have tried using on_press / on_click and other triggers, but kept encountering errors “does not inherit value” and so I left that option and went to HA automation creations.
I have created 8 switches that I am able to operated from the overview page of the HA without any glitch.
I am looking to use input pins (specifically PIN 5) on ESP32-S connected to the switches so that upon being activated with 3v current, they would trigger a relay. I have created HA Automation, however, that is not working when I connect PIN 5 to the 3V PIN on ESP32. Although when I press on Run Action, it does trigger Switch 1.
Any help would be much appreciated. I am pinning my Yaml code along with HA automations screenshot here.
Thanks in advance

‘’’
captive_portal:

output:

  • platform: gpio
    id: “Switch_1”
    pin: 32
    inverted: true

  • platform: gpio
    id: “Switch_2”
    pin: 33
    inverted: true

  • platform: gpio
    id: “Switch_3”
    pin: 25
    inverted: true

  • platform: gpio
    id: “Switch_4”
    pin: 26
    inverted: true

  • platform: gpio
    id: “Switch_5”
    pin: 27
    inverted: true

  • platform: gpio
    id: “Switch_6”
    pin: 14
    inverted: true

  • platform: gpio
    id: “Switch_7”
    pin: 23
    inverted: true

  • platform: gpio
    id: “Switch_8”
    pin: 22
    inverted: true

light:

  • platform: binary
    output: “Switch_1”
    name: “Switch 1”

  • platform: binary
    output: “Switch_2”
    name: “Switch 2”

  • platform: binary
    output: “Switch_3”
    name: “Switch 3”

  • platform: binary
    output: “Switch_4”
    name: “Switch 4”

  • platform: binary
    output: “Switch_5”
    name: “Switch 5”

  • platform: binary
    output: “Switch_6”
    name: “Switch 6”

  • platform: binary
    output: “Switch_7”
    name: “Switch 7”

  • platform: binary
    output: “Switch_8”
    name: “Switch 8”

binary_sensor:

  • platform: gpio
    name: “M_Switch_1”
    pin:
    number: 5
    mode:
    input: true
    pullup: true
    ‘’’

This is the Automation Config

""id: ‘1659254205259’
alias: Manual Switch
description: ‘’
trigger:

  • platform: state
    entity_id:
    • binary_sensor.m_switch_1
      from: ‘off’
      to: ‘on’
      for:
      hours: 0
      minutes: 0
      seconds: 2
      condition: []
      action:
  • type: toggle
    device_id: 7ef06d02902738e1a7d45adbe1df55ab
    entity_id: light.switch_1
    domain: light
    mode: single""

Reading your requirements “fail-safe” no “wi-fi”. Because this is your requirement you can’t use HA as a automation, HA will will need wi-fi conection. So that’s out. You must have it on your ESP code… so

You cold try this. Must have a push button switch not toggle (on/off).

replace this

binary_sensor:

    platform: gpio
    name: “M_Switch_1”
    pin:
    number: 5
    mode:
    input: true
    pullup: true

with this

binary_sensor:
  - platform: gpio
    pin: GPIO5
    name: "M_Switch_1"
    on_press:
      then:
        - switch.toggle: Switch_1

or you can try this

first have to put id in light

light:

    platform: binary
    output: “Switch_1”
    id: switch_1_light
    name: “Switch 1”

then again replace your binary sensor with this. Your switch must be pulse… like press button not a toggle

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO5
      inverted: true
    name: "M_Switch_1"
    on_press:
      then:
        - light.toggle: 
            id: switch_1_light

Thank you for the help. You are correct, HA Automations would not work in that case.
The HA Automations are working, as I was wrongly trying to trigger it by connecting pins with VCC and not GND.

I tried using the code provided by you, but it is ending up in fast flickering of switches. I even tried adding a delay of 1000ms like :
“”"
on_press:
then:
- delay: 1000ms
- light.toggle:
id: switch_1_light
“”"
but it keeps on flickering.
I think it an issue with the “toggle function”. Can you help me with this.
Thanks a lot in advance.

yep

not sure what is happening try pin 12

I have tried other pins, but the flickering issue remains.
Here is the log for the same.

[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:40][D][light:035]: ‘Switch 1’ Setting:
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:40][D][light:035]: ‘Switch 1’ Setting:
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:40][D][light:035]: ‘Switch 1’ Setting:
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:40][D][light:035]: ‘Switch 1’ Setting:
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:40][D][light:035]: ‘Switch 1’ Setting:
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:40][D][light:035]: ‘Switch 1’ Setting:
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:40][D][light:035]: ‘Switch 1’ Setting:
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:40][D][light:035]: ‘Switch 1’ Setting:
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:40][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:40][D][light:035]: ‘Switch 1’ Setting:
[23:14:41][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:41][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:41][D][light:035]: ‘Switch 1’ Setting:
[23:14:41][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:41][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:41][D][light:035]: ‘Switch 1’ Setting:
[23:14:41][D][light:035]: ‘Switch 1’ Setting:
[23:14:41][D][light:046]: State: ON
[23:14:41][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:41][D][light:035]: ‘Switch 1’ Setting:
[23:14:41][D][light:035]: ‘Switch 1’ Setting:
[23:14:41][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:41][D][light:035]: ‘Switch 1’ Setting:
[23:14:41][D][light:046]: State: OFF
[23:14:41][D][light:035]: ‘Switch 1’ Setting:
[23:14:41][D][light:046]: State: ON
[23:14:41][D][binary_sensor:036]: ‘M_Switch_1’: Sending state ON
[23:14:41][D][binary_sensor:036]: ‘M_Switch_1’: Sending state OFF
[23:14:41][D][light:035]: ‘Switch 1’ Setting:

Hi @AwAcS did you try this? just wondering :thinking:

binary_sensor:
  - platform: gpio
    pin: GPIO5
    name: "M_Switch_1"
    on_press:
      then:
        - switch.toggle: Switch_1

and or have you commented out or played around with the invert?

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO5
#      inverted: true
    name: "M_Switch_1"
    on_press:
      then:
        - light.toggle: 
            id: switch_1_light

the only other I can thing of is resistors on the ESP

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO5
      inverted: true
      mode:
        input: true
        pullup: true
    name: "M_Switch_1"
    on_press:
      then:
        - light.toggle: 
            id: switch_1_light

info to this is below… really hope this help you. :grinning:

Hey @Blacky,
Cannot thank you enough for the help you are providing.
I seem to have pin pointed the issue. It does not lie with the code, rather with the ESP32 module.
I am using “Ai Thinker NodeMCU-32S-ESP32 Development Board – IPEX Version”

I unplugged all the wires providing input signals (even though I had not connected them to any GND / VCC) leaving the bare pins. Using the code provided by you, these pins seem to be touch sensitive. Upon even touching the blank wire without any charge, it seems to trigger the connected relays (connected via code) and if kept touched, they start flickering. Removing the wire stops the relays from flickering but then keeps them in “on” state.
Will add code herein again, just in case it may point out the reason for such action.

‘’’
esphome:
name: behind-wardrobe

esp32:
board: nodemcu-32s
framework:
type: arduino

Enable logging

logger:

Enable Home Assistant API

api:
encryption:
key: “Gc+euCrfRv6ZbvAtHB2jdAG6dh9TkkJecUeaL188X98=”

ota:
password: “b770b9daa0af1b9d2bf891ebe9e8be34”

wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password

Enable fallback hotspot (captive portal) in case wifi connection fails

ap:
ssid: “Behind-Wardrobe Fallback Hotspot”
password: “M0ORLZX4ziPy”

captive_portal:

output:

  • platform: gpio
    id: “Switch_1”
    pin: 32
    inverted: true

  • platform: gpio
    id: “Switch_2”
    pin: 33
    inverted: true

  • platform: gpio
    id: “Switch_3”
    pin: 25
    inverted: true

  • platform: gpio
    id: “Switch_4”
    pin: 26
    inverted: true

  • platform: gpio
    id: “Switch_5”
    pin: 27
    inverted: true

  • platform: gpio
    id: “Switch_6”
    pin: 14
    inverted: true

  • platform: gpio
    id: “Switch_7”
    pin: 23
    inverted: true

  • platform: gpio
    id: “Switch_8”
    pin: 22
    inverted: true

light:

  • platform: binary
    output: “Switch_1”
    name: “Switch 1”
    id: switch_1_light

  • platform: binary
    output: “Switch_2”
    name: “Switch 2”
    id: switch_2_light

  • platform: binary
    output: “Switch_3”
    name: “Switch 3”
    id: switch_3_light

  • platform: binary
    output: “Switch_4”
    name: “Switch 4”
    id: switch_4_light

  • platform: binary
    output: “Switch_5”
    name: “Switch 5”
    id: switch_5_light

  • platform: binary
    output: “Switch_6”
    name: “Switch 6”
    id: switch_6_light

  • platform: binary
    output: “Switch_7”
    name: “Switch 7”
    id: switch_7_light

  • platform: binary
    output: “Switch_8”
    name: “Switch 8”
    id: switch_8_light

binary_sensor:

  • platform: gpio
    pin:
    number: 21
    inverted: true
    name: “M_Switch_1”
    on_press:
    then:
    - delay: 1000ms
    - light.turn_on:
    id: switch_1_light
    on_release:
    then:
    - light.turn_off:
    id: switch_1_light

  • platform: gpio
    pin:
    number: 19
    inverted: true
    name: “M_Switch_2”
    on_press:
    then:
    - delay: 1000ms
    - light.turn_on:
    id: switch_2_light
    on_release:
    then:
    - light.turn_off:
    id: switch_2_light

  • platform: gpio
    pin:
    number: 18
    inverted: true
    name: “M_Switch_3”
    on_press:
    then:
    - delay: 1000ms
    - light.turn_on:
    id: switch_3_light
    on_release:
    then:
    - light.turn_off:
    id: switch_3_light

  • platform: gpio
    pin:
    number: 16
    inverted: true
    name: “M_Switch_4”
    on_press:
    then:
    - delay: 1000ms
    - light.turn_on:
    id: switch_4_light
    on_release:
    then:
    - light.turn_off:
    id: switch_4_light

  • platform: gpio
    pin:
    number: 17
    inverted: true
    name: “M_Switch_5”
    on_press:
    then:
    - delay: 1000ms
    - light.turn_on:
    id: switch_5_light
    on_release:
    then:
    - light.turn_off:
    id: switch_5_light

  • platform: gpio
    pin:
    number: 13
    inverted: true
    name: “M_Switch_6”
    on_press:
    then:
    - delay: 1000ms
    - light.turn_on:
    id: switch_6_light
    on_release:
    then:
    - light.turn_off:
    id: switch_6_light
    ‘’’

Hi,
All the selected input pins seem to be capacitive and working with touch as well. Could you kindly help me with the code so that the same may be used as push button. I saw a few examples wherein the value of ohms received upon touch was configured to be read as push button but they all were for Aurdino board and not HA (I mean yaml code).
However, with the board type as “nodemcu-32s”, it keeps on saying that most of the pins do not support touch.
Using the code provided, all the pins used are working with touch alone.

I am trying to follow along but I am unsure…

1/ With the code you just posted you have disconnected the switch and have bare wires.
2/ If that is correct when you touch the wires together (Bridge it out) and keep them connected the light comes on? (no flickering)
3/ When you disconnect them the light goes off?

next
1/ With my code you have disconnected the switch and have bare wires.
2/ If that is correct when you touch the wires together and release the light comes on? (no flickering)
3/ When you touch them together and hold them light on flickering?
4/ If you touch them together and release light on and if you gain touch them together and release light off.

Q: In HA do you have a toggle switch that turns the light on/off? if so what is the ID

sorry for asking questions… just trying to work this out in my head… would love to have in next to me :pensive:

more code to try

binary_sensor:
   - platform: gpio
     pin:
       number: 21
       mode: INPUT_PULLUP
       inverted: True
     name: "M_Switch_1"
     filters:
       - delayed_on: 10ms
     on_press:
       then:
         - light.toggle: 
             id: switch_1_light #this id is to be the one that works in HA change if necessary

or

binary_sensor:
   - platform: gpio
     pin:
       number: 21
       mode: INPUT_PULLUP
       inverted: True
     name: "M_Switch_1"
     filters:
       - delayed_on: 10ms
     on_press:
       then:
         - light.toggle: 
             id: switch_1 #this id is to be the one that works in HA change if necessary

just checking your switch is wire from 21 to Ground?

I am sorry for the delay and the confusion.
The ESP32-S module I have been using seems to vary from the standard ESP32.
When the ESP was connected to the relay, the pins that were to be used as switch (Manual / dumb), were touch sensitive. So I removed the wires from those pins (input pins) and tried touching the exposed pins with bare hand, which seems to have been creating problems and switching the relays on and off leading to flickering.
So I tried to use “esp32_touch” platform but not all pins were supported to be used as touch inputs. Then I started shuffling the output pins and input pins (As per ESP-32 board layout for touch sensitive pins). I am successful in using 5 input pins which should be enough for now.
Now it is working perfectly once it is on, however, upon it being switched on, all the input pins send switch on signal (only once when the esp restarts). Is there any way around it?
I am also putting my code here, just in case you can provide more inputs or if someone else needs it in future.
Thanks a ton.

esphome:
name: behind-wardrobe

esp32:
board: nodemcu-32s
framework:
type: arduino

Enable logging

logger:

Enable Home Assistant API

api:
encryption:
key: “Gc+euCrfRv6ZbvAtHB2jdAG6dh9TkkJecUeaL188X98=”

ota:
password: “b770b9daa0af1b9d2bf891ebe9e8be34”

wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password

Enable fallback hotspot (captive portal) in case wifi connection fails

ap:
ssid: “Behind-Wardrobe Fallback Hotspot”
password: “M0ORLZX4ziPy”

captive_portal:

esp32_touch:
setup_mode: false

output:

  • platform: gpio
    id: “Switch_1”
    pin: 18
    inverted: true

  • platform: gpio
    id: “Switch_2”
    pin: 19
    inverted: true

  • platform: gpio
    id: “Switch_3”
    pin: 25
    inverted: true

  • platform: gpio
    id: “Switch_4”
    pin: 26
    inverted: true

  • platform: gpio
    id: “Switch_5”
    pin: 17
    inverted: true

  • platform: gpio
    id: “Switch_6”
    pin: 16
    inverted: true

  • platform: gpio
    id: “Switch_7”
    pin: 23
    inverted: true

  • platform: gpio
    id: “Switch_8”
    pin: 22
    inverted: true

light:

  • platform: binary
    output: “Switch_1”
    name: “Switch 1”
    id: switch_1_light

  • platform: binary
    output: “Switch_2”
    name: “Switch 2”
    id: switch_2_light

  • platform: binary
    output: “Switch_3”
    name: “Switch 3”
    id: switch_3_light

  • platform: binary
    output: “Switch_4”
    name: “Switch 4”
    id: switch_4_light

  • platform: binary
    output: “Switch_5”
    name: “Switch 5”
    id: switch_5_light

  • platform: binary
    output: “Switch_6”
    name: “Switch 6”
    id: switch_6_light

  • platform: binary
    output: “Switch_7”
    name: “Switch 7”
    id: switch_7_light

  • platform: binary
    output: “Switch_8”
    name: “Switch 8”
    id: switch_8_light

binary_sensor:

  • platform: esp32_touch
    name: “ESP32 Touch Pad PIN 13”
    pin: 13
    internal: true
    threshold: 1000
    on_state:
    then:
    - light.turn_on:
    id: switch_1_light
    on_release:
    then:
    - delay: 1000ms
    - light.turn_off:
    id: switch_1_light

  • platform: esp32_touch
    name: “ESP32 Touch Pad PIN 14”
    pin: 14
    internal: true
    threshold: 1000
    on_state:
    then:
    - light.turn_on:
    id: switch_2_light
    on_release:
    then:
    - delay: 1000ms
    - light.turn_off:
    id: switch_2_light

  • platform: esp32_touch
    name: “ESP32 Touch Pad PIN 27”
    pin: 27
    internal: true
    threshold: 1000
    on_state:
    then:
    - light.turn_on:
    id: switch_3_light
    on_release:
    then:
    - delay: 1000ms
    - light.turn_off:
    id: switch_3_light

  • platform: esp32_touch
    name: “ESP32 Touch Pad PIN 32”
    pin: 32
    internal: true
    threshold: 1000
    on_state:
    then:
    - light.turn_on:
    id: switch_4_light
    on_release:
    then:
    - delay: 1000ms
    - light.turn_off:
    id: switch_4_light

  • platform: esp32_touch
    name: “ESP32 Touch Pad PIN 33”
    pin: 33
    internal: true
    threshold: 1000
    on_state:
    then:
    - light.turn_on:
    id: switch_5_light
    on_release:
    then:
    - delay: 1000ms
    - light.turn_off:
    id: switch_5_light

You must use pullup or pulldown on your binary sensors

  - platform: gpio
    name: “ESP32 Touch Pad PIN 27”
    pin:
      number: 27
      mode:
        input: true
        pullup: true
    internal: true

And maybe a filter

  - platform: gpio
    name: “ESP32 Touch Pad PIN 27”
    pin:
      number: 27
      mode:
        input: true
        pullup: true
    internal: true
    filters: 
      - delayed_on: 100ms 
      - delayed_off: 100ms 

Avoid pin 14 if possible

Hi there,
I did try to use the pull up function but it does not work with platform “esp_touch” and i had to change it to gpio. Changing the same to gpio led to having same issues of flickering. I did some tinkering and came to a conclusion that I had to set definition for threshold using “ESP32_touch” platform. I used a filter and it is working without any glitch. I am avoiding PIN 14 as suggested by you. It is working as intented. I will have to make some changes based on how my esp reacts on reboot but that will be done at a later stage.
Thank you for all the help.

Send picture of your board.

You shouldn’t be having problems with input_pullup and as you are using switches using the binary sensor is the best option in my opinion.
Consider using resistors to do the pullup, I imagine you are doing the bench tests. If yes depending on where you run the wires you may start having problems with the finished project

This is the link to the board

This simply is an issue with the chip. I guess they are of sub par quality and hence the flickering issues and issues with them not connecting properly with WiFi.
Although a few of them work fine with “esp_touch” platform. Using them for now till I get replacements for all the chips.
Ordered esp32 wroom

They are working amazingly well with the code that you guys provided. Thank you for all the help guys.