I’ve done a search for this topic and found a couple of posts [1] [2] but they both suggest adding a UPS to Home Assistant, which is not what I want to do.
My user story: As someone who lives in an area that has power outages more frequently than I’d like and as someone who travels, I would like to receive a notification telling me that a power outage has occurred and how long the power was out so that I can make sure to check things like the refrigerator and other chores when I return home.
While I could add a UPS to my Home Assistant’s raspberry pi, it’s not in a convenient location to do so. Additionally, a UPS for HA (at least for me) would be over-engineering as I’d rather use the UPS on things like my desktop or internet.
Additionally, even if it was on a UPS, there isn’t much HA can do for me while the power is out besides report the status of the electricity. Anything/everything else would not be on a UPS and I’m not in a position to purchase a whole house generator.
So instead, I think getting notified when the power is back on would be a better solution. The solutions I’ve linked above also suggest a ping test. I think this can provide important additional data (e.g. The internet is available) but it’s slightly outside my specific use case. (Aside: I’m fairly sure that if the Internet is not available, I won’t receive notifications on my phone, even if I’m connected to the house wifi, correct? iirc, notifications for Androids flow through Google to be pushed to phones.)
I know that there is a trigger event when Home Assistant starts, but this won’t tell me how long the power was out.
It seems like I’ll have to create an automation to update a datetime helper every minute (or something like that) and then have a separate automation that checks to see if the datetime on the helper is more than 30 minutes old (or whatever length of time I would want to make sure to check the fridge).
Am I on the right track here? Or is there a sensor/metric that I can use that will simplify this?
From what I understand, HA isn’t terribly tolerant of power outages and that eventually it’s likely to result in issues/corruption. I think that’s why most of the posts you’ve found mention UPSs. One other option along these lines is using a laptop to run HA. I had an old budget one literally lying in a closet; it was a significant upgrade from a Pi performance-wise and having a battery was an unexpected benefit. I have mine setup to automatically shutdown HA if the battery gets to a certain level; adding a notification to this would be easy since you’d already have the data to work on.
It depends on the Persistent Connection settings on your phone/app; there are multiple options ranging from never to always.
Here’s an idea completely outside of Home Assistant if you’re interested.
I have a Ting device installed in my home.
It has a subscription fee, but I ordered it via my Home Insurance (State Farm) and they’re picking up the tab for it - I think, I read that others might as well.
When my power or internet go out, I receive notifications like this - it even tells me when my backup power kicks in and stops again which is irrelevant for you but a nice feature for me on top of the other info:
You can artfully use the Home Assistant trigger in an automation to approximate as closely as possible when Home Assistant has restarted after a power outage.
For example, create the following housekeeping automations:
On a stop event, flip an input_boolean to False.
On a start event, flip the input_boolean to True and save the current date and time in an input_datetime.
Then expand the automation in #2: before changing either of the helpers, check whether the input_boolean is already True. If so, that means Home Assistant wasn’t shut down gracefully (or it would have, in #1, set the input_boolean to False).
The state of the input_datetime will tell you the last time that HA started up. That’s not exactly what you want, but you can add a third housekeeping automation to update this input_datetime, say, every minute, or every 5, or whatever resolution you care about to estimate how long power was out. If you do this, upon restarting from a power outage, you’ll at least know the last time that your automation updated the input_datetime.
It’s not perfect: it will false-report a power resumption if Home Assistant restarts after a crash, for example.
But, more fundamentally, since you report that you have frequent power outages, it is a bad idea to run Home Assistant without any battery backup. You will eventually find that your system is corrupted, whether at the application level or at the operating system level. Writing an automation to report power outages and overlooking this critical risk is foolhardy.
As a bonus, if you do use some kind of battery backup, you should be able to purchase one that communicates with the host computer for your Home Assistant–and then you can actually detect power outages (because you’ll know when the UPS has kicked in, and then when it has kicked off).
I have an old Android phone permanently on charge. When the power goes off an automatic process sends me a text, when the power comes back another sends me a second text. There are several apps which will do this - Llamalab Automate, for example, or Tasker.
The phone has its own battery, of course, so it can handle an outage of 24 hours or so. No connection with HA needed, though I believe Automate and Tasker both have integrations.
I’ve actually been working on power monitor device for the last couple of weeks that will send me a pushover message when the power has been restored to my home. The device consists of an ESP-01S microcontroller connected to a single relay board which is connected to a zigbee door sensor. The ESP-01S is powered by a 5V USB adapter that is plugged into one of my home’s AC outlet. So when the device is powered by the home’s mains power, the relay switch is closed and the zigbee door sensor reports to Home Assistant that it is closed and that indicates the home has mains power. When the power goes out in the home, the ESP-01S becomes unpowered and the relay opens up which sets the zigbee door sensor to “opened” and that notifies Home Assistant that the mains power is out. I have my Home Assistant (raspberry pi 5), cable modem, and wifi router all connected to a UPS which can stay powered for about 90 minutes. I have an automation that waits for 60 minutes and if the mains power isn’t restored (as indicated by the zigbee door sensor), it will initiate a shutdown of Home Assistant and the Raspberry Pi. I do this because I want Home Assistant and my Raspberry Pi to shutdown gracefully instead of suddenly which could result in corruption.
Now, when the mains power comes back on, the ESP-01S powers up as well as my raspberry pi with home assistant and the cable modem and wifi router. The ESP will be the first to boot up and be active, and I’ve programmed it to wait until it gets a good wifi/internet connection and then send me a pushover message to my smartphone. That way, I’m notified when the power has been restored. I also have it wait for about 5 minutes before it flips the relay that will set the zigbee door sensor to “closed” to let Home Assistant know that the power has been restored in the home. I do that, because if the door sensor is set to "closed’ before Home Assistant is fully booted up, it doesn’t seem to see the current status of the door sensor. My understanding is that Home Assistant doesn’t poll battery-powered devices for their status, but waits until these devices report in.
Below is a picture of my circuit. I built this device so if I’m away from my house for work or vacation, I will be notified when the power has been restored. This is done indpendent of Home Assistant, in case my Raspberry Pi doesn’t boot up and Home Assistant isn’t running.
You could use healthchecks.io. You ping the service regularly (for free) and if the pings don’t arrive (eg. power out), you get notified by their service. Similarly you get notified when it’s back up. There’s also a HACS component to bring the state of all the checks into HA (which is obviously a bit useless when you have no power).
Seems nice, but there is a 30 minute window on which the Pi won’t boot. (if power is restored after shutdown but before the UPS runs out. If the Pi would support WOL you could make the ESP01 send a WOL packet, but I don’t think the Pi supports this.
Interesting problem.
What if UPS connected to PI via a relay(normally closed) controlled by ESP. When shutdown PI the ESP turns relay to open. You could determine when PI shuts down by measuring voltage across the UPS battery on the ESP.
If esp measures battery voltage going up it can turn relay on to repower PI. If ESP shuts down to lack of power the relay will turn on but PI wont power on as USP wont be supplying power.
There will need to be discharge protection on UPS cells so definite shut off of power to ESP before battery can turn on Pi but not enough to run it.
If ESP powers back on and after few mins it hasnt detected HA running but UPS voltage is good you could run a script on ESP to turn relay open then closed again to restart PI.
Make a datetime helper and make an automation that runs each minute and checks that datetime helper and if the value is over your limit of difference between current time and datetime helper value.
If it is over the limit send a notification and then update the datetime helper to current time.
If it is not over the limit then just update the datetime helper
The RPI sits in a utility closet with the rest of my networking stuff. I could put a UPS on the router, RPI, network switch, etc. but again nothing else in the house would work.
“Frequent” isn’t the right word. I think I said “more frequent than I’d prefer”. In an average year, I suffer maybe 5 outages? My concern here is more that it will happen while I’m traveling.
Having a UPS is not a bad suggestion from a corruption standpoint. That being said, the value for Home Assistant for me is less historical than the automation and configuration itself. While the database and other information is a nice to have, I don’t consider it critical to my needs. To say it another way, my RPO for data is 3-4 weeks and if I lost all of it today, I’d be annoyed, but okay.
My configuration, etc. is all backed up on github, which is more my concern than anything else.
I know that HA is working on a backup to anywhere and once it does that, I’ll do regular backups to S3.
I love this but it’s probably outside my skillset! Do you have the schematic available?
I do t think HA has issue on power outages but you will not have data for that period. It cannot tell when power went out because it was not running to register change.
If your running in Pi, a used laptop may provide UPS power and better performance.
Yes, easy to send yourself something like a message on telegram when HA starts up. You can go back and look at history of various sensors and see the period there were no new readings.
If the RPI, the network switch and the router are working, and your sensors not, than you can make a automation that send you a message that some sensors are offline, or if you measure the voltage from your power line, you can make a notification that voltage is zero.