[SOLVED] Home Assistant start event and stop event

Hi,

I had following binary sensor defined in the configuration.yaml and I noticed today that it has stopped working.

- template:
  - trigger:
      - platform: homeassistant
        event: start
        id: "HA_START"
      - platform: homeassistant
        event: shutdown
        id: "HA_SHUTDOWN"
    binary_sensor:
      - name: "HomeAssistant up and running test only"
        unique_id: "ha_up_and_running_test_only_wM5fHTnaLG"
        delay_on: "00:00:30"
        state: >-
          {%- if (trigger.id == "HA_START") -%}
            true
          {%- elif (trigger.id == "HA_SHUTDOWN") -%}
            false
          {%- endif %}
        icon: mdi:home-assistant

This enabled binary sensor homeassistant_up_and_running 30 seconds after home assistant start event and I trigger my automations based on this state change.

Today I noticed that this binary sensor was getting turned on much earlier, even earlier than “Home Assistant started” logbook entry. So 30 seconds of delay_on is not working. At the time its triggered, I think automation engine is not running so all the automations missed this state change.

When debugging this, I noticed that homeassistant.start event triggers automations at exact time when I see the logbook entry “Home Assistant started” but when I have template based triggered binary sensors in configuration.yaml, they see homeassistant.start event about 6-7 seconds earlier.

Has anyone experienced this ? Is there any solution to this ?

Regards,
Vaibhav

I have tried using action: that was recently used as following to replace the delay_on parameter but this also doesn’t work.

  - trigger:
      - platform: homeassistant
        event: start
        id: "HA_START"
      - platform: homeassistant
        event: shutdown
        id: "HA_SHUTDOWN"
    action:
      - if:
          - condition: template
            value_template: "{{ trigger.id == 'HA_START' }}"
        then:
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
    binary_sensor:
      - name: "HomeAssistant up and running test only"
        unique_id: "ha_up_and_running_test_only_wM5fHTnaLG"
        state: >-
          {%- if (trigger.id == "HA_START") -%}
            true
          {%- elif (trigger.id == "HA_SHUTDOWN") -%}
            false
          {%- endif %}
        icon: mdi:home-assistant

With this, I run into a problem described here: Trigger Update Coordinator: Running script requires passing in a context

Actually you have it set to one minute:

Either way the home assistant started event does not fire until all integrations are loaded (or have timed out), including the template integration.

When those integrations load is totally unpredictable.

Corrected the typo.
I copied the code from when I was trying different values and formats like

Based on your statement

the home assistant started event does not fire until all integrations are loaded (or have timed out), including the template integration.

I am thinking that may be its because I don’t have non-conditional default value set for my binary_sensor which is causing home assistant to set it to On when template integration is loaded.

I now tried adding else condition to my binary_sensor:

- template:
  - trigger:
      - platform: homeassistant
        event: start
        id: "HA_START"
      - platform: homeassistant
        event: shutdown
        id: "HA_SHUTDOWN"
    binary_sensor:
      - name: "HomeAssistant up and running test only"
        unique_id: "ha_up_and_running_test_only_wM5fHTnaLG"
        delay_on: "00:00:30"
        state: >-
          {%- if (trigger.id == "HA_START") -%}
            true
          {%- elif (trigger.id == "HA_SHUTDOWN") -%}
            false
          {% else %}
            false
          {%- endif %}
        icon: mdi:home-assistant

But I still see my binary_sensor turn On before Home Assistant started logbook entry and also another binary sensor (homeassistant up and running) which is set by automation that is triggered with homeassistant start event.
First line: binary sensor which is set by automation
Second line: binary sensor set with code above

What else can I try ?
Do you know if template binary_sensors are set to On if non-conditional state is not specified in configuration.yaml ?

No that’s not it. Triggered template sensors are restored to their value before shut-down after a restart (and reload).

OK. That is what I based this binary sensor on initially but I see different results now.

My trigger based binary sensor value is NOT restored after restart.
This binary sensor is set to Off with HA_SHUTDOWN trigger but when HA restarts, I see this binary sensor turns ON even before Home Assistant start event.

If I set this binary sensor to Off with a curl command. Then restart HA. Now I correctly see that the restored state is Off.

In summary,

Other trigger based binary sensors I have correctly maintain their state across HA restarts.

Can there be a race condition causing HA shut down before recording state changes that happened because of homeassistant.shutdown event are never recorded ?
So since state changes are not recorded, they are not restored ?

Very likely, yes.

If you search the forum for “shutdown event trigger” you will find many people who have issues with this trigger. Personally I have no issue sending a shutdown notification, but others definitely do.

Can you please share code for your automation that sends notification when HA shuts down so I can try the same on my system ?

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

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

Sending notifications using shutdown trigger in automations works for me as well. My suspicion is still a race condition or something with saving states that were changed with shutdown trigger.

I tried a few things.
Looked at this post Graceful shutdown sensor

Didn’t use all of the code from this post but used homeassistant_started and homeassistant_stop.

- template:
  - trigger:
      - platform: event
        event_type: homeassistant_stop
      - platform: event
        event_type: homeassistant_started
        id: "HA_STARTED"
    binary_sensor:
      - name: "HomeAssistant up and running"
        unique_id: "ha_up_and_running_wM5fHTnaLG"
        delay_on: "00:00:30"
        state: >
          {% if trigger.id == 'HA_STARTED' %}
            on
          {% else %}
            off
          {% endif %}
        icon: mdi:home-assistant

With this code, it works. State is off when HA stops, state is On 30 seconds after HA starts. Tried 5-6 times and its consistently correct.

I don’t know why platform: homeassistant events don’t work. I have already spent few hours going down this rabbit hole so I will leave it here. It works. Why it didn’t work before, I don’t know.

This is fixed with HA Core 2024.1

Backward-incompatible changes - Home Assistant shutdown event
PR #91165

Now code from Original Post works as expected.

1 Like