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