Raspberry Pi 4 model B hosted HA and using GPIO

Hi there, I’m trawling through posts and getting no joy.
Long term I’m looking to use opto-couplers to link my 12V home alarm PIRs to the GPIO ports of my Pi and I was just wanting to get the s/w working while I wait on the hardware to arrive. My plan was to just test the GPIO pins to 3.3V or GND and hope to see the toggle in HA to then get some automations setup.

HA (spec below)

arch|armv7l
|installation_type|Home Assistant Core|
|os_name|Linux|
|os_version|5.4.83-v7l+|
|python_version|3.7.3|
|version|0.115.6|

The Pi is running :

PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian

By adding the following line to my config file I was naively hoping to see 3 sensor type devices appear.

binary_sensor:
  - platform: rpi_gpio
    ports:
      10: PIR Landing
      11: PIR Office
      12: PIR MasterBedroom

I’ve enabled remote access to GPIO pins (not SPI or I2C) in rasps-config and installed pigpio, rpi.gpio and still no devices.

This article implied no setup required :slight_smile:

Any advice from the collective ?

Thanks.

Don’t use the Pi’s GPIO inputs. Even if you get it working the Home Assistant GPIO integration is unreliable. This issue has been open for over three years:

There are alternatives. My favourite is using another pi running this: Expose the Raspberry Pi GPIO pins over MQTT. It is configured with yaml and has been running reliably for me as long as that issue has been open.

Search the forum for Flyte’s MQTT GPIO software. I have made many posts on this.

2 Likes

Cheers @tom_l I have an older Pi somewhere and give it a go

It’s very light and should run on any model.

Actually as you are running the core version (instead of Home Assistant OS) you should be able to run it on the same pi as home assistant. Though I have never tired this.

So some progress !!
Well, I have pi-mqtt-gpio.log

2021-01-31 15:40:44,850 mqtt_gpio (INFO): Startup
2021-01-31 15:40:45,100 mqtt_gpio (INFO): Connected to the MQTT broker with protocol v3.1.1.
2021-01-31 15:40:45,110 mqtt_gpio (INFO): Polling: Input 'doorbell' state changed to True
2021-01-31 15:40:45,120 mqtt_gpio (INFO): Polling: Input 'PIRLanding' state changed to True

But mosquitto.log

1612107644: Socket error on client pi-mqtt-gpio-e83249bd3ba79932e16fb1fb5100dafade9954c2, disconnecting.
1612107645: New connection from 192.168.0.31 on port 1883.
1612107645: Socket error on client pi-mqtt-gpio-e83249bd3ba79932e16fb1fb5100dafade9954c2, disconnecting.
1612107645: New client connected from 192.168.0.31 as pi-mqtt-gpio-e83249bd3ba79932e16fb1fb5100dafade9954c2 (c0, k60, u'DVES_USER').
1612107665: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.

Should I be worried?
Now with my LED light strips I needed to send a command via Tasmota console to make the devices visible to the MQTT Integration I have in HA (something like SetOption19 1)

Im guessing their is an equivalent - any ideas / pointers ?

Many thanks in advance.

Yes you should not be getting those socket errors.

Yes, there is a discovery boolean you set to true in the mqtt-gpio config: MQTT Configuration · flyte/mqtt-io Wiki · GitHub

This is new. I manually defined my mqtt sensors and switches in home assistant.

Boom - we are good to go !
Edit: (all working on one Pi)

Thank you ++appreciated

1 Like

Hi,
I’m new to HA and Raspberry Pi and just set up HA (core 2023.8.2) on a Pi 4 model B. Just found a way to read the GPIO of the Pi directly from HA:

  1. Install libgpiod in the Pi: go to HA Terminal tab, run command: “apk add libgpiod”. You then should be able to run commands like “gpioget 0 21” (get the state of GPIO21 on chip0). However mine gives error: “error reading GPIO values. Operation not permitted”. I think this is something to do with the terminal user doesn’t have permission to read/write the ports. However the command in HA sensor would works.
  2. In configuration.yaml, add the following:
command_line:
  - binary_sensor:
    name: "My GPIO21 sensor"
    unique_id: "my_gpio_sensor_21"
    command: "gpioget 0 21"
    payload_on: 1
    payload_off: 0

After restarting I have a sensor in HA that I can use to automate other tasks, though not sure how responsive this is, properly not instantaneous but for me this is good enough, and quite simple as well.
I found other solutions using the command “cat /sys/class/gpio/gpio21/value”, or - platform: rpi_gpio don’t work any more. Apparently the newer Raspberry Pi 4 with new OS has discontinued this hardware access method.