Erratic Contact Sensor Behaviour

Hi all –
I connected a magnetic contact/door sensor with an ESP32 board (MH-ET LIVE ESP32) at GPIO5 and GND, with the ESPHome code below:

  - platform: gpio
      number: GPIO5
      mode: INPUT_PULLUP
    name: apt2_magnetic_sensor
    device_class: window
      - delayed_on_off:
          time_off: 30s
          time_on: 0s

When the contact sensor circuit is open, i.e. the magnet is in distance, the binary sensor returns a firm ON, but when the the magnet touches the sensor, it returns a constant ON/OFF/ON/OFF… I had to add a delay to OFF to get some stable output but sometimes, it is not enough.

Any thoughts?

My first guess would be faulty wiring (or a faulty reed switch), especially since it’s only giving you issues when in the closed state. I’d double-check your connections and also swap out the sensor.

Sounds like pin5 is still floating.

  - platform: gpio
      number: GPIO5
      mode: INPUT_PULLUP
      inverted: True

Try adding inverted True first. Otherwise perhaps add a harder pullup with a physical resistor instead of input pullup.

So default state is off. Try without pullup or with pulldown.

We don’t know if it’s the most common normally open or a normally closed reed switch. I use both. Sometimes get tied up in the logic. :crazy_face: I’ve found leaving it floating is an interesting experience.

If sensor has internar pulldown, esp pullup would go in competition…

I have somewhat similar issue. Using input pullup.

  - platform: gpio
      number: GPIO13
        input: true
        pullup: true
      inverted: false
    name: "Mailbox"
      - delayed_on: 100ms

When sensor is either closed or opened (can’t remember now which one is OK), it is fine, but in opposite state, it is flapping. I suspect this is due to bad cable (thin or interference or both, length is ~12m). Would hardware resistor help in this case? What value should I try with? Do I disable internal then? If I disable it now with long cable connected, it does not flap, but it also does not change state at all.

Unshielded cable of that length is huge antenna for interferences.
Like I wrote above, if you don’t know your reed sensor circuit, you can try with internal pulldown first:
pulldown: true (in case of esp32)

For hardware resistor correct value depends on many things, you could start with 1k - 4k7

In case you have bare reed switch (not some sensor circuit), which is just a magnetic switch, you wire it between GPIO and GND and set internal pullup or use hardware pullup resistor.

It is just normal magnetic. When magnets are near each other, circuit is closed and GPIO and GND are connected.
I can try with hw resistor. Do I wire it just between GPIO and GND? Do i need to disable internal then? (Remove pullup: true) part?
With code above, when I just connect sensor to GPIO and GND (20cm cable), and move magnets, it behaves as expected.

If your switch is bitween Gpio and Gnd, you wire 4k7 pullup between gpio and 3.3V
And yes, you can remove internal pullup.

That sounds like a normally open reed switch.