Graceful shutdown and restart in case of power failure

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.

As yet only on paper. I plan to get a UPS with a NUT (Network UPS Tools) interface, connect it with the HA NUT integration, and write an automation to shut down when battery level is low.

That should be good idea and ideal way to go. Since this setup is for my friend who wants to keep the cost lower, he went with the normal APC ups without any NUT features.

1 Like

I use the following automation setup for managing my UPS in my rack that handles all the primary gear that is connected to it. The actions at 10% charge left are actioned through hass.agent’s MQTT commands to the client installed on the main PC and NAS is set to safe shutoff on its own once the press action is activated and everything to power on when power is restored.

I have a seperate automation setup to notify me when my HA instance has restarted and use a switchbot bot to turn on and off the usb port that my skyconnect is plugged into on my mbeat 7 port hub which I also got rid of the battery from and swapped in a cr2 battery eliminator cable so I don’t have to worry about new batteries when I am not home should I need to control it.

I have smart plugs as well set to on state on power restore as well as backup for core devices to act as a auto power switch for the restore on power loss setting to work more consistently as a failsafe.

alias: UPS Automations
description: ""
trigger:
  - type: battery_level
    platform: device
    device_id: 
    entity_id: 
    domain: sensor
    below: 99
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: UPS Discharging
  - type: battery_level
    platform: device
    device_id:
    entity_id: 
    domain: sensor
    below: 50
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: "UPS Discharging: 50%"
  - type: battery_level
    platform: device
    device_id: 
    entity_id: 
    domain: sensor
    below: 10
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: "UPS Discharging: 10%"
  - platform: state
    entity_id:
      - sensor.main_ups_status
    from: null
    to: Online Battery Charging
    id: UPS Charging
    for:
      hours: 0
      minutes: 0
      seconds: 5
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - UPS Discharging
        sequence:
          - service: notify.mobile_app
            metadata: {}
            data:
              title: UPS
              message: Main UPS is Discharging
              data:
                notification_icon: mdi:battery-arrow-down
                color: yellow
      - conditions:
          - condition: trigger
            id:
              - "UPS Discharging: 50%"
        sequence:
          - service: notify.mobile_app
            metadata: {}
            data:
              title: UPS
              message: Main UPS is at 50%
              data:
                notification_icon: mdi:battery-50
                color: yellow
      - conditions:
          - condition: trigger
            id:
              - "UPS Discharging: 10%"
        sequence:
          - service: notify.mobile_app
            metadata: {}
            data:
              title: UPS
              message: Main UPS is at 10%
              data:
                notification_icon: mdi:battery-10
                color: red
          - device_id: NAS power button
            domain: button
            entity_id: 
            type: press
          - device_id: main pc hass.agent mqtt press
            domain: button
            entity_id: 
            type: press
          - type: turn_off
            device_id: main pc monitors smart plug
            entity_id: 
            domain: switch
      - conditions:
          - condition: trigger
            id:
              - UPS Charging
        sequence:
          - service: notify.mobile_app
            metadata: {}
            data:
              title: UPS
              message: Main UPS is Charging
              data:
                notification_icon: mdi:battery-charging
                color: green
mode: single

1 Like