ESPHOME: Erratic Switch Behavior - Switches suddenly going Off and On - sometime resets the Device

Hi All, I am facing an issue with ESPhome config. My Switches using Binary sensor setup is creating havoc in the operations. Through app / webserver / Home Assistant everything works perfectly. But when I use the Switch - it works for sometime - suddenly while switching on some other lights, all the lights goes off momentarily, sometime the Device simply goes for reboot, sometime it flashes. Remember its not just a single light , it can be any. I have 8 such Lights and 8 such binary switches connected. I have seen EMI (Noise in the wire) so I have added Physcial resistors to pull up - it has reduced any the continuous flickering (on/Off) which was happening earlier. I have used Delays or Debounce in the code which has also helped. I have used Pull_Up which also helped. But at the end of the day its just not working as its completely unreliable - You switch on one switch and one fine moment - it again switches off and on other lights, rests the device and behaves erratic.
Condition: The Wires between the Binary Sensor GPIO and the DEVICE are very long - almost 6 meters and it is surely creating EMI and resistance, resulting to Flicker - Its solved using Physical Pull up resistors and debounce,
But I am far away from a Solution. Can anyone help?

#substitutions:
#friendly_name: 8switch77ver3
esphome:
name: 8switch77ver3
platform: ESP32
board: nodemcu-32s

on_boot:
priority: -100
then:
- lambda: |-
// Initialize pins early for stability
pinMode(32, INPUT_PULLUP);
pinMode(33, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(27, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);

      digitalWrite(4, HIGH);  // Adjust based on your relay logic
      digitalWrite(17, HIGH); 
      digitalWrite(18, HIGH);
      digitalWrite(19, HIGH);
      digitalWrite(16, HIGH);
      digitalWrite(21, HIGH);
      digitalWrite(22, HIGH);
      digitalWrite(23, HIGH);    

wifi:
networks:
- ssid: “SSID1”
password: “Pass”
manual_ip:
static_ip: 192.168.29.77
gateway: 192.168.29.1
subnet: 255.255.255.0

- ssid: "SSID2"
  password: "Pass"
  manual_ip:
    static_ip: 192.168.29.77
    gateway: 192.168.29.1
    subnet: 255.255.255.0 

power_save_mode: none
use_address: 192.168.29.77

ota:

api:
reboot_timeout: 0s

web_server:
port: 80

Define switches for relays

switch:

  • platform: gpio
    pin: GPIO4
    name: “Relay 1”
    id: relay_1
    restore_mode: ALWAYS_OFF
    inverted: true
  • platform: gpio
    pin: GPIO16
    name: “Relay 2”
    id: relay_2
    restore_mode: ALWAYS_OFF
    inverted: true
  • platform: gpio
    pin: GPIO17
    name: “Relay 3”
    id: relay_3
    restore_mode: ALWAYS_OFF
    inverted: true
  • platform: gpio
    pin: GPIO18
    name: “Relay 4”
    id: relay_4
    restore_mode: ALWAYS_OFF
    inverted: true
  • platform: gpio
    pin: GPIO19
    name: “Relay 5”
    id: relay_5
    restore_mode: ALWAYS_OFF
    inverted: true
  • platform: gpio
    pin: GPIO21
    name: “Relay 6”
    id: relay_6
    restore_mode: ALWAYS_OFF
    inverted: true
  • platform: gpio
    pin: GPIO22
    name: “Relay 7”
    id: relay_7
    restore_mode: ALWAYS_OFF
    inverted: true
  • platform: gpio
    pin: GPIO23
    name: “Relay 8”
    id: relay_8
    restore_mode: ALWAYS_OFF
    inverted: true

Define binary sensors for physical switches

binary_sensor:

  • platform: gpio
    pin:
    number: GPIO32
    mode:
    input: true
    pullup: true
    name: “Physical Switch 1”
    id: physical_switch_1
    filters:

    • delayed_on: 500ms
    • delayed_off: 500ms
      on_press:
    • switch.turn_off: relay_1 # Turn on relay on switch press
      on_release:
    • switch.turn_on: relay_1 # Turn on relay on switch press
  • platform: gpio
    pin:
    number: GPIO33
    mode:
    input: true
    pullup: true
    name: “Physical Switch 2”
    id: physical_switch_2
    filters:

    • delayed_on: 500ms
    • delayed_off: 500ms
      on_press:
    • switch.turn_off: relay_2 # Turn on relay on switch press
      on_release:
    • switch.turn_on: relay_2 # Turn on relay on switch press
  • platform: gpio
    pin:
    number: GPIO25
    mode:
    input: true
    pullup: true
    name: “Physical Switch 3”
    id: physical_switch_3
    filters:

    • delayed_on: 500ms
    • delayed_off: 500ms
      on_press:
    • switch.turn_off: relay_3 # Turn on relay on switch press
      on_release:
    • switch.turn_on: relay_3 # Turn on relay on switch press
  • platform: gpio
    pin:
    number: GPIO26
    mode:
    input: true
    pullup: true
    name: “Physical Switch 4”
    id: physical_switch_4
    filters:

    • delayed_on: 500ms
    • delayed_off: 500ms
      on_press:
    • switch.turn_off: relay_4 # Turn on relay on switch press
      on_release:
    • switch.turn_on: relay_4 # Turn on relay on switch press
  • platform: gpio
    pin:
    number: GPIO27
    mode:
    input: true
    pullup: true
    name: “Physical Switch 5”
    id: physical_switch_5
    filters:

    • delayed_on: 500ms
    • delayed_off: 500ms
      on_press:
    • switch.turn_off: relay_5 # Turn on relay on switch press
      on_release:
    • switch.turn_on: relay_5 # Turn on relay on switch press
  • platform: gpio
    pin:
    number: GPIO14
    mode:
    input: true
    pullup: true
    name: “Physical Switch 6”
    id: physical_switch_6
    filters:

    • delayed_on: 500ms
    • delayed_off: 500ms
      on_press:
    • switch.turn_off: relay_6 # Turn on relay on switch press
      on_release:
    • switch.turn_on: relay_6 # Turn on relay on switch press
  • platform: gpio
    pin:
    number: GPIO12
    mode:
    input: true
    pullup: true
    name: “Physical Switch 7”
    id: physical_switch_7
    filters:

    • delayed_on: 500ms
    • delayed_off: 500ms
      on_press:
    • switch.turn_off: relay_7 # Turn on relay on switch press
      on_release:
    • switch.turn_on: relay_7 # Turn on relay on switch press
  • platform: gpio
    pin:
    number: GPIO13
    mode:
    input: true
    pullup: true
    name: “Physical Switch 8”
    id: physical_switch_8
    filters:

    • delayed_on: 500ms
    • delayed_off: 500ms
      on_press:
    • switch.turn_off: relay_8 # Turn on relay on switch press
      on_release:
    • switch.turn_on: relay_8 # Turn on relay on switch press

logger:

text_sensor:

  • platform: wifi_info
    ip_address:
    name: ESP IP Address
    #name: $friendly_name IP Address

captive_portal:

I am a rookie at this, so I am pretty much just guessing, but it sounds like you have tried to prevent EM noise at the ESPHome end.
What about at the device end? Have you prevented it there too?

Was the first time you tested any switches or binaary sensors after they were all wired and set up in esphome?

You need to use code formatting when posting yaml too. The way it is now, it isnt helpful. Can you please fix that first.

Yes at the Device end also - I am using Hardware Pull Up resistors - Hardware changes has improved the Situation but its still an issue. Actually it seems not all people has exposure beyond the Software level. I am sure when you use long wires in the Binary Switch to connect GPIO’s with the physical Switch - EMI will occur. But is there any one who has done some modifications or work to prevent this?

I would test a single light connected with a short wire.
If that works, then connect all 8 lights, but with short wires.
If that works, then it seems like it is the length of the wire in combination with the transmitters and receivers.
I would then probably look into maybe using a ESP at each light and then an ESP in the central point, which means the long wires connect two ESP devices and you can then choose a connection type that support it better.

Alternatively you could pull new shielded wires and then make sure the shield is connected to ground.