#Automation to fire every hour after specific event

1 messages · Page 1 of 1 (latest)

lavish jasper
#

I have an event happening, that is stored in the input_datetime helper. I'd like to, after that, fire an automation every 1h after the event, periodically, for unlimited amount of time. What's the right approach?

  • Use template comparison and limit myself to 1 minute updates
  • Create a timer helper that has 1h expiration, fire the automation on expiration, restart the timer and start over?
balmy patrol
#

You could have two automations, one that does the thing once an hour, after creating it you disable the automation. The second automation enables the first one when the datetime triggers.
Can you explain what you're trying to achieve in the first place?

lavish jasper
#

After the event occurs, I want to trigger TTS t+1h, t+2h, t+nh (until stopped manually) via Alexa.

#

You could have two automations, one that does the thing once an hour If I do that (which I thought about too), then the TTS will happen sooner than t+xh after the event.

balmy patrol
#

Then create a second datetime helper, the automation triggers on both helpers. If triggered by the first datetime it sets the second one 1h in the future. If triggered by the second one, it does its thing and as the last step sets the second datetime helper 1h in the future again. To abort the loop you set the second datetime helper in the past

#

Only one automation needed

lavish jasper
#

sure, that's the way, same as having elapsed timer. Cool, we are aligned in the strategy

opaque topaz
lavish jasper
#

out of curiosity, from perfomance standpoint, do you know how HA evaluates the datetime triggers? Is it using cron underneath or simply polls the state every second ?

quartz jackal
lavish jasper
#

I just want to understand the underlying engine to handle datetime triggers at this point. 🙂

#

I know templates can be heavy and they are normally executed on a minute basis, unless it is entity there

humble olive
#

There's a funcition built into the event loop that HA uses that runs functions at a specific time. This is built into all abstracteventloops that python uses with asyncio. It's likely a single while loop that handles looking at the time provided to it and firing from there.

#

i.e. the code is so low and abstract it's actually outside of HA

#

you can bet it's optimized to all hell

lavish jasper
#

ok so it is done down to the practically system level (OS) runner - makes sense

#

thanks for this explanation

humble olive
#

And I haven't looked at that code either, I'm just guessing how call_at works

lavish jasper
#

very nice, that means every time the datetime helper is changed, this function must be called with new time to call the callback, which then evaluates the automations connected to this trigger

humble olive
#

Yes, I use the method that @opaque topaz described

#

Although I use a single datetime instead of multiple

#

When the "alert" is disabled, I set it to a time in the past.

#

otherwise I always just add an hour (or any increment) to the single datetime entity.

#

to do that...

- action: input_datetime.set_datetime
  data:
    datetime: "{{ now() + timedelta(hour=1) }}"
#

you could also use the trigger time if you have a long running action section

#
- action: input_datetime.set_datetime
  data:
    datetime: "{{ trigger.now + timedelta(hour=1) }}"
#

that only works with time based triggers.

lavish jasper
#

I'll have a look now