Detecting power outages

I’m running HA at our vacation home. It’s a rural location and there are power outages, especially in winter. I’d like to get a notification when these happen.

What would be a good way to detect the power outages? Can I run an automation when HA shuts down? I trust that HA shuts down gracefully with restarts and host reboots also? What about if a reboot is issued from the command line?

My idea is, that a clean shut down is stored somewhere, and when HA comes back up, if the mark isn’t there, it sends a notification. This won’t reveal the length of the outages, though. I might add another automation that records the current time every minute, and use that to calculate the length.

Personally I’d drop a UPS in that offers a suitable uptime (depending on your hardware) and use it to power the HA host and networking equipment necessary to communicate out. Make sure that your hardware can resume power state after an outage.

Then either integrate the UPS into HA or an independent service and setup your shutdown etc.

Combine that with an external monitor like UptimeRobot.

I have our networking gear, storage boxes and HA server running on small UPSs that give enough time to shut down cleanly within 5 to 10 minutes of an outage, leaving 30 minutes runtime for the network hardware. For powering back up I rely on wake on lan to avoid scenarios where the power is bounced repeatedly. I’ve had this happen once - it cost me an non-UPSd server and I’m still salty about it.

I agree with @TazUk ; you would definitely need a UPS for a graceful shutdown. I reccommend you to use the same UPS for both HA server and also the router (to keep the internet up); so whenever you have power outage, the system can send you an immediate message whenever the power is down. You would need a smart UPS with a USB connection to the HA server, so whenever the power is down and the battery is about to deplete; the linux and HA can gracefully shut down using NUT or apcupsd or the native app of that UPS. I use an APC BackUPS for that.

Also, there are different ways to understand when the power is down; for myself i use the “power mains” attribute of the dumb alarm system IP connection card. You can also use the power attribute of a UPS connected sensor or “unavailable” attribute of a mains connected sensor or a diy sensor specifically made for power outages (connected to UPS and HA server via PINs or ethernet cable) or the attributes of the smart UPS.

Like TazUk and febalci said, an UPS would be the best solution, but that’s something nice to play with. :slightly_smiling_face:

Create a file sensor

sensor:
  - platform: file
    name: Restart Log
    file_path: "/config/restart.log"

and a file notification

notify:
  - name: restart_notify
    platform: file
    filename: /config/restart.log
    timestamp: true

Maybe you need to whitelist the /config directory in configuration.yaml.

homeassistant:
  ...
  allowlist_external_dirs:
    - /config

Now one automation for HA shutdown

- id: "1612867115303"
  alias: HA Shutdown
  description: ""
  trigger:
    - platform: homeassistant
      event: shutdown
  condition: []
  action:
    - service: notify.restart_notify
      data:
        message: shutdown
  mode: single

and one for HA start

- id: "1612867183386"
  alias: HA Start
  description: ""
  trigger:
    - platform: homeassistant
      event: start
  condition: []
  action:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ 'shutdown' in states('sensor.restart_log') }}"
          sequence:
            - service: notify.restart_notify
              data:
                message: start
      default:
        - service: notify.your_phone
          data:
            message: unclean shutdown
  mode: single

Now, on a normal shutdown ‘shutdown’ is written to /config/restart.log.

Home Assistant notifications (Log started: 2021-02-09T10:39:55.205115+00:00)
--------------------------------------------------------------------------------
2021-02-09T10:39:55.205146+00:00 shutdown
2021-02-09T10:40:19.670550+00:00 start
2021-02-09T10:46:33.052513+00:00 shutdown

If HA starts and ‘shutdown’ is in the last line of the log, it was a clean shutdown and ‘start’ is written to it.

If not, a message is sent to another notify service.

13 Likes

Thank you all!

@VDRainer’s suggestion was exactly what I was thinking. I’m marking it as a solution. You saved me probably a lot of time by not having to come up with the code with my noobish skills. I did also add the calculation of the power outage length. It’s now included in the notification I get.

I appreciate the suggestions to get a UPS. However, I don’t think I want to invest in one at this point. This is a sort of side project and there are no critical functions involved. Although, now that the idea is in my head, I don’t know if I can resist buying the UPS. :smiley:

1 Like

Hello guys.

I’m using an APC UPS with USB connection to my Raspberry Pi, and installed NUT on Home Assistant.
I successfully got it working to notify me via Telegram when there’s a power outage by creating an automation that triggers when the UPS sensor changes from OL (online) to OB DISCHRG (on battery discharging) like this:

I don’t care much about the Telegram notification, as I’m really doing this because I have some Osram bulbs that will turn on every time the power goes off, so I need to run this automation to make lights go off after an outage.

The problem I’m facing is that if there’s a short outage (all outages at my country are very short (less than 5 seconds and mostly by lightning during heavy rains) the NUT sensor will not notice the short change between online and on battery.

Does anyone have an idea or alternative solution to what I want to do?

Cheers!

It’s basically the same thing as I’m doing. Monitor something that is supposed to be always on. Even if you can’t get info that the thing turned off because of a power outage, as long as it does turn off, and sends info that it has now turned on, it will work.

Does the NUT sensor have anything like this? You could also add something like a Tasmota plug somewhere and monitor that. With MQTT it should send something useful when it comes back online.

Big thanks for this. I was looking for this kind of solution as well.

First time to edit yaml file and the code helped so much, almost like a walkthrough.

In the Philippines, power outages are a way of life.

I have some aqara and xiaomi devices that don’t have power or state memory and it’s a problem always trying to remember to turn on switches after power outages, more so if i’m not home. (example: pressure pumps).

Hope that an unclean shutdown can be a trigger to turn on the pumps on restart.

So many ways to do this.

Need two HA devices and a cheap UPS. Call them home and remote.

Connect UPS to remote Internet gateway and remote HA

Power goes out on remote HA – remote HA sends notification of power out

Connectivity between home and remote HA – communication failure – home HA send notification

Internet comes back, remote HA history shows what happened.

Now you need a cellular modem to enhance system further.

They are template sensors!

I came here looking for a viable solution too because like @webudo if my power outage is a blip during a storm my UPS doesn’t even see it. When I started looking at device I did see that the 2 Android phones with the companion app installed did see the blip soonest in terms of whether the phones were charging. This would generally only work in the middle of the night, and only if one of my wife and I were home. It has me considering digging out an old Android phone that I can install the HA companion app on and plug it in somewhere near my equipment. It would be nice if there was some other device that could report as fast as I see my phone reporting back to HA.

As a follow-up I have parts on order to be able to make a device out of a door/window contact sensor that will be able to instantly tell me when a power outage occurs. The idea came from the following post:

This solution wouldn’t be limited to Zigbee, however it does require that whatever connectivity that is required for the door/window sensor be active/online during a power outage. Since my RPi is connected to a UPS my Zigbee network will remain online so this will work perfectly. If you were to have only WiFi and your WiFi didn’t stay online during a power outage then this would be a problem.

1 Like

Here what I do.

I have the HA box on a UPS, the UPS has a USB cable that plugs into the HA box, I run a NUT server on the HA box, then I use the HA NUT integration to obtain the status information from the UPS. When the HA sensor reports “offline”, I generate an alert.

Yes, I have the same setup. However, a 30 second power outage during a storm won’t register in the UPS as going on battery. Try it and you’ll see this pretty quickly. That means it’s not a completely reliable way to track all power outages. Yes, for many minutes long outages you can use this, however, those are less common than blips during storms.

I have a simple solution deployed on my setup, you will need:

  1. An android device, could be a fire tablet.
  2. Fully kiosk android app and Fully kiosk Browser integration.

Make sure the device is always plugged in to power, a charging status entity (binary_sensor.fire_tablet_plugged_in) will be exposed in home assistant and you can automate off that.

Set up an automation using the built in editor or nodered to send an alert when ever the device stops charging for say 5 seconds or as needed, also set an automation when it is back up and charging.
Voila!

PS: you need a UPS for your home assistant host device and internet infrastructure.

I already mentioned that consideration of using some Android device and monitoring it’s charging status.

I’m going with the custom solution with a dedicated device just for this as to eliminate the need for a bunch more overhead like the Android OS and the HA app requirements. Eventually the Android OS and/or the HA app will end up being no longer supported and a security risk on your network. With an actual sensor & relay that is dedicated to this sort of monitoring you eliminate a ton of resources/requirements. If I already had a device on the wall as a kiosk, or was planning to go that route, I might have considered it as a temporary solution. However, the dedicated solution seems like a better option in the long run. And for like $20 in parts and a little time it’s a pretty cheap solution.

1 Like

Newbie here. My solution is I have my UPS plugged into a smart outlet, assigned to a static local IP which then powers my HA and router. I used the Ping Addon to create an entity then use that entity in an automation to check it’s entity status and do something when it detects it is disconnected.

You can get the state through NUT reliably. No need to ping a smart outlet.

That assumes you have NUT.

The UPS should have a USB port which you can plug into the computer. Once that is done setup NUT. This way you can also shutdown the computer before the UPS runs out of power if you are in an extended power outage.

Smart Plugs eventually fail. So while this is not a mission critical application (like having your furnace come on), I probably would not do it to avoid the additional failure mode.