Graceful shutdown and restart in case of power failure

I am building a Home Assistant system that needs to work reliably at a location far from where I live. I am thinking about graceful shutdown and restart in case of power failure and have some ideas, but wonder if anyone else has a setup actually working?

I am running HA on a Dell Optiplex 3050 MFF.

I acknowledge previous discussions but they do not cover all of this and are specific to the Raspberry Pi:

I can of course connect the computer and the internet router to a UPS. Then there is no problem as long as the power comes back on before the battery goes flat. After that, it would still fail in an uncontrolled way, like just turning off the power when there is no UPS, and that leaves a risk of database corruption if HA happens to be in the middle of an operation at the time.

I have in mind a small battery-operated device that operates autonomously when HA is off but connects to it when its is on.
It would require
ā€“ a means of communicating with HA (Ethernet, ZigBee, Bluetooth or WiFi)
ā€“ a 230V voltage detector connected to the mains before the UPS, to detect the power failure and inform HA (which will notify me)
ā€“ some way of determining UPS battery status, e.g. DC voltage measurement (?) to inform HA it has to shut down
ā€“ a means of restarting the HA computer, e.g. a relay that does the equivalent of pushing the start button

Has anyone built anything like this for a reasonable price? Perhaps a Shelly Uni plus a 240V relay for the mains detector? Perhaps an ESP32 with a relay for the computer restart output? In either case the power could come from a decent sized battery pack or a separate UPS that would last a very long time compared with the one being monitored??

Did you find a solution for this?

Turning back on is an option in the BIOS: thereā€™s an AC power recovery setting somewhere.

Iā€™m looking for a means to gracefully shutdown a mini PC when power goes off, but donā€™t really need (nor want) a (bulky) UPS.

By luck 5 or 12V input maybe? Could then make use of a power bank that can be charged and discharged at the same time (ā€œpower pathā€). Just need to add a esp than into the mix and some detection if the power bank is charged - or maybe even just read the charge level (ether by measuring the cell voltage or maybe some diodes to detect leds indicating the charge percentage).

Obviously need to check if the switching of the power source is ā€œfastā€ enough to donā€™t crash the mini pc.

For the esp(home) part this could be the snippet to tell HA to do a graceful shutdown:

...
    on_...:
      - homeassistant.service:
          service: hassio.host_shutdown

This can be for example triggered when the battery charge drops to a certain voltage or the charge percentage reaches a certain level. :battery:

I already set the BIOS to recover when the power comes on and that works OK. The question is how to shut it down in a controlled way that would not interfere with the restartā€¦

19V actually, but I have a power pack for that. The computer (a Dell Optiplex) complains that it is not the original power supply but the BIOS can be set to ignore that, so it does work. However it does not report when the mains is off or what the battery level is.

I could buy a separate mains voltage detector and when that goes off, invoke the shutdown procedure, but I do not know how to restart the computer when the mains comes back if the power pack is still providing voltage.

There are smart UPSs that report their mains and battery levels, but then I would need another processor to listen to it over USB that does not require a UPS itself, and presumably a switch in the 19V supply to turn the main computer back on when the power comes back. I was looking for something simpler and cheaper!

1 Like

Hi, i just watched the start of this video today:

im sorry that its in portuguese,but i think its okay to understand with auto translate subtitles.

he is using some ideas that you have, node mcu+ analog voltage sensor and some code, looks a little difficult because he have to calibrate this analog sensor a little, to roughly find the values for energy on or off.
then he uses the state of the energy to create automation to shutdown the home assistant.

For your restart, maybe you can use mqtt on another device, maybe a router with openwrt, to send a wake on lan package for your home assistance, thats one idea.

this looks like an interesting project!

Yes, thatā€™s the kind of thing I had in mind, and a cheap solution! Thanks.

Wake-on-LAN is a great one for the restart (it is available for my Dell Optiplex) and if I get an ESP32 with an ethernet module I could program it from HA with ESPHome to take care of the restart with WoL as well as signalling mains failure for the HA self shutdown. That would work with a cheap 19V UPS like the Eaton 3S Mini.

(This code might help GitHub - teravice/Arduino-WOL: Use arduino to wake on lan any PC )

Iā€™m not sure if there is actually a proper galvanic isolation between AC and DC the way it is implemented in the video.

Wonder how this can work at all - maybe with a very high AC load which might cause a (little) AC voltage drop (in some countries/setups)?

It might be much better (safer and easier) to use a non-contact method detecting AC current like a ZMT103C for example together with a esp(home) node.

I thought he said he rejected that option precisely because of that problem ā€“ but I donā€™t speak Portuguese.

I wonder how to detect a running appliance on AC via voltage anyways because it doesnā€™t matter if a device is on or not as the voltage will/should always be the same :thinking:

Do you need the database?
If not, use a dummy db, it will never corrupt :grin:

# Put the SQLITE db in memory.
recorder:
  db_url: 'sqlite:///:memory:'

Home Assistant does!

You donā€™t. If you want to detect whether a device is running use a plug with a current monitor. But here we want to know if there is a power cut, and that we do with voltage.

You misunderstood me, what I ment is that it can be a memory based databaseā€¦or do you need the data to be saved for future reference? :thinking:

When you use:

# Put the SQLITE db in memory.
recorder:
  db_url: 'sqlite:///:memory:'

HA will have a database, but no data will be written to your harddisk, hence it can not get corrupted either :wink:

But in case of a power down, all data will be lost.

I do not know the ins and outs of the HA database, but I assume HA must write some data to disk sometimes as there would otherwise be no persistence over a restart. Whatever that data is, there is a risk ā€“ as with any computer system ā€“ that if it happens to be in the middle or a write operation when power is lost, data could be corrupted.

It has never happened to me yet with HA so perhaps the risk is low? I have not made a high priority of this project!

There is as far as I know no persistency, that is also the reason that any running automation will restart when ha is being restarted. To my knowledge only statistics are stored.

So if you use a ā€˜virtualā€™ database, it will only reside in memory, and no data is written to diskšŸ˜‰

Helpers are persistent

Hi All,

Just wondering if thereā€™s a proper solution to this?
Iā€™m running adguard on my HA on a DELL optiplex small form factor.
and found that whenever I lost power, and although thereā€™s that BIOS setting to power on automatically, adguard always get itself in its knickers in a twist. So I have to manually power cycle in again one more time before things goes smoothly again.
Thanks

I use it without Adguard and that works OK with the right BIOS setting. Why do you need Adguard with HA?

Hi Andy,
Have you got any solution identified that is working for you? I am in similar situation where the Dell Optiplex 7050 Micro running HA is sitting in a corner room in wild. I have put the system and few of the ESP chips behind 660VA UPS to protect them from abrupt power outages. I am thinking to use one of the cloud based switch that we use to power on the HA remotely with similar BIOS settings that you have. The cloud based switch will be triggered to power on the HA whenever needed. Since this setup is headless the UPS will continue run for 30 minutes.
My idea is this. When HA detects the cloud based switch is offline for more than 20 minutes it will trigger shutdown command and halt. Before shutting down it will generate an email mentioning its status. If you have found any better way please share.