WTH Can we have an offset on date/time helper trigger in automations

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.

Being able to trigger an automation before/after the next_alarm sensor from the companion app would be a great use for this too !

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

1 Like

Hi,
I tried your automation, but the trigger doesn’t work for me


platform: template
value_template: >-
  {{ states('sensor.time') == (
  states.input_datetime.testzeithelfer.attributes.timestamp | int - 120 ) |
  timestamp_custom('%H:%M', False) }}

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):

service: template.reload
data: {}

If you prefer the full script:

alias: "RELOAD: Template Entities"
sequence:
  - service: template.reload
    data: {}
mode: single
icon: mdi:reload

But just to advise, the sensor/template/script won’t update until you have a trigger (Automation Triggers). Or see Template Sensor Triggers

Hope this helps :smiley:

Thanks, will try it later

Having the Date/Time integration added in the last version, I’d like to bump this thread.

I’ve not yet managed to get the above code solutions working.

It would be great to have this in the UI editor for ease.

There would be so many use cases. Gradually waking a bedroom light before the next set phone alarm for example.

Trigger 2 minutes before the time specified by the Input Datetime.

trigger:
  - platform: template
    value_template: >
      {{ (now() + timedelta(minutes=2)).strftime('%H:%M') == 
        states('input_datetime.example')[:-3] }}

How it works

  • 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.

4 Likes