Automation on homeassistant shutdown event no longer working

My shutdown automation is not triggering. I just tried adding an input_text.set_value to it but I noticed it’s never been triggered.

alias: HA Shutdown
description: ''
trigger:
  - platform: homeassistant
    event: shutdown
condition: []
action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.reset_multisensor
  - service: input_text.set_value
    target:
      entity_id: input_text.saved_destination
    data:
      value: '{{ states("input_select.destination") }}'
mode: single

the boolean is also not on.

Working for me (HAOS 7.1, HA 2022.12.10)

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

How are you shutting down HA? I’ve noticed the only way to get any kind of consistency using the ‘Shutdown’ trigger is when shutting down HA from-within HA.

NOTE: I run HA inside VirtualBox on Windows so YMMV.

I’m running Supervised Home Assistant OS on the Home Assistant Blue ODROID-N2+
I always do it from here…

image

That’s how I restart too. So the difference is I’m running HAOS where as you are running supervised.

What operating system are you using?

I looks like only notify actions are executed when the shutdown event is triggered.

I’ve added some additional automations for testing. All automations has been triggered and I received all notifications. But the input_booleans hasn’t been turned off.

And although the notifications has all been sent the automation overview shows all the automations as never triggered.

  - alias: "Home Assistant - Gestoppt"
    id: "home_assistant_gestoppt"
    trigger:   
      - platform: homeassistant
        event: shutdown         
    action:
      - service: notify.telegram
        data:                  
          message: "Home Assistant wurde gestoppt."
      
  - alias: "Home Assistant - Gestoppt (Test)"
    id: "home_assistant_gestoppt_test"
    trigger:   
      - platform: homeassistant
        event: shutdown      
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.wetter_warnungen_einschalten
          
  - alias: "Home Assistant - Gestoppt (Test 2)"
    id: "home_assistant_gestoppt_test_2"
    trigger:   
      - platform: homeassistant
        event: shutdown     
    action:
      - service: notify.telegram
        data:                  
          message: "Home Assistant wurde gestoppt (Test 2)"           
      - service: input_boolean.turn_off
        entity_id: input_boolean.uhr_einschalten
		
  - alias: "Home Assistant - Gestoppt (Test 3)"
    id: "home_assistant_gestoppt_test_3"
    trigger:   
      - platform: homeassistant
        event: shutdown     
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.besuch      
      - service: notify.telegram
        data:                  
          message: "Home Assistant wurde gestoppt (Test 3)"    		
1 Like

Try this:

      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.uhr_einschalten

Or this:

      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.uhr_einschalten

None of the services for the input_boolean is triggered. Only the notification is sent although it is the last service in the automation.

  - alias: "Home Assistant - Test Shutdown"
    id: "home_assistant_test_shutdown"
    trigger:   
      - platform: homeassistant
        event: shutdown     
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.test_a        
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.test_b      
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.test_c          
      - service: notify.telegram
        data:                  
          message: "Home Assistant wurde gestoppt."    

It seems that at the time the shutdown event is triggered only the notify service works and all other services has already been stopped.

1 Like

Was scratching my head with this too :frowning:
No other idea than create my own Restart HA button with script doing all the housekeeping and then finally call

service: homeassistant.restart

Clumsy but it works :slight_smile:
Best, JR

1 Like

Thanks for the idea!

Yes, that’s one way. The only downside is that it won’t work when you upgrade HA core version, then it will reboot by itself anyway.

I know this is an old post. I will say that I too wanted a work around for myself. So I was thinking why not generate the TTS file ahead of time. Copy it to where the rest of your media is stored and play it like the other sound files. I think the problem is tts itself is not able to receive the file because the system is being shutdown. But if your other sounds work then you can still cast before shutdown has completed. Just a thought, I’m going to try this myself and get back to you!

I also have been wondering if the reason for shutdown is logged and whether or not it could be included in the message.

Just as my previous speaker, I also know that this is an old post.

But for me too, automations listening to the shutdown event are not triggered. I’m trying to set an input boolean prior to shutdown so I know whether HA has been shutdown properly or by a power outage.

Not sure if this is indented behaviour. I would assume that HA would wait for automations that depend on this event to finish before actually beginning the shutdown process. (Maybe add a time threshold so automations cannot block the shutdown indefinitely)

Happens to me too, setting input booleans in the shutdown event doesn’t work, using 2023.3.5

If anyone else has a similar need, you can create a Trigger-based Template Sensor that indicates, on startup, whether Home Assistant had been shutdown properly or had been interrupted (such as by a power outage).

I ended up creating a binary sensor and an automation to implement the same functionality

automation:
 - alias: Power loss detection - Update current time
   initial_state: true
   trigger:
     platform: time_pattern      
     seconds: "/30"
   action:
     - service: input_datetime.set_datetime
       target:
         entity_id: input_datetime.power_loss_heartbeat
       data:
         time: "{{ now().strftime('%H:%M:%S') }}"

  - alias: 'Power loss recovery'
    initial_state: true
    trigger:
      platform: homeassistant
      event: start

    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: binary_sensor.power_loss_detected
          state: 'on'

    action:
      - service: script.alexa_notify
        data:
          alexa_device: media_player.kitchen_echo
          notification_type: announce
          current_volume: >-
            {{ state_attr('media_player.kitchen_echo', 'volume_level') }}
          message: 'Power loss detected, restoring state to normal'

binary_sensor:
  - platform: template
    sensors:
      power_loss_detected:
          friendly_name: Power loss detection sensor
          value_template: >-
            {{ as_timestamp(now()) - as_timestamp(states('input_datetime.power_loss_heartbeat')) > 120}}
          device_class: problem 

Hoping anyone would find this useful.

There’s no need to use a Time Pattern Trigger to repeatedly update an Input Datetime every 30 seconds. All that’s required is to detect the homeassistant_stop and homeassistant_started events using a single Trigger-based Template Sensor (see linked example posted above).

A Trigger-based Template Sensor and one automation
versus
A Template Binary Sensor, two automations and an Input Datetime

1 Like

Probably right, I gave it a go and it seems to be working for the shutdown case, I’ll give it a go and do a real-life simulation tomorrow, thanks for insisting @123 :slight_smile:

- alias: HomeAssistant shutdown notification
  trigger:
    platform: homeassistant
    event: shutdown
  action:
    - service: media_player.volume_set
      data:
        entity_id: media_player.dining_room_speaker
        volume_level: 0.4
    - delay: 00:00:01
    - service: script.house_announce_diningroom
      data_template:
        tts_msg: "Home Assistant, System shutdown initiated"

I’m the OP of this thread. My shutdown notification from my original post (quoted above) has started working again today, after updating to Home Assistant OS 11.2 (from 10.5) :partying_face:

this is due to a fix in core 2024.1 , not perse the OS update to 11.2

1 Like