A time trigger allows you to use a date/time helper instead of a hardcoded time. Would it be possible to add an optional offset (+/-) to this, in the same way as sunrise/sunset does.
Could be used in a situation like an alarm time (the date/time helper) and the automation can be triggered to run, say, 15 minutes before the date/time to turn something on to be ready for alarm time.
This way changing the alarm time helper adjusts the ‘alarm’ automation and the ‘turn on before alarm’ automation
And not just for the date/time helpers, It will be useful to any timestamp entity. For example running automation to notify about certificate expire two weeks before it expires.
I did a little digging and found something that works. I am using 2022.12.8 and tested (successfully) the following automation that will allow you do an offset. You can configure it however you like, but note that the “120” is seconds.
alias: Test Automation
description: "2 minutes before the value template triggers, based on the datetime helper, an audible notification will be sent to a specific echo device"
trigger:
- platform: template
value_template: >-
{{ states('sensor.time') == (
states.input_datetime.alarm_clock.attributes.timestamp | int - 120 ) |
timestamp_custom('%H:%M', False) }}
condition: []
action:
- service: notify.alexa_media_my_echo
data:
message: This is a test
data:
type: tts
mode: single
Hope that this is close to what you were looking for, and helps you out
I did create the date/time helper and set it to a time later today, so I assumed that the trigger will start the automation 120 seconds before the set time. But it didn’t
Anything else I have to do before?
Sorry, didn’t see the post until now. Is this a template sensor or automation? In any case, you’re missing the trigger that will cause the automation to run or the sensor state to change.
I haven’t used triggers on my template sensors, but if it’s done as an automation with a trigger it should work fine. As far as template sensors with triggers, there are a few sources on the HA Community Forums of folks that have utilized this method.
If you’re using it as a template sensor without a trigger it would likely only change whenever you either would restart HA or reload the template sensors. To reload the template sensors you can write a script (that is called by an automation when it is a certain time):
now() reports the current date and time as a datetime object.
timedelta(minutes=2) creates a timedelta object with a value of 2 minutes.
(now() + timedelta(minutes=2)) adds 2 minutes to the current time. The result is a datetime object.
strftime() is one of several methods of a datetime object.
strftime('%H:%M') converts the datetime object, produced by the addition, to a time string displaying hours and minutes.
states() reports an entity’s state value which is always a string.
[:-3] is used to slice off the last three characters of a string.
states('input_datetime.example') reports the Input Datetime’s time as a string displaying hours, minutes and seconds.
states('input_datetime.example')[:-3] reports the Input Datetime’s time as a string displaying hours, and minutes but no seconds.
When the current time, displayed as a time string like 07:45 is equal to the Input Datetime’s value, also displayed as a time string, then the template evaluates to true and triggers the Template Trigger.
Be advised the template is evaluated every minute (not every second).
EDIT
Correction. Need to add the offset to the current time, not subtract it.
Adding to this, as I think the use-cases are virtually unlimited, and surprised it hasn’t been implemented yet, especially since it already exists for the sun integration:
A notification 2 hours after input_datetime.wakeup value, to have your morning coffee
Dim the lights to certain value 2 hours before input_datetime.bedtime value
Turn off the TV 30 minutes after input_datetime.bedtime value, if it’s still on
I know you can achieve this with a template trigger, but that requires hardcoding the delay, and I prefer to have this in a blueprint, and pass the entity and delay as an input, based on the use-case.