Trigger 5 mins before input_datetime

I have created a helper input_datetime.fajr_time and set the time 5:10 AM
Now i want to trigger an automation 5 mins earlier at 5:05 AM.

How to run the automation bellow, 5 min earlier based on input_datetime.fajr_time?

alias: AA Fajr
description: ""
trigger:
  - platform: time
    at: input_datetime.fajr_time 
condition: []
action:
  - type: turn_off
    device_id: f2500bf16b7e9e12c0aafc77049e7663
    entity_id: 50ce6336052e0a28b9458b4af1a02a44
    domain: light
mode: single

you can’t use the datetime as the trigger. you will need to use a template trigger:

lias: AA Fajr
description: ""
trigger:
  - platform: template
    value_template: "{{ as_timestamp(states('input_datetime.fajr_time')) - 300 }}"
condition: []
action:
  - type: turn_off
    device_id: f2500bf16b7e9e12c0aafc77049e7663
    entity_id: 50ce6336052e0a28b9458b4af1a02a44
    domain: light
mode: single

@finity : I have added your code. But still its not triggering. Any suggestions?

You need to compare it to the current time.

If your helper is date-and-time use either:

{{ now().timestamp() >= as_timestamp(states('input_datetime.fajr_time')) - 300 }}

or

{{ now() >= states('input_datetime.fajr_time')|as_datetime|as_local - timedelta(minutes=5) }}

If it is time-only:

{{ now() >= today_at(states('input_datetime.fajr_time')) - timedelta(minutes=5) }}
2 Likes

Works! Thanks!

weell shoot. of course you’re right. I guess I was distracted and forgot about that part.

thanks for the correction.

2 Likes

MAN!!!

@Didgeridrew

I’ve spent more than 2 hours or who knows how many posts using who knows how many failed templates trying to make this damn trigger work. And I finally found this solution and it worked flawlessly. I never had set up one of those buy me a coffee logins… until now… but I guess your next :coffee: is on me. Thanks bro!

1 Like