Pi GPIO Binary Sesnsor

Hello

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.

This is my code.

switch:
  platform: rpi_gpio
  ports:
17: Nursery
18: LivingRoom
27: OutsideLight
22: Heating
23: Pin16
24: Bathroom
25: Under Stairs
5: Pin 29
  invert_logic: true

binary_sensor:
  platform: rpi_gpio
  ports:
16: OutLight
21: LRoom
20: Nursery
26: Bathroom
  value_high: "On"
  value_low: "Off"
  invert_logic: true
  bouncetime: 50
  pull_mode: "UP"

automation:
- alias: 'OutLight'
  trigger:
platform: state
entity_id: binary_sensor.OutLight
state: 'on'
  action:
service: switch.toggle
entity_id:
   - switch.OutsideLight
- alias: 'LRoom'
  trigger:
platform: state
entity_id: binary_sensor.LRoom
state: 'on'
  action:
service: switch.toggle
entity_id:
   - switch.LivingRoom
- alias: 'Nursery'
  trigger:
platform: state
entity_id: binary_sensor.Nursery
state: 'on'
  action:
service: switch.toggle
entity_id:
   - switch.Nursery
- alias: 'Bathroom'
  trigger:
platform: state
entity_id: binary_sensor.Bathroom
state: 'on'
  action:
service: switch.toggle
entity_id:
   - switch.Bathroom

Hope some one can resolve this, im begining to give up :confused:

Thanks

Could anybody verify the code please?

Thanks in advance

my json formatting skill arent great so spacing suggestion may be incorrect.
You can try changing the service call in the automation. this may help.

switch looks OK
Maybe formatting (couldnt tell if it was your post only)

switch:
  platform: rpi_gpio
  ports:
    17: Nursery
    18: LivingRoom
    27: OutsideLight
    22: Heating
    23: Pin16
    24: Bathroom
    25: Under Stairs
    5: Pin 29
  invert_logic: true

binary sensor look OK
Maybe formatting (couldnt tell if it was your post only)

binary_sensor:
  platform: rpi_gpio
  ports:
    16: OutLight
    21: LRoom
    20: Nursery
    26: Bathroom
  value_high: "On"
  value_low: "Off"
  invert_logic: true
  bouncetime: 50
  pull_mode: "UP"

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)

automation:
  - alias: 'OutLight'
    trigger:
      - platform: state
        entity_id: binary_sensor.OutLight
        state: 'on'
    action:
      - service: homeassistant.toggle
        entity_id: switch.OutsideLight

  - alias: 'LRoom'
    trigger:
      - platform: state
        entity_id: binary_sensor.LRoom
        state: 'on'
    action:
      - service: homeassistant.toggle
        entity_id: switch.LivingRoom

  - alias: 'Nursery'
    trigger:
      - platform: state
        entity_id: binary_sensor.Nursery
        state: 'on'
    action:
      - service: homeassistant.toggle
        entity_id: switch.Nursery

  - alias: 'Bathroom'
    trigger:
      - platform: state
        entity_id: binary_sensor.Bathroom
        state: 'on'
    action:
      - service: homeassistant.toggle
        entity_id: switch.Bathroom

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)

I will report back

Thanks again

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.

hi

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 :slight_smile: :slight_smile:

Thanks again

Hi Frank,

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:

  • platform: command_line
    command: python3 /home/pi/work/read_doorbell.py
    name: Doorbell
    scan_interval: 1

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()

i hope that makes sense

Hi

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.

Thanks

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()

Hi Frank,

Have noticed on the most recent release of HA inputs are more responsive.
Are you still having issues?

Rob.

Hi

I give up in the end and not tried them again since. Iam now using ESP modules

Cheers

No probs. Frank,

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.

Rob.

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.

Hi Guys

I’m having the same issue as everyone above.

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.

Hi, may I know if this problem has been resolved? I am having the same issue but cannot find a solution after searching on the forum and web.

Thanks!

I think I found a solution to this eventually.

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.

Cheers!

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.

From the GPIO Spec, I found the note “Pins 3 and 5 are pulled up on the board to the 3.3V supply by 1.8 KΩ resistors to enable them to be used easily as I2C communication pins.” So maybe another quick fix is to use pin 3 or 5 if they are empty, but I haven’t tried it yet.

Of course still hope someone can fix the bug in the code


Right now I don’t need an input pin, I just play a little bit with Home Assistant.
Is there a ticket system for entering bugs?

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.