Beginner question about reading GPIO inputs to Home Assistant

Hey guys,

This is going to be a pretty simple question, so sorry if it’s super obvious. I’m not a veteran at programming, I do have some experience with Python but I find the json or whatever is used to manually add cards on Lovelace unintuitive.

My goal is this:
I have an alarm panel that I want to pull some information from and put it into Home Assistant. My plan was simply to integrate my alarm panel and the Home Assistant app to create a “common” GUI to access so I could get some rudimentary information about my alarm system without having to open a second app. Short term I wanted to simply add an armed on/off state. Eventually I would like to add an output that would allow me to arm the panel from Home Assistant, although a few problems might arrive with that so I figured I’d start with the armed state.

So that leads me to this:
For starters I wanted to have a card on Home Assistant showing the armed state of the alarm system. So it’s basically a binary sensor. I’ve hooked up my alarm panel to switch a relay on/off depending on its armed state. I’ve hooked up #18 and GND to NO+C on that relay. I believe that’s as it should be. My issue is displaying that information on my Home Assistant dashboard.

This is what I’ve added to the configuration.yaml:

#Adds inputs from RPi GPIO
binary_sensor:
   - platform: rpi_gpio
     ports:
       18: armed_state

As you can see I’ve followed this guide.
I figured I would then find a binary_sensor configuration on the Home Assistant dashboard but I can’t figure out how to add it. I previously modified the configuration.yaml to add a system monitor for the RPi and that showed up as an entity and was super easy to just add to the dashboard, so I was hoping it was the same for the binary sensor.

Would I have to add it manually? If so, I really have no clue on what code to write. I’ve searched far and wide for a guide to set up a simple read of a binary_sensor but I haven’t found anything I felt answered my question.

Are any of you able to help me? It would be much appreciated.

Your binary sensor should be able to be added to Lovelace by using for example an entity card. First however, look in the developer tools, states that your sensor effectively is there.

Thanks for the answer. The binary sensor shows up as an entity now and I can as such add it to Lovelace. My original problem was that it did not show up. Turns out the solution was two reboots of the RPi.

My next problem is that the state of the binary sensor is not continuously updated. It seems very unreliable. Sometimes it does and sometimes it doesn’t. I’ve read that people are using templates to continuously monitor the state of a sensor. Would you think that would solve my problem?

Okay, so I did some digging. HAss is apparently just poor at continuously checking the state of GPIO pins. So I went in as root /sys/class/gpio and exported a folder with the GPIO pin number I’m using for my input. With that I can add:

  - platform: command_line
    name: armed_state
    command: "cat /sys/class/gpio/gpio21/value"
    payload_off: "0"
    payload_on: "1"
    scan_interval: 10

and it the new entity will update every 10 seconds. The downside is perhaps that it’s not instant, but that’s not needed for my sensor, it’s just a visualization of whether the alarm panel is armed or not. I suppose you could lower the scan_interval, not sure how low you can go.

1 Like

This was my conclusion, too. You may have found some of the threads where this was discussed at some length. Hope they helped you come to this workaround more quickly than I did!

I ended up going down to a 5-second scan_interval. Like you, I don’t need split-second results. I’m monitoring the boiler and zone controller in my heating system. The folks who were trying to use GPIO pins for a doorbell function are not so fortunate.

Yes, though it did cause me some wasted hours.

Unrelated to this I am now having issues integrating Google Calendar with my Home Assistant. I get error 401 and the google_calendars.yaml file is not even created. I’m pretty much at a loss about this now. Some people have had the same error but no one seems to have shared their solution. Weirdly trying to integrate Google Calendar also broke my previous solution about reading the GPIO state from the value file in the sys directory tree.

Does anyone have any experience with integrating Google Calendar? I don’t understand why I am having so many issues, since I assume many people use Google Calendar so it should be integrated well? I kept getting errors about my httplib2 version. After installing an OLD version (0.10.3, compared to the current 0.18.x) I stopped getting version error but am now stuck on

Error: Invalid response 401. Error: invalid_client
You will need to restart hass after fixing.

which gives me 0 indication of what the error actually is. Ugh.

This is a pretty active forum. Best to start a new thread with a descriptive title which will catch the attention of folks who use Google calendar in HA.

Yeah, I know it was a bit of a wide shot. I spent another hour on it though and the problem is that the guide on HA’s website about integrating Google Calendar is incomplete, at least in my case. I’ll explain what I did to fix it in case someone else has the same problem and stumbles upon my post.

The guide here tells you to create your Google Calendar API credentials as “TV and Limited Input”. This possibly is the right choice if you installed HA with the HA image, but I installed it just as a package on Raspbian. In my case Google Calendar will show as “invalid client” and error 401 unless I use a credential for “Other UI (e.g. Windows, CLI tool)”.

After I created another set of credentials of the “Other UI” type Google Calendar worked just fine.