Add event for HA Restart (to correctly detect power loss)

After many power outages I want an automation to check all the lights, as almost all devices end up in the “on” state.

Unfortunately, Z2MQTT doesn’t detect correct state after power loss, so a light that’s on is seen as off.

To detect power outage, I wanted to set a boolean helper to “on” on HA shutdown trigger, check that on HA startup trigger. If “on” do nothing, if “off” reset all my lights and finally set the helper to “off”.
In that case after restart and helper is in “off” state, this must be due to power loss.

Unfortunately, the shutdown doesn’t get triggered when restarting HA (after updates etc). I think the event only fires when HA OS is restarted. So there’s no way to detect a power loss or clean restart.

I would like an extra event to trigger on (HomeAssistant > restart)

If you loose power, without a UPS, Home Assistant stops without warning. You cant pull the power on any pc and expect it to do anything before dying.

1 Like

That’s not what I expect. Darn, I tried to be clear in one go :wink:

I want my own helper to be in a normally “unclean” state. When there’'s a power loss, after startup I check that and it should say “unclean”. I can act accordingly then. But when I restart manually, I can set it to a “clean” state. Then at startup it’s detected as “clean” and I can set it to default “unclean” again.

However the shutdown event isn’t fired at a frontend/supervisor restart, that’s what I’m missing. So an extra event which fires at a manual restart (not shutdown, just a restart from the interface) would do the trick.

1 Like

The restart in developer tools > yaml is sending the shutdown event, but indeed, an update doesn’t seem to do that.

I tried it from Settings, System. Also did it from Developer tools just yet, but either way my automation isn’t fired.
The thing is, shutdown isn’t triggered at restart, but startup is. So there’s a difference there and maybe there’s a reason for it. In that case an extra event would be sweet.

So this trigger does not happen?

    platform: homeassistant
    event: 'shutdown'

What exactly is a “frontend/supervisor restart”?

Can you describe how it occurs?

I’m sorry, I don’t really know the differences in all the HA versions, just because I have a standalone mini-PC with the generic image installed since day one. So I don’t know where to draw the line between OS/frontend/supervisor etc. And that’s why I was thinking the shutdown only fires when the OS/host machine is rebooted, because my automation doesn’t work. I’m probably wrong :sweat:

That aside, here’s my simplified automation. It doesn’t run when I restart HA from the interface. So from Developer Tool > Restart or Settings > System > Restart

alias: HA shutdown
description: ""
mode: single
  - platform: homeassistant
    event: shutdown
condition: []
  - service: input_boolean.turn_on
    data: {}
      entity_id: input_boolean.clean_power_cycle

That should definitely work.

It works for me.

- id: affb7625-0dc2-4075-88a9-6eae3563af6c
  alias: 'Shutdown'
    platform: homeassistant
    event: 'shutdown'
  - service: notify.telegram_system
      title: '⚠️ *System shutdown*'
      message: >
        Home Assistant is shutting down.

        {{ now().strftime("%H:%M:%S") }}



Yep, mine also works just fine, and looks similar;

- id: homeassistant_shutdown
  alias: "HomeAssistant shutdown"
  mode: single
  max_exceeded: silent
    - platform: homeassistant
      event: "shutdown"
    - service: notify.own_devices
        title: "HomeAssistant"
        message: "🟠 HomeAssistant will reboot"

The only differences I can see between @tom_l and my automations compared to yours, it that you don’t have an id, and you have conditions. Can you add an id to the automation and/or remove conditions?

@tom_l @khenderick You are right, when I setup a notification it works.


  1. the shutdown is not traced (nog logbook entries)
  2. the service input_boolean.turn_on (or off) doesn’t work for me

Based on both 1 and 2 I concluded the shut down event isn’t triggered.
To inspect the state of my input boolean after restart, I set it to reverse (so turn_off at restart), but after the restart it’s still in the same state. So therefore conclusion no. 2

How is your test on that one?

Since this automation is running when the system is shutting down, it seems a bit of a hit-and-miss if I read some topics on the forum.

One thing I’m thinking about; is there an initial value configured for your input_boolean that would cause to overwrite the value set by the automation?

1 Like

No, I haven’t done that. I just created a helper from the interface, that’s all.
My conclusion is backed up by the fact that I tried to reverse the usage of the helper (so set it off, but then it remains on).

So either way this is a buggy situation. The notification does work, but setting an input boolean does not. Or, maybe, the state preservation is in the way of this? Runs and saves to database before this automation somehow?

What I’d recommend is to consider solving a different problem first, which is the PC does an uncontrolled shutdown when you lose power. Eventually, this will cause disk corruption, history corruption, etc. Likewise, if you lose power when you are not home, it’d be nice to get notified.

Get an UPS with a USB port, put your PC and router / modem on it, plug the USB port into the PC and get a NUT server setup. In HA run the NUT client. You will then get notified when power is lost. Also if it’s a short power fault, the system won’t shutdown and your zigbee stick won’t reboot also.

I have several of these, to keep camera systems, networks, etc. online

APC UPS Battery Backup Surge Protector, BE650G1 Surge Protector with Battery Backup, Dataline Protection, Backup Battery Power Supply


Thanks @PeteRage. Lately we are facing serious power outages due to an old electricity network somehow. Most of the times (4 times in the last 2 weeks) they last for 2-4 hours. As I can’t do anything about it, I don’t care about being notified.

Your recommendation is another way of solving the same problem. Indeed a UPS helps in shutting down HA in the proper way. Otherwise I don’t need one, so for me that feels like buying a truck to deliver a letter to my neighbor :upside_down_face:. And it’s another power consuming device.

Most browsers these days have a mechanism of detecting an unclean close down, so they let you restore your open tabs after power loss. In my opinion HA could offer the same thing or at least let me automate it myself. Apparently the way I thought of doing it, is not working as I think it should.
If this indeed is a bug, I think many people would benefit from a solution.

I read all the posts twice (yes, I swear I did! :smiley: ), but still don’t get, what you’re after.

Why do you want to have all this on the shutdown, and not at the next start? Let’s stay with the lights as an example. If you want to control what is really happening with your lights, it doesn’t matter what state they were in at the power outage, what matters is, what state they are in at the next start. You should have a check on the start of HA if all the things you want to control have the state you want.

Setup a list, where the states you want your lights to be in after a restart is described. After a restart you check the real states against this list and act accordingly.

Or am I totally wrong here? If so, please give me a hint why. :rofl: Really, I don’t get it. I would always try to look what happens after the start, not before the shutdown. :slight_smile:

@paddy0174 I understand you get confused :wink: At first this was a feature request to add an extra event. As it seems however, it’s only a buggy situation.

I’m coming from this one Best way to check Zigbee lights after power outage? - Configuration / Zigbee - Home Assistant Community (

To be short: after power loss, all my lights come up “on”. That wouldn’t be so bad, I have automations in place to set the lighting the way I want. However, most Zigbee devices don’t report the actual new state to HA after power loss, so in my dashboard and automations, lights appear to be out when they’re actually on.
I think it’s because after power loss, the lights are instantly on and HA needs a minute to start and also setup the Zigbee network.

Now you could think to switch them off in any case, but they don’t switch off because HA or Zigbee2MQTT thinks they are already off, so nothing happens. Until I reset the situation by switching switch on all lights and then switch em off again. And then to whatever state I want them to be.

The problem is, after a manual restart from the front-end (for example an update), I do not want to reset the lights al the time, but there’s no state to detect if we come from a normal restart or a power loss.

If power losses are usually > 1h, you could also do that with an input_datetime in which you set the current datetime every 5 minutes using an automation. Another automation, on homeassistant.start then checks this and knows how long you’ve been offline. If > 1h, you consider it a power loss, and do you magic.

1 Like

@khenderick That’s a nice idea. However not a one size fits all. Problem is still that HA does not what I expect it do. Being able to run an automation at restart should not be a “you cannot call a service to set a value”

To be honest, I agree. I would suggest reporting this as a bug on github, so the development team can fix it, or maybe provide some insights in what will and will not work in the docs (in case it’s not something that can easily be fixed)

For now, it seems that a workaround will be the only option.

I also wanted to note that I think one of my posts above contained an error, as I think the event is actually raised on update, but my setup is up-to-date and I can’t verify.

Thanks for the heads up. Yes will report it as bug. And thanks for responding :sunglasses: