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.
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.
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. I’ve found leaving it floating is an interesting experience.
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.
I am coming back as I have been scratching my head with this for quite some time already, and can’t figure it out.
I have esp8266 that i am using for 2 wiegand readers. Wiegand readers are powered by 24V, there is 5V MeanWell power supply powering ESP, grounds of these 2 connected (otherwise wiegand is not working properly). This all works great. I added reed switch to sense when mailbox is opened and that is where it gets weird for me. Binary sensor is configured as follows:
When there is nothing connected between D7 pin and GND, it shows as open. If I short it with short cable, it reports closed. Then connected about 15m long cable that actually leads to mailbox, this is where issues start - as soon as mailbox is opened, contact flips between on/off, filter doesn’t help as these flips are happening and filter just tries to iron it out - unsuccessfully.
So I thought interference, tried new cable, tried to ground the cable, nothing helped really.
So I came up with trying new board. Using exact same board, exact same code - new board works only when powered via phone usb charger. Only then everything works perfectly.
Using the same power supply on previous ESP that has readers on it does not work either.
Bought new power supply (meanwell 5V/3A) and used it only for new ESP - does not work. Powering via VIN+G or via USB port makes no difference. It is simply only “new” ESP with phone charger from usb.
All this is DIN mounted, next to each other. Input for all (all meanwell power supplies or phone charger) are hooked up on the same phase. All inputs are just L+N, output is +/-, no grounding is connected, etc.
I could understand some interference, i just don’t get it what is so special about regular USB phone charger that makes it to work, and why wouldn’t it work on previous ESP then? I have also tried disconnecting 24/5V power supplies that are used by wiegand readers, just to rule out everything there - it makes no difference either.
Also, when I took 5V meanwell powersupply and tried it on my desk with spare ESP with same config, and using 35m long cable (ethernet, used 2 twisted pairs) to short D7 to GND, it worked just fine. ESP was powered via VIN/GND. Any ideas?