I’m hoping that someone can give some insight here.
Last night one of my sensors triggered right before I was going to bed. It was one of my window sensors. It momentarily opened and then closed again, but the sensor should not have triggered since the magnet that keeps the circuit closed did not move.
I am using all wired sensors for my doors and windows. All are the reed switch magnetic sensors for an old school alarm. This sensor is about 10 years old (was used for an old alarm), so maybe this is part of the issue… but these sensors are pretty reliable, so I’d be a little surprised.
All sensors are connected to my Raspberry Pi 3 using the GPIO pins and are Normally Open sensors. Meaning that when the window/door is closed (so the magnet is closing the circuit), a current passes through the wire and reports a HIGH to the GPIO pin. I did a little research and found that a floating value (due to electromagnetic interference) can cause ghost switching to occur, but my understanding is that this shouldn’t happen since the closed state is a HIGH value to the GPIO pin. Also, my understanding is that Home Assistant by default uses the built in pull up resistor in the Raspberry Pi to prevent the ghost switching.
I’m concerned because if one of these sensors trips in the middle of the night while my kids are sleeping, I don’t want my sirens to start blaring and wake up the family and the neighborhood!
Should I add a pull up resistor to the circuit? Or am I right here in that this ghost switching should not be the issue? In that case, there must just be an issue with this sensor… I haven’t caught any of the other switches doing this behavior… but something like this happens pretty infrequently, so it’s hard to catch.
Just got another ghost trip on another sensor… This one is also 10 yrs old… But again, it would really surprise me if its the sensor failing. Previous sensor has not tripped again.
I’ll try to help you, but I’m sorry for my English
RPi gpio pins have internal pull up resistors, but for long wires will be better to use external pull up resistors. And also you can use decoupling capacitors (https://en.m.wikipedia.org/wiki/Decoupling_capacitor).
I’m controlling my rolling shutters with my Rpi, but as I need more gpio pins, I used 4 MCP23017 chips. These chips have a “reset” pin that should be connected to high for normal operation. I noticed that sometimes these chips “automatically” reset themselves. And then I realized that it was because, due to interferences, it was receiving low in the reset pin. So, I used a decoupling capacitor next to the reset pin, and it worked perfectly, no more resets.
There was another problem with my setup. All the wall switches for the rolling shutters are connected to mcp23017 input pins (using external pull up resistors, as they are receiving high when nobody is pressing a switch), and sometimes I was receiving a low value in a random input pin, so the shutters sometimes pull up or down randomly. To fix this issue, instead of modifying the stripboard where 4 mcp23017 chips are adding more capacitors, I added an input debouncing by software: when it receives a low in an input pin, a shutter is only pulled if the input is low for more than 100 milliseconds. And it worked like a charm! It seems that the interferences are shorter than 100 milliseconds.
To summarize: you can try adding resistors and/or capacitors, but I think it will be easier to check if you are receiving a low for more than 100 milliseconds (or more, as when the window is really open it will stay for more time). I don’t know how to do it yet in home assistant, as I developed my software using Android things. If your implement it in HA, please post here, because I’m switching to home assistant soon and it may be useful
Thank you for the detailed response. Very helpful!
I think home assistant is already using a deboucing value of 100ms, but I’ll check. Thanks for the idea.
For the decoupling capacitor:
How would I wire this up? In line with sensor?
What nF do you recommend (GPIO pins are using 3.3v)?
You think I could wire one capacitor to the shared ground wire that all of my sensors are connected to, or would I need a decoupling capacitor for each GPIO pin/sensor?
For an external pull up resistor, same question as for the capacitor:
How would I wire this up? In line with sensor?
What ohms do you recommend (GPIO pins are using 3.3v)?
You think I could wire one resistor to the shared ground wire that all of my sensors are connected to, or would I need a resistor for each GPIO pin/sensor?
Should I use both a pull up resistor with the decoupling capacitor?
This is kind of what I’m thinking. Will this work?
I’m using 10 GPIO pins for sensors as input, and 4 for relays as output. The relays are right next to the RPi, and I haven’t had any issues with those ghost switching (as of yet - I’d only know if 2 of the 4 switched).
They are wired up like the diagram I posted above, minus the resistor and capacitor.
Are you using 1x 10k resistor per sensor, or have you combined them like I am thinking about doing above?
Did some more reading up on pull up/ pull down resistors. I think I am starting to understand how this works. Which means my diagram makes no sense at all.
The thing is that my sensor switches are closed when my windows/doors are closed. So there is a HIGH signal being sent to the pin. So I think I need a pull down resistor here, but I should only get ghost switching when the sensor switch is open. Where the pin would falsely read a closed circuit when it was actually open. This was not the case for me.
So that leads me to believe that either:
A: Something when wrong with the sensor switch itself and it temporary opened… maybe its not close enough to the magnet, or maybe its just a bad sensor.
B: Home Assistant did something funky. It did happen not too long after a reboot… maybe it was restoring the last state and something in the data got corrupted… idk. I’m shooting in the dark.
The second ghost switch I posted about turned out to be my wife actually opening the window. It’s pretty cold out where I live, so I didn’t expect her to have opened the window. lol. So I can throw that out.
In the mean time, I did look up the debounce time in HA and it is defaulted to 50ms. So I’m increasing that to 100ms. Hopefully that will prevent it from happening again. If it does, I’ll take a look at the sensor and look at replacing it… or maybe I’ll just do both as a precaution. I really don’t want my 2x 120db sirens going off in the middle of the night accidentally.