I am wanting to use the offset value of an input_datetime sensor in an automation as a trigger. Essentially, I have a light that is turned on by input_datetime.night_light_on_weekday, and then 20 minutes after that, I want a different automation to run. The input_datetime.night_light_on_weekday may be manually changed from time to time, so I need to use this as the reference point to my automation offset.
WIth a template, it would appear that I can modify the time so that it will run 20 minutes after with this:
But when I do that, the automation refuses to acknowledge that this timestamp ever exists, and fails to trigger the automation. Nothing in the automation trace logs, and nothing in the system log. What’s the best way to accomplish this?
The template subtracts 20 minutes from the current date and time and reports it as a time string in HH:MM:00 format. It then compares the time string to the state value of the Input Datetime which is also a time string in HH:MM:00 format.
If the two time strings are identical, the Template Trigger is triggered.
It would be straightforward if there was no need to offset the Input Datetime’s value. Then you could use a Time Trigger like this:
trigger:
- platform: time
at: input_datetime.night_light_on_weekday
An alternative to the Template Trigger I suggested is to create a Template Sensor, whose device_class is timestamp, that subtracts 20 minutes from the Input Datetime.
template:
- sensor:
- name: Offset Night Light on Weekday
device_class: timestamp
state: >
{{ (today_at(states('input_datetime.night_light_on_weekday')) - timedelta(minutes=20)).isoformat() }}
Then the automation’s Time Trigger can simply be this:
trigger:
- platform: time
at: sensor.offset_night_light_on_weekday