How to run automations against "now" at startup? (aka do automations keep state)

I would like to understand how automations trigger some entities in two cases:

  • the condition is a range, say “the light is on between 13:30 and 17:25, otherwise off”
  • the condition is a single event, say “the light must be switched on when the sun sets”

When HA is running while these conditions happen, everything is clear.

What happens when it is started when a condition should be met but the trigger is in the past? (it is now, at HA boot time, 14:45 - for the first case and the sun is down (again, at HA boot time) for the second case).

Specifically: are these conditions “states”, that is HA checks all the time if they are met and sets the state in accordance, or is the automation triggered only when the boundary happens (so things happen at 13:30, 17:25 and at sunset)?

If the latter: is there a way to set the state at HA boot time, so that it catches up with what the state of the entities should be?

For time triggers in the past the automations are not triggered on restart. For state triggers the states are restored and the automations trigger. e.g. Your time range + light state trigger will occur as long as the current time is in the range specified when the restart occurs. Your sunset automation won’t trigger if you restart after sunset.

Change or add additional triggers to the automations. e.g. instead of ‘at sunset’, use ‘sun is below horizon’.

The other option is to add a HA start-up automation. This will get messy fast as you add more time dependent automations you need to check and is not the best option.

I have a number of heating and cooling automations that use template binary sensors to check if time ranges (supplied by input_datetimes) are active, I use these to trigger the automations and they do trigger after a restart if needed.

It is indeed by time boundary that automations are triggered - so if HA is down when the time passes, it will not trigger. You can in fact have multiple triggers for an automation in hass - so you could use the time as well as “at start”

1 Like

That’s not a good idea without conditions in the automations. The automation will always trigger at startup. E.g. at sunset and at startup will trigger if you restart a 9am in the morning.

I mean, it really depends on the use case… If I am checking sensors to determine if it should send a push notification, I probably dont care or need that condition - but if I am turning on inside lights, I probably would want conditions.

@tom_l I’m trying to build almost identical automation.
I have a couple od input_datetime and input_numbers that I want to use to build a simple scheduler.
Could you share your config? That would really help

Here you go:

      friendly_name: "Lounge AM Automation Time Active"
      - sensor.time # required to update now()
      - input_datetime.lounge_ac_am_on_time
      - input_datetime.lounge_ac_am_off_time
      - input_boolean.lounge_ac_am_automation
      value_template: >-
        {% set d = now().strftime("%Y-%m-%d ") %}
        {% set t = now().timestamp() %}
        {% set am_start = strptime(d + states('input_datetime.lounge_ac_am_on_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {% set am_end = strptime(d + states('input_datetime.lounge_ac_am_off_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {{ (am_start <= t <= am_end) and is_state('input_boolean.lounge_ac_am_automation', 'on') }}
1 Like