Im having a bit of touble with binary sensor on Pi GPIO, I have exhausted all hard wiring options, resistors, short wires, long wires, ferrite rings etc connected to various push button types (all normally open going normally closed momentary) in a live house enviroment. I have also tried a momentary relay connected to the GPIO input as this toggles in minimal ms.
im fully educated on relays, wires etc as this has been my day to day job for many years, as a IP CCTV/Network Engineer. So im now thinking it must have something to do with how my configuration is coded. Its simple in many ways i want a physical switch on a wall connected to GPIO input to trigger a GPIO output.
Switching the outputs via the HA web interface, works everytime. So no problems there. toggling the inputs (switch on wall), only randomly automates and switches the output.
try homeassistant.toggle or explicit homeassistant.turn_on / homeassistant.turn_off instead of switch.toggle maybe
Maybe formatting (couldnt tell if it was your post only)
Thanks for your help on this. I will give it a go shortly. I think my code spacing is correct, i dont get any errors in the log and as i say it does trigger randomly, but is nowhere near reliable, it may trigger once every 20 presses of the switch (this includes leaving an interval between each press)
after reading again, it sound like the GPIO switches (outputs) are OK
it is the binary sensor (inputs) that are NG
what is your high, low voltage value?
is the state of these sensors stable.
I did have this same issue when using input #4. I remember this causing the sensor to show unexpected state (low when I expect high, for example)
try using these #18, #27, #22, #24, #25, #5 or #8 if this is easy to do for you.
Also, try verifying the input is stable. provide high 3.3V/Low 0V to pin and confirm expected result.
There is no problems with the outputs at all. Ive used the code you provided and im still having the same random results.I have also configured GPIO5 as a binary sensor as per one of the preferred inputs.
I have also noticed when running gpio readall in the cli gpio5 is listed as mode OUT should this not read as IN for input? I have forced it to input using gpio mode and still have the same results. I have noticed though when restarting the HA services, gpio readall reverts gpio 5 as OUT again.
My physical switches all seem stable and i am currently for testing using a momentary NO --> NC breadboard push button (soldered to the 10cm of GPIO Pin Wire, tested switch with multimeter).
It just seems a really strange issue im having and i thought it would have been one of the more simpler tasks, but its taken hours and hours up to now. Im confident there is an answer somewhere. I have also purchased some arduino ESP8266 i can see that being a challenge
Iâm having the same problem (the binary_sensor seems to âfloatâ). Iâm running homeassistant 0.36.1 on 2017-01-11-raspbian-jessie on pi zero
My sensor is defined as follows:
binary_sensor:
platform: rpi_gpio
pull_mode: UP
invert_logic: FALSE
ports:
24: Sensor 24 (pin 18) bell # connect to ground
I just have a wire from physical pin 6 to physical pin 18, with no resistors (though I added a switch to make testing easier).
I have no âproperâ solution with homeassistant, but I have got a workaround which seems to work. So the information below might shed some light.
I have a python script (read_doorbell.py) that i run separately (as a non-binary sensor command_line, but setting the binary_sensor):
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP) # pin 18
status = GPIO.input(24)
if status == 0:
print(âONâ)
else:
print(âOFFâ)
GPIO.cleanup()
read_doorbell.py gets run as a homeassistant (non-binary) sensor command_line:
sensor D:
This works, but is not an ideal solution as (a) itâs done by polling every second and (b) itâs a separate script to maintain. However it suggests to me that there is something in the homeassistant config / code that prevents this sensor from working as I expect (rather being than hardware thing).
Note a simple python script, run from the shell, seemed to show the status of the pin to be a solid 0 or 1 when I made / broke the circuit:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP) # pin 18
while True:
status = GPIO.input(24)
print status
GPIO.cleanup()
Thanks for the info and im glad its not just me having the same issues⊠Can any of the developers shed any light on this issue???
As i thought it was interference, i went and purchased an ESP8266 to have it local to the switch locations, however i seem to be getting the same results.
I will give the python scripts a go, as you say not ideal but if it fixes it for now its a solution.
As a side note im still running on the previous version.
Hi Frank,
i found my python scripts were not exiting so the pi gradually slowed down. If you do try something based on my scripts, you need to add an exit to the python scripts or find some sort of method to avoid running the scripts many times.
Also, in case my previous posting was a bit complicated, here is the simple python script i used to test the basic circuitry outside home assistant (which might help).
Richard
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP) # pin 18
status = GPIO.input(24)
print(status)
while True:
status = GPIO.input(24)
print(status)
time.sleep(0.25)
GPIO.cleanup()
Sounds like the input pin was floating, and maybe it needed a stronger pull up resistor then the internal weak enabled.
But I also noticed on earlier versions of HA to also not always register a button press as well.
So could be a combo of issues.
I donât know if you resolved your issue yet but Iâve been playing with push-buttons (Iâm eventually going to add a 4 output 315MHz receiver and use 4-button fobs to toggle lights).
I was having a similar issue where the button worked PART of the time but wouldnât reliably indicate âACTIVEâ when pressed and âNOT ACTIVEâ when released. Sometimes the sensor icon would hang in âACTIVEâ after the button was released.
I figured out that changing the âbouncetime: 1â gets rid of all debounce processing and then it responded flawlessly (so far).
Warning, BOUNCETIME value must be a minimum of 1. Setting it to zero (0) will cause a soft error which prevents the sensors from showing up and functioning.
Been testing and trying everything I can think off. Tried a few different settings and the problem stays the same.
I have tried WayneTâs bounce time reduction but even then it only works some time but not always.
I have scripted a quick small script in python to test the hardware side and from that script everything are 100%
My initial idea were to run a small script in the back ground of the Pi to do the switching of the relays for time being while this issue persist in HA but even that has now fallen down the drain as HA does not update the status of the GPIOâs correctly.
It seems like HA only update the sensor status every few milliseconds (good normal practice) but if the switching does not happen it the refresh time HA does not update the status for either Inputs or Outputs.
So whats happening at this stage is
you push the button outside HAâs refresh time and HA does not pick up that the switch are on (even after 5 mins HA still donât read the switch closed)
now if you play with a few other switches you have the same until it picks up one that changed in the refresh period, once it picks one up all of the inputs are suddenly updated and working but then you have the same again switching them off.
The same on the outputs. you can change the output to be on with python and HA doesnât see that if you fallen in the blind time frame of not refreshing but as soon as one gets picked up all of the output statuses gets updated in HA.
Surely this are a small error in HA coding around the refreshing of the GPIO statuses.
If thereâs anyone out there that might know how to fix this problem or even if there are anyone that can point me in the right direction to someone on the development team, that will be fantastic.
I see a lot of ppl struggle with this issue and it would be nice to get it sorted out.
For those who have the same issue, I would suggest you add a pull up resistor from 5V pin to the input pin. I donât know what is wrong with the Pi GPIO Binary sensor code (as I do see the internal pull up command in the code) but it looks like the input pin is floating which caused the problem.
Thatâs dangerous because the GPIOs are only 3V3 compatible. Raspberryâs GPIOs.
It must be a bug in Home Assistant. With pure python scripts everything works fine also using interrupt instead of polling.
Then we should connect the pull up resistor from the 3.3V pin. In fact, I did this in the first beginning but donât know why it couldnât fix the floating problem so I changed it to 5V.
For me it is working okay, I am using a pull up resistor to 3.3V and a 100nF capacitor from input to ground. As trigger I am using âfromâ and âtoâ
trigger:
- entity_id: binary_sensor.doorbel
platform: state
from: 'off'
to: 'on'
Sometimes the state stays in âonâ instead in âoffâ, but the trigger shown above seems to be robust so far.