Configuring an automation trigger for HASS shutdown

Hi,

I’d like to inform in Alexa (Alexa TTS custom media player component) that HASS is going to shutdown

Is there any way to do it ?
I’d like to mention I use docker if it’s of any relevance

Thanks,
Ohad

automation like:
trigger:
platform: homeassistant
event: shutdown
action:

1 Like

Thanks, really obvious solution, I could’ve find it in the documentation easily…

Well, it’s almost on the first page here

True, I just didn’t think it existed so I didn’t search for it

Hello everyone. Since I updated to 0.107.7 shutdown event is not firing anymore. Is anyone else having this same problem?

- id: ha_notification_system_shutdown
  alias: ha_notification_system_shutdown
  initial_state: 'on'
  trigger:
  - platform: homeassistant
    event: shutdown
  action:
  - service: notify.notify
    data_template:
      message: >
        HA {{ as_timestamp(now()) | timestamp_local }}

        shutdown

I hadn’t used Shutdown event before but I wanted to today. It doesn’t work for me either. I have a simple automation that should trigger on Shutdown, but doesn’t.

so what is the purpose of your message?
if you need help, please do it properly.

I hadn’t used Shutdown event before but I wanted to today. It doesn’t work for me either. I have a simple automation that should trigger on Shutdown, but doesn’t.

I am trying to save the State of a Cover as it resets when I restart HA.

The Automation is

- id: '1586223538070'
  alias: Z-Save Cover State
  description: Save Cover State before Restart
  trigger:
  - event: shutdown
    platform: homeassistant
  condition: []
  action:
  - data_template:
      entity_id: input_select.front_blinds
      state: '{{states(''cover.front_blinds'')}}'
    service: python_script.set_state

  - data_template:
      message: '{{states(''cover.front_blinds'')}}'
    service: persistent_notification.create

The Cover is

  - platform: template
    covers:
      front_blinds:
        friendly_name: 'Front Blinds'
        optimistic: true
        open_cover:
          service: script.blinds_up
        close_cover:
          service: script.blinds_down
        stop_cover:
          service: cover.stop_cover
          entity_id: cover.somfy

If I trigger the Automation manually, it works and I receive the notification. If I restart HA, it doesn’t trigger.

Is there a problem with running an Automation using the Home Assistant Shutdown trigger when you RESTART HA?

Sorry, should have done a Reply instead of a comment. Just wanted to know why I was having the same issue as Jose.

I think the issue here is when you save the state.
I’d suggest to change your automation so it triggers on cover.front_blinds state change (and check that `trigger.to_state.state is valid before saving the state).

The reason for that is how shutdown works - HA fires shutdown event and components can clear after themselves. Some take more time, some have nothing to do and die quickly… so it might happen that by the time you try to call set_state, that input_select.front_blinds no longer exists.
and you’ll probably won’t see that persistent_notification, too :wink:

Hope it explains something.

One more thing - why do you use python_script.set_state?

Thanks, that is what I ended up doing.

The reason I use the script is that the state of the covers are always reset when I restart HA. I have them included in Recorder and I can see the history there. But when I restart, the cover goes to Open even if it was closed.

So I store the state in an input_select whenever the state changes and then restore the state on startup.

Is there a better way or a reason the state doesn’t persist?

I don’t have any covers but just a thought - do any of your binary_sensors restore their state after restart? I don’t think they natively do.
The only things that normally do that are input_xxx.
Don’t you think it’s a good reason?

Still - what’s the reason of using input_select to store a state (string)?

My binary sensors do restore but they are all part of ESPhome devices that maintain their states.

I had hoped Covers would restore state through Recorder but that doesn’t seem to work.

I can set the available options (open/closed) and set the input_select to the correct state to save. I suppose an input_text would work as well.

exactly, because an idea behind sensors is they reflect state of something external so that external should take care it’s preserver after restart.
one of popular was is to use MQTT sensor and publish current state with retain flat so the state of such sensor will be restored by HA correctly provided your MQTT broker is up and running. maybe that’s a good alternative?

I doubt there is direct link between storing states and restoring them despite the fact they have the same root :wink:

I think it should. Actually, as it’s open/close (i.e binary), use input_boolean. And set its state by its service calls, set_state is not for that.

MQTT may be the best option. But I still need to restore the state after a restart. input_boolean would need to provide open/close back to the state after restart. A data_template could do that.

I thought part of the reason for recorder was to restore some states.

Thanks for your explanation of why the automation doesn’t necessarily trigger on shutdown. I’m 90% there now. Just have to figure out one stubborn cover. :slight_smile:

please don’t change fonts here, the one you’ve just used is not for ordinary messages.

in case of MQTT binary sensor you need to configure it properly and to make sure an appropriate topic has a state message with retain flag. you can do that by exactly that automation - use mqtt.publish service. that way the sensor’s state will be picked up by HA on startup. and basically for all automations and fronted you’ll need to use that sensor to avoid any confusion.

Same result can be achieved by using input_boolean and it will work even without MQTT so I’d suggest to go that way. Again, use input_boolean instead of cover.front_blinds so you’ll always know the state.

Bump.

automations during homeassistant shutdown do not fire. only push notifcations, no scripts before the notification, no changing of boolean or input selects.

can anyone confirm?

 trigger:
    platform: homeassistant
    event: shutdown
  action:
  
  - service: script.turn_on
    entity_id: script.prep_reboot   
  
  - device_id: 10005beb2b
    domain: switch
    entity_id: switch.sonoff_10005beb2b
    type: turn_off

  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.safe_shutdown
        
  - service: persistent_notification.create
    data:
      title: Warning
      message: Hass shutdown
      
  - service: notify.notify
    data:
      title: Warning
      message: Hass shutdown
1 Like

Anything new about this? I have the same problem. I can’t fire the shutdown trigger of HA.

I think that it maybe fire this trigger even when reboot the HA?

1 Like

When you press the restart button, HA fires an event but immediately begins teminating things. A shutdown automation may (or most llikely may not) get triggered. Since the shutdown process is now well underway, and things happen quite quickly, your automation will fail.

The only option I see is to only use a script to restart HA and never use the HA restart button. The last thing your script would do is execute:
service: homeassstant.restart:

alias: Home Assistant - Restart
sequence:
  - service: alexa_media.update_last_called
    data: {}
  - service: notify.persistent_notification
    data:
      title: Home Assistant
      message: Restarting...
  - service: script.notify_alexa_media
    data:
      message: home assistant is restarting
  - service: homeassistant.restart
    data: {}
mode: single
1 Like