Raspberry GPIO - Python Script vs home-assistant configuration

Hello All,

I had some issues with a GPIO binary_sensor ,which is a doorbell, behaving weird (long press on button to trigger, sometimes no event at all, etc).

I went on to write a python script to do exactly what I assumed home assistant should be doing with my config - but it behaves significantly different, which confused me.

I hope one of you can point out my mistake! Goal for this one: Run a python script which only makes a sound.

configuration (binary sensor…)

    - platform: rpi_gpio
      ports:
          18: doorbell 
          #        invert_logic: true #tested both
          pull_mode: DOWN 

automation.yaml:

   trigger:
      platform: state
      entity_id: binary_sensor.doorbell
      to: 'on'
   action:
       - service: shell_command.doorbell #just a media play action

and the python block to compare:

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN)
    while True:
      if (GPIO.input(18) == False):
            play_mp3("/home/homeassistant/doorbell.mp3") #method invoking mpg123

The main differences I observed:

  • Reaction Speed is way higher in python: I need to not press the button as long
  • First press after a long time is 100% ignored in HASS.
  • Sometimes, HASS does not trigger on button press. I searched half an eternity for a hardware-mistake until checking it - not the same issue with python.

All those points make me think I made a configuration mistake.

The full code with all the embarassing stuff I’m currently trying to achieve can be found on github as well, including the full python script.

Opinions?

Bonus-Question:
If this should be just the way it is, is there a more elegant way to trigger a binary event within HASS from the python script?
(I’m currently experimenting with TCP binary_sensors, but more as a learning thing than a long term solution)

Thanks,
bahmi

I just noticed your spacing is off in your yaml, make sure every new line is indented by only 2 spaces.

1 Like

Thank you!

I wasn’t aware that it had to be two spaces, thought it’s more generous - I updated my configs to auto-indent accordingly from now on! I’ll fix the configs tomorrow!

Can something like this already be the cause for inconsistent behaviour? I would’ve assumed that formatting is quite … bool in its result :smiley:

[edit] I stumbled across a different thread where someone has a similar problem. Perhaps correlated?

Thanks!