Reed switches gives a lot of false state changes

I have several reed switches mounted on my doors/windows. I have used the MC-38 types and they are all mounted very close to the magnet:

The problem is that in my logbook of Home Assistant I see a lot of wrong state changes. For example this one:

Three times the door is closed and opened. That is correct. And then you see suddenly another 3 times the door “is opened and closed” which is NOT the case.

I have this problem with wrong log items of the reed switches with several windows/doors.

(I have also this problems with C++ sketches on some controllers).

I’m using a Wemos D1 Mini for connecting the reed switches. This is for example the config which is used for the picture above (the log with the wrong state changes):

binary_sensor:
  - platform: gpio
    pin: D5
    name: "Voordeur"
    device_class: door    

I’m using an external resistor for a pull-up:

Are there more people that has this problems? And do I wrong?

Maybe try this and or change the delay

binary_sensor:
  - platform: gpio
    pin: D5
    name: "Voordeur"
    device_class: door 
    filters:
      - delayed_off: 10ms

or this being “on” but the “off” should be yours having a “device_class: door”

binary_sensor:
  - platform: gpio
    pin: D5
    name: "Voordeur"
    device_class: door 
    filters:
      - delayed_on: 10ms

I use it the other way around. I have the resistor on GND and via the 3.3V switch. It will also depend on the length of the wires from the switch. Here I have about 4 m of UTP cable to esp. I have it installed on the door and I use the same system to read the gas meter. Everything works without problems and false impulses.

Maybe connections? if you are using the same reed switch and it works on your gas meter. Also maybe check your cable if it is next to or crossing another power cable

I could try the delay filter on / off. But you see in the log that there are false state changes after 1 and 2 seconds.

The wire I’m using is very thin: 2 x 0.08 mm2. Could that be a reason?

I think there is no problem with the wire connections, everything is soldered.

Yes, the length and cross-section of the wire can affect it (greater wire resistance and voltage drop)
You can try replacing the resistor with a smaller value. For example 4k7 or something similar.

Maybe try @pepe59 suggestions

or have

binary_sensor:
  - platform: gpio
    pin: D5
    name: "Voordeur"
    device_class: door 
    filters:
      - delayed_off: 3s

below is the info Debouncing Values

Okay but what about the cable. Somebody adviced me to have the thinnest cable available. But before I try the smaller resistor and the filter are those alarm cables always a good plan to use?

They seem to have some protection on the wires.

2 x 0,14 mm2
2 x 0,22 mm2

What thickness of the wires do you advise me to use?

I’m using 4 Reed contacts without external pullup resistors on one D1 without problems.
The longest cable is about 10m, Telephone cable 0.6mm2.

binary_sensor:

  - platform: gpio
    pin:
      number: D1
      mode: INPUT_PULLUP
    name: eg_garten_az
    device_class: window

  - platform: gpio
    pin:
      number: D5
      mode: INPUT_PULLUP
    name: eg_garten_ez
    device_class: window

  - platform: gpio
    pin:
      number: D6
      mode: INPUT_PULLUP
    name: eg_hof_ez_li
    device_class: window

  - platform: gpio
    pin:
      number: D7
      mode: INPUT_PULLUP
    name: eg_hof_ez_re
    device_class: window

In any case, I would recommend the largest cross-section that you can afford due to the installation.
It is not a good idea to lay it parallel to the AC power lines.

But are those 2 cables which I posted good to use?

Normally an alarm system works on 12v and our Wemos works with 3.3V.

Or do I need the telephone cable of +/- 0.5 mm2?

Thin cable is not good. The advice should have been thicker is better but remember thicker cost more and you don’t want to thick.

@pepe59 said he uses UTP cable (Ethernet cable). normally it is 0.22 mm2 and twisted. My understanding is that twisted helps with induction of other voltages. So at 4m away it could be ok.

@VDRainer is using telephone cable and it probably twisted as well at 10m. Normally telephone is 0.5mm2 but suggested his is 0.6mm2.

twisted may help and they are normally twisted in pairs.

I use UTP cable as I already wrote. Due to the false pulses you have, it is clear that some voltage from another distribution is being induced into your cable or your GPIO is floating at low/high levels

Okay I will do some tests with telephone cable which I have here on stock.

I have now use everywhere the 10K resistor because in all examples you see the 10K resistor.

But with the ticker cable: can I still use the 10K resistor or is it always better to use the 4.7K resistor?

Anything between 1k and 10k should work

Try using the thicker twisted cable and try to keep it away from power cables especially if it is parallel to the AC power lines as we have suggested. Crossing over power is not that bad but if you can keep about 150mm or 6 inches away it helps.

1 Like

Okay thanks for all the tips!

How far is the reed switch from the ESP? Your schematic (THANK YOU for not using Fritzing) looks good as does your code. You are likely getting induced noise on the GPIO pin. The longer the wire from the ESP, the more likely it’s going to see induced noise. A smaller pullup (4k7) might help. Wire size is irrelevant here since you are only sensing a voltage. There’s no current. Twisted pairs only suppress induced noise in a balanced pair of signal wires which you do not have. Never run low-voltage wires close to line voltage wires. You are almost guaranteed to get induced transients.

Can you wire a Wemos temporarily just a few inches from the switch? If the glitch problem goes away, it is almost certain to be the wiring.

Have you tried a debounce in the code?

You can probably keep your wiring and just add a simple delayed_on filter which should fix your bouncing

binary_sensor:
  - platform: gpio
    pin: D5
    name: "Voordeur"
    device_class: door 
    filters:
      - delayed_on: 500ms # This value needs to be as low as possible but as high that there are no false-positives anymore

The door seems to go a bit over the frame. Does it get blocked by the frame so any pressure difference (by opening/closing) some other door doesn’t move this door just enough for the Reed switch to operate?

1 Like