Template sensor time

Hello. I’m new and I’m learning. I beg your help to subtract some time in automation from the next wake-up call sensor. I’d like to turn on the heater in front of the alarm.

{{states(‘sensor.p30_lite_xxxx_next_alarm’) ***_minus 1 hour 17 minutes }}

I deed for your help

{{ states('sensor.p30_lite_xxxx_next_alarm') - timedelta( minutes = 77 ) }}


{{ states('sensor.p30_lite_xxxx_next_alarm') - timedelta( hours = 1, minutes = 17 ) }}

I knew it was going to be very easy for someone.
Thank you.

Timedelta is a new-ish feature. It was introduced in the WTH month release (v0.115). I thought it was about time I leaned to use it :slight_smile:

I think I’ll add a couple of examples to the docs for people who don’t know Python.

If I want time after alarms can I use a negative number?

No, just add the timedelta.

e.g. 77 minutes after next alarm:

{{ states('sensor.p30_lite_xxxx_next_alarm') + timedelta( minutes = 77 ) }}

Oh, Sorry. :grin: I understand now.

So unfortunately, I’m doing something wrong.

What is the exact state of sensor.p30_lite_xxxx_next_alarm ?

sensor mobile app with the name sensor.mobil_name_next_alarm_clock

State. Value. What it contains.

I’ve been trying for ages to convert your sensor value to a datetime object and so far have not had any success. Maybe someone else can help.

{% set timedate = states('sensor.p30_lite_xxxx_next_alarm')|replace('T', ' ')|replace('Z', '+0000')|replace('.000', '')%}
{{ strptime(timedate, '%Y-%m-%D %H:%M:%S%z') -  timedelta( minutes = 77 ) }} 

Also please don’t post screenshots of text. I’m lazy and prefer to cut and paste the state rather than typing it all out. All you had to paste was 2020-11-25T17:56:00.000Z

Ok. Put it off. The template is not working.
Thank you for your time.

Use this format


On mobile, can’t try it

verified, %Y-%m-%dT%H:%M:%S.%fZ works.

{% set t = states('sensor.p30_lite_xxxx_next_alarm') %}
{% set t = strptime(t, '%Y-%m-%dT%H:%M:%S.%fZ') %}
{{ as_timestamp(t + timedelta(hours=1, minutes=17)) | timestamp_custom('%H:%M', false) }}

The as_timestamp() function is fairly clever and is able to convert that datetime string to a timestamp.

This should work

{{ (as_timestamp(states('sensor.p30_lite_xxxx_next_alarm')) - (77 * 60)) | timestamp_local() }}

It works, only the result is wrong.

this whole new templating thing is starting to be so confusing… I have an explicit timestamp sensor, which is now seen as a number:

seems that because of that, I can do any of the manipulations suggested above?

Uh nextalarm timestamp was always a timestamp :wink: I know this because I helped you write it

Yes… that works