Detect when HomeAssistant offline and reset server with gpio pin

So, my HomeAssistant runs on a Pi3 and seems to go offline roughly weekly. This is clearly not ideal, but I’m assuming that it’s just running out of memory or something. The fix is to use better hardware, but right now PI’s are a little expensive (!)

So. I’ve a few spare ESP01’s.
The idea is to create a node that will notice that HomeAssistant can’t be reached, and flip a GPIO pin that would be connected to the reset line of the Pi.

To work sensibly, it would detect HA being offline, and then continue to check for… three minutes(?) to see if it comes back (could be a brief network interruption), if it does not come back, flip the reset pin.
Importantly, it must not flip the reset pin again for… at least ten minutes, or something. To avoid it rebooting the Pi while it’s still booting up!

So, I started to write the yaml for this and rapidly fell in the dirt. I figure I must be re-inventing the wheel anyway, because surely someone has already done this?

If you’ve done this already, or know a good way to implement it, please share your code :slight_smile:
Many thanks.

Don’t do it, you never ever turn a computer off ungracefully.

Fix whatever is wrong with it!

And you don’t need another pi to get better hardware. Any old PC will do, within reason.

Thanks for the reply.
The Pi is headless and when it falls over, it loses network connection too, so a hard reboot is the only option. I don’t even have a monitor I could connect to it.
The implementation of HomeAssistantOS with it’s containers seems to be quite resilient against unscheduled shutdowns too.

As for ‘fix it’, it’s HomeAssistant’s distribution, the same one anyone else with a Pi3 is using. I don’t think this is a fault, the Pi3 is just getting a little old for this workload.

A Pi draws about 3W of power at load, “Any old PC” will draw, 200 minimum? So, if the 3 gets replaced, it gets replaced with a Pi4.

The ESP’s themselves have a watchdog to force a restart if they can’t connect (after 15 minutes), so I’m just trying to implement the same for the server.

Sure, if you buy a big desktop PC. If however you use a laptop then the power draw is a lot less - doubly so because the main power draws on a laptop are the screen and graphics card, neither of which will be getting used with HA.

That would indeed be a fault, one with the Pi and/or its power supply.

I can try more diagnosis to remedy the fault, here’s what I’ve done so far:

  • Replaced PSU, multiple times, various manufacturers and ratings. Currently using a small branded USB-C power supply with a USBC to MicroUSB cable plugged into it.
  • Replaced cable, multiple times, different types of cables, different lengths.
  • Replaced SD card in the pi, installed HomeAssistant from scratch, reconfigured from scratch without restoring backup.
  • Wiggled the power wire while running. Tapped/Hit the power supply, cable, Pi while running to check for loose connections.
  • Tried to view logs, but can find nothing meaningful in there, probably because I’m not looking in the right place.

None of those changed anything.
I really doubt it’s a hardware issue, because it represents itself as a crash once every ~7 days (I’ve not actually counted it, but I sure do seem to power cycle it on Sundays/mondays a lot). This seems more like software to me.

Perhaps I was wrong, and no-one has done this before. I accept the points about not hard-resetting a computer, but computers do crash sometimes, unexpectedly, and these systems control our lights, doorbells, heating etc. A home assistant instance randomly offline for the first time in years (for example) would benefit from being automatically restarted if it’s owner was away from home for the weekend, etc etc.

Perhaps it shouldn’t be standard practice to have the reset button all the time, but it could prevent a situation where the system falls into an unsafe state.

Why not try restarting every night? Say around 3am run hassio.host_reboot service. It will shut down and restart properly. It is probably running out of memory.

1 Like

To be fair, that’s not a bad idea.
A 3am reboot isn’t going to hurt anything. I’ll write an automation in HomeAssistant to do just that. Thanks.