Time triggered automations: behaviour question

Should I open a feature request for this?

I set an automation with a time trigger at 8pm but my RPi rebooted between 7:59pm and 8:01pm, will the after: trigger?

And if yes, then what about this second case: automation triggered at 8pm as expected, then a reboot occurs, will it trigger again? I have existential questions about time triggering.

I’d like an automation to trigger only once, as soon as possible, when HA is available and it is past a defined time. Without an input_boolean helper set to true at startup, reset to false at midnight.

No it won’t trigger in either case.

If you want it to then you need to add a home assistant start trigger as well as a time condition.

Someone already opened a feature request for this today. IMO it is very unlikely to be implemented. Also it was poorly written and I can’t find it again.

Like this — triggers at 8pm or when HA starts, checks it’s between 8pm and midnight and that the automation hasn’t run its action in the last five hours.

alias: This time triggered automation
trigger:
  - platform: time
    at: "20:00:00"
  - platform: homeassistant
    event: start
condition:
  - condition: time
    after: "19:59:59"
    before: "23:59:59"
  - "{{ now()|as_timestamp(0) - state_attr('automation.this_time_triggered_automation', 'last_triggered')|as_timestamp(0) > 18000 }}"
1 Like

You may also be able to replace that second condition with a state condition if you are switching something on or off. e.g. if it turns a light on, check that the light is off.

1 Like

Indeed, if there are multiple reboots occurring after 8pm, it could trigger the automation multiple times, which is not desirable. So now I need to identify which part of the automation is relevant for detecting whether it has already been executed.

However, I had an idea - wouldn’t it be better to check the last_triggered property of the automation? Or will it be the current run?

That’s a very good point — I’ve just run a test to confirm, and the answer is no, the condition is looking at the previous time the automation got as far as the action:

- alias: "Misc - last triggered test"
  id: 7f1fbc7c-32fc-44dd-8669-7f9eafdfee89

  trigger:
    - platform: time_pattern
      minutes: "/1"

  condition:
    - "{{ now()|as_timestamp(0) - state_attr('automation.misc_last_triggered_test', 'last_triggered')|as_timestamp(0) > 180 }}"

  action:
    - service: input_datetime.set_datetime
      entity_id: input_datetime.alt_test
      data:
        timestamp: "{{ now().timestamp() }}"

Why? it doesn’t hurt anything to trigger an automation as long as it’s not an every second situation.

if the automation triggers but the device is already in the correct state then nothing happens.

or just put a condition to check the device state before the action runs as Tom mentioned above.

This automation is designed to transition the house into “night mode” by activating various features, such as enabling people detection with the camera, closing curtains, and activating automations based on occupancy and presence detection in certain areas. However, some parts of this automation should not be performed more than once per day (for example, it simulates a sunset in the kids’ room, it should only occur once and not again at 11:26 pm if HA were to restart). Therefore, I needed to find a solution to start it only once a day.

While checking a light on or off in the automation may not be a dependable way to determine if it has already been executed, as someone may have done so manually prior to its run, I have now a reliable condition to detect if it has been started (at least partially) thanks to the solution provided by @Troon and the additional testing done on my last_triggered idea.

Thank you @Troon and @tom_l for the precious help.

Sorry, I got the impression that it was just a simple automation. since you didn’t post more of the requirements I didn’t know there was more involved than just “did it turn on x?”.

Never mind…

No problem, I didn’t mention the automation details because my question was primarily focused on the behaviour of a time-triggered automation rather than the automation process itself.

More like a rhetorical question to learn more about HA every day :slight_smile:

1 Like