Trigger an automation BEFORE the time of a time helper

I have set up a time helper so I can easily input a “wake up” time every day.
I have 2 scripts that I’d like to call at different times related to the “wake up” time.
First a script should execute that makes the light next to my bed turn on at 1% and fade up, I have a script that does this, and I can trigger it at the “wake up” time but I’d like to set it to happen 20 minutes before the “wake up” time.
Second a script executes that slowly opens my curtains to 50% open over a couple of minutes, gradually increasing the natural light coming in. This one is easy because it just needs to happen at the “wake up” time.
I don’t want to use 2 separate time helpers as this is messy for other users to operate.
So basically I am trying to figure out how to trigger an automation with a negative time offset so it goes off before the time helper value.
I have tried adding

offset: "-00:30:00"

to the trigger but with this added the system will not allow me to save the changes to the automation. It seems to make it invalid.

Can anybody help me see what I am getting wrong?

Not sure if this is still valid, but I solved a similar task using a “helper” helper entity. Meaning, I created one input with the actual alarm time and an additional datetime entity for each automation related to it. To keep this in sync I use a script calculating the actual times for each of the related entities whenever the entity “alarm clock” is updated. Now I can use these “helper” datetime entities to trigger all my automations with a given offset to my alarm clock automatically.

Create a Template Trigger.

  - platform: template
    value_template: "{{ now().timestamp() | timestamp_custom('%H:%M') == (state_attr('input_datetime.whatever', 'timestamp') - 1800) | timestamp_custom('%H:%M', false) }}"

How it works:

It compares the current time to the input_datetime's time less 30 minutes (1800 seconds).

This part simply reports the current time in HH:MM format:

now().timestamp() | timestamp_custom('%H:%M')

This part takes the timestamp attribute of your input_datetime entity, subtracts 1800 seconds, and converts it to HH:MM format.

(state_attr('input_datetime.whatever', 'timestamp') - 1800) | timestamp_custom('%H:%M', false)

It compares the two results and triggers when they are equal. The template updates every minute because it contains the now() function.


NOTE

The assumption here is that the input_datetime is time-only.

4 Likes

Thanks for this, very helpful

omg thank you so much I searched for this solution for several months now and this finally works for my usecase