Datetime calculation

Hey guys!

I have a wake-up alarm configured as a datetime.

I like to trigger some automations (heating and lights) a specified time before the alarm time.

I tried a template sensor that subtracts 1hour (or a time specified in a separate datetime) from the alarm but this do not work.

Can someone tell me how to fix this or how to do it with a template trigger?

  - platform: template
        friendly_name: 'badheizung_startzeittest'
        value_template: '{{ (states.input_datetime.alarm.state | timestamp_custom("%H:%M:%S")) - (states.input_datetime.badheizung_vor_wecker.state | timestamp_custom("%H:%M:%S")) }}'

Thanks in advance

Just do a forum search on “wake-up” to get a bazillion of examples.

1 Like


thank you for your answer.
You´r right there are millions of anwers for that question, and that was the problem :slight_smile:

For now I will do it with a simple template trigger that triggers 3600 seconds before alarm (input_datetime)

{{ states('sensor.time') == (state_attr('input_datetime.alarm', 'timestamp') - 3600) |timestamp_custom('%H:%M', false) }}

thanks to the community

So you decided to ask the same question to add another identical answer. Interesting :joy:

Hey @koying !
Sorry for double asking

But I didn´t get the answer till now.
Asked for a template sensor and now is a template trigger → same function :slight_smile:

thank you


If you wish you can replace the reference to the sensor.time entity with now() like this:

{{ now().ctime()[11:16] == (state_attr('input_datetime.alarm', 'timestamp') - 3600) |timestamp_custom('%H:%M', false) }}

Thank you!

And how to use this as a sensor?

It could be used in a Template Binary Sensor. It would report on the moment the current time matches the input_datetime’s adjusted time (i.e. minus 3600 seconds) and off for all other times.

However it’s already suitable for use as a Template Trigger for an automation.

If the goal is to display the adjusted time in a Template Sensor, simply remove the entire left half of the equation and just use the right half.

Thank you so much

Yes the goal is (was) to display the time till the alarm time.
So I think of a calculation like time now minus alarm time.
Then I could use this for trigger

But this way works fine as well, just without displaying the time left

Greez from Vienna