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 ?
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:
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
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,
Restart HA → HA_SHUTDOWN event trigger → State Off → Off state NOT restored after HA restart
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 ?
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.
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.
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.