Shutdown safely if power fails using GPIO input

I am new to the under-the-hood workings of Home Assistant. I want to be able to shut down HA gracefully after a period of time if a power failure is detected. The Pi will be connected to a UPS. I do this on some of my other Pi projects by running a Python script that monitors a GPIO pin that is connected to a 3V source not powered by the UPS the Pi is plugged into.

So I would like to be able to monitor power by reading the GPIO input going low, poll it a couple of minutes after the power failure is detected to make sure it is not just a temporary glitch, and initiate a graceful shutdown if the AC power is still out. I have at least 5 minutes of UPS power to rely on.

In a full Rasbian install this is easily done, but I don’t see how to do it in a HA install. I did see some old posts but they pointed me to GPIO features in HA that were deprecated. Is there a way to do this and are there any articles that anyone could point me to?

Thanks in advance.


GPIO support is now via a third party integration. This recent post should put you on the right track:

In your case a mains relay could switch 3.3v on the GPIO to trigger your automation.