ESPHome Motion sensors changing states every second

I have 21 hardwired motion detectors in my house centrally wired to two Esp32 devices running ESPHome. One of these ESP32’s is hooked to 12 of the detectors and works flawlessly. The other ESP32 is hooked to 9 detectors. On this ESP32 6 of the detectors are changing their state every second or more. How can I fix these so they are stable?

Here is my ESP32 code

esphome:
  name: motion_sensor_board_2
  platform: ESP32
  board: nodemcu-32s

wifi:
  ssid: "Basement_AP_IoT"
  password: "XXXXXXXX"
  #Static IP
  manual_ip:
    static_ip: 192.168.1.105
    gateway: 192.168.1.1
    subnet: 255.255.255.0
  #Turn Off Power Save Mode
  power_save_mode: none

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Motion-Sensor-Board-2"
    password: "YG6ZvkWHIdDt"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO18
      mode: INPUT_PULLUP
    name: "Basement Garage Door Beam Sensor"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO5
      mode: INPUT_PULLUP
    name: "Side Garage Motion 3"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO17
      mode: INPUT_PULLUP
    name: "Side Garage Motion 2"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO16
      mode: INPUT_PULLUP
    name: "Side Garage Motion 1"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO32
      mode: INPUT_PULLUP
    name: "Side Garage Beam Sensor"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO33
      mode: INPUT_PULLUP
    name: "Big Garage Beam Sensor"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO25
      mode: INPUT_PULLUP
    name: "Upstairs Hall Motion"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO26
      mode: INPUT_PULLUP
    name: "Master Toilet Motion"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO27
      mode: INPUT_PULLUP
    name: "Kassidy Closet Motion"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO14
      mode: INPUT_PULLUP
    name: "Beyer Closet Motion"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO12
      mode: INPUT_PULLUP
    name: "Master Closet Motion"
    device_class: motion
  - platform: gpio
    pin: 
      number: GPIO13
      mode: INPUT_PULLUP
    name: "Garage Closet Motion"
    device_class: motion

Here’s the log showing the problem.

[21:16:42][D][binary_sensor:036]: 'Kassidy Closet Motion': Sending state OFF
[21:16:42][D][binary_sensor:036]: 'Beyer Closet Motion': Sending state OFF
[21:16:42][D][binary_sensor:036]: 'Master Closet Motion': Sending state OFF
[21:16:42][D][binary_sensor:036]: 'Garage Closet Motion': Sending state OFF
[21:16:42][D][binary_sensor:036]: 'Upstairs Hall Motion': Sending state OFF
[21:16:42][D][binary_sensor:036]: 'Master Toilet Motion': Sending state OFF
[21:16:42][D][binary_sensor:036]: 'Kassidy Closet Motion': Sending state ON
[21:16:42][D][binary_sensor:036]: 'Beyer Closet Motion': Sending state ON
[21:16:42][D][binary_sensor:036]: 'Master Closet Motion': Sending state ON
[21:16:42][D][binary_sensor:036]: 'Garage Closet Motion': Sending state ON
[21:16:42][D][binary_sensor:036]: 'Upstairs Hall Motion': Sending state ON
[21:16:42][D][binary_sensor:036]: 'Master Toilet Motion': Sending state ON
[21:16:43][D][binary_sensor:036]: 'Upstairs Hall Motion': Sending state OFF
[21:16:43][D][binary_sensor:036]: 'Master Toilet Motion': Sending state OFF
[21:16:43][D][binary_sensor:036]: 'Kassidy Closet Motion': Sending state OFF
[21:16:43][D][binary_sensor:036]: 'Beyer Closet Motion': Sending state OFF
[21:16:43][D][binary_sensor:036]: 'Master Closet Motion': Sending state OFF
[21:16:43][D][binary_sensor:036]: 'Garage Closet Motion': Sending state OFF
[21:16:43][D][binary_sensor:036]: 'Kassidy Closet Motion': Sending state ON
[21:16:43][D][binary_sensor:036]: 'Beyer Closet Motion': Sending state ON
[21:16:43][D][binary_sensor:036]: 'Master Closet Motion': Sending state ON
[21:16:43][D][binary_sensor:036]: 'Garage Closet Motion': Sending state ON
[21:16:43][D][binary_sensor:036]: 'Upstairs Hall Motion': Sending state ON
[21:16:43][D][binary_sensor:036]: 'Master Toilet Motion': Sending state ON
[21:16:44][D][binary_sensor:036]: 'Upstairs Hall Motion': Sending state OFF
[21:16:44][D][binary_sensor:036]: 'Master Toilet Motion': Sending state OFF
[21:16:44][D][binary_sensor:036]: 'Kassidy Closet Motion': Sending state OFF
[21:16:44][D][binary_sensor:036]: 'Beyer Closet Motion': Sending state OFF
[21:16:44][D][binary_sensor:036]: 'Master Closet Motion': Sending state OFF
[21:16:44][D][binary_sensor:036]: 'Upstairs Hall Motion': Sending state ON
[21:16:44][D][binary_sensor:036]: 'Master Toilet Motion': Sending state ON
[21:16:44][D][binary_sensor:036]: 'Kassidy Closet Motion': Sending state ON
[21:16:44][D][binary_sensor:036]: 'Beyer Closet Motion': Sending state ON
[21:16:44][D][binary_sensor:036]: 'Master Closet Motion': Sending state ON

I suspect the issue is probably the length of cable, how precisely is this wired?

The wires are long. Each sensor has a 22/4wire run to it from the mechanical room. 2 wires run 12v the other 2 hook to the C and NO terminal on the sensors. With each sensor, the wire length is probably between 30ft and 100ft depending on where they are located., all run in the walls/ceilings during construction. But the other ESP32 doesn’t have this issue, and the wires to the sensors on that device are as long or longer than the sensors I’m having issues with on this device.

Add resistors from GPIO’s (input pins) to +3.3V, a few kohms will do, depends on length of a cable, interference… try 10kohm or 4,7kohm. Built-in pullups are pretty high valued (in the range of 50kohm) and it’s not enough if cable is long. I’ve had same problems and they dissapear with additional resistors.
And - put resistors on ESP inputs, not where sensors are.

Second solution would be more complex (and only needed in extreme situations and veeery long cables): switch 12V with sensor’s relay (instead 3.3V) and then at ESP’s inputs lower voltage to 3.3V with resistor divider and zener diode.

Thanks, for the feedback. I don’t like the idea of adding resistors. I’m going to try to isolate the sensors using an opto sensor between the motion sensors and the ESP32.

That would also be ok. Of course, you will still need some kind of pullup resistor at esp side, or input will float when opto won’t conduct.
Adding pullups is very normal and very often - practically almost every circuit (with mcu) have them. You can user SMD’s (if you have skills) on esp board. Inputs just can’t float. They MUST be either GND or +3.3V.

Hi, did you get a resolution for this issue?

Yep, I used an optocoupler between the ESP and the wired sensor.

It was only 1 of the 2 boards doing that? Was there a decent distance between the two boards? Makes you wander where the interface is coming from too. Might not be a bad idea to proactively add them or trying shielded wire too. I’ve read that some lower quality DC psu’s can put out a choppy voltage and interface too. It’s good you got it working with the opto but my curiosity isn’t satisfied lol.

I think one of the wires on one of the sensors had an issue. That was the underlying problem, but the optocoupler fixed everything. It’s been awhile, so I can’t remember the exact issue.