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.