I’m not really sure I’m following your questions. But I think this might help. When you define an automation, if you don’t define initial_state, then its state at startup will be whatever it was before the restart.
To be more clear, an automation is either on or off. When it is on the triggers and conditions will be evaluated as necessary and the actions will run when the automation is “triggered.” When the automation is off the triggers and conditions will not be evaluated and the actions won’t run (unless manually triggered.)
The on/off state of an automation will be restored after a restart if you do not specify an initial_state in the config. But if you do specify initial_state, then that will control if the automation is on or off after a restart.
Does that help, or did I completely misunderstand your questions?
What you’re asking is that HA would repeat any time based automations to set the starting state?
The issue is how far do you go; many of us have automations that trigger based on events and if that event has already occurred in the past do we really want to assume that the outcome of that automation is what you want for an initial state?
I think more practical would be for HA to continually persist the state of all the entities it can modify (switches, lights, sensors, etc) so that after a restart it can resume where it was prior.
The challenge here is that could be a lot of info to continually persist on often small machines running off SD card. You’d want it kept relatively up to date to ensure current.
I didn’t know that. So if I have the front door light turning ON at 6PM then I have a power failure and the power comes back at 10PM, if I have a light OFF at 9PM then HA will restore the last state to ON but will not trigger the OFF even though it’s past 9PM?
Ok, that scenario explanation helps. I had no idea that’s what you were asking about. I thought you had your automations configured to be initially off, but somehow turned them on after starting HA, and then wanted them to stay on after a restart.
So, for this particular scenario, you want to add an automation that is triggered by HA starting, and uses a service template to turn the light on or off based on the time of day.
- alias: Turn light on or off at HA start
trigger:
platform: homeassistant
event: start
action:
service_template: >
{% if now().hour >= 18 and now().hour < 21 %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
entity_id: light.MY_LIGHT
In fact, you could do it all in just one automation:
- alias: Control light
trigger:
- platform: time
at: '18:00:00'
- platform: time
at: '21:00:00'
- platform: homeassistant
event: start
action:
service_template: >
{% if now().hour >= 18 and now().hour < 21 %}
light.turn_on
{% else %}
light.turn_off
{% endif %}
entity_id: light.MY_LIGHT
Which two places? In the trigger and action parts of one automation? Or are these times used elsewhere, too? The answer is kind of yes, but it’s probably not worth it.