GPIO and Home Assistant
As part of moving my home server to solar power, I need a way of switching over to mains if there’s been a few grey days and the battery is getting low. I’ve attached a relay to the Raspberry Pi’s GPIO to switch the charger input from solar to a MEAN WELL 100 W supply but integrating is surprisingly tricky:
- The
rpi_gpio
module uses RPi.GPIO which isn’t 64 bit clean and fails to compile on Ubuntu - The
remote_rpi_gpio
module uses pigpiod which for some reason uses 20 % CPU when idle.
I settled on
pi-mqtt-gpio
, as most of my other custom integrations are over MQTT. The config is:
mqtt:
host: localhost
topic_prefix: host/keylime
discovery: true
gpio_modules:
- name: gpiod
module: gpiod
digital_outputs:
- name: mains_select
module: gpiod
pin: 17
on_payload: "ON"
off_payload: "OFF"
discovery
is undocumented but enables Home Assistant
MQTT discovery, so it’s zero extra config.
This should be usable on non-Pi hosts as well due to using the gpiod module.