MACE0ACE
(MaceAce)
November 25, 2020, 9:31am
1
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
tom_l
November 25, 2020, 9:41am
2
{{ states('sensor.p30_lite_xxxx_next_alarm') - timedelta( minutes = 77 ) }}
or
{{ states('sensor.p30_lite_xxxx_next_alarm') - timedelta( hours = 1, minutes = 17 ) }}
2 Likes
MACE0ACE
(MaceAce)
November 25, 2020, 9:47am
3
I knew it was going to be very easy for someone.
Thank you.
tom_l
November 25, 2020, 9:50am
4
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
I think I’ll add a couple of examples to the docs for people who don’t know Python.
MACE0ACE
(MaceAce)
November 25, 2020, 9:57am
5
If I want time after alarms can I use a negative number?
tom_l
November 25, 2020, 9:59am
6
No, just add the timedelta.
e.g. 77 minutes after next alarm:
{{ states('sensor.p30_lite_xxxx_next_alarm') + timedelta( minutes = 77 ) }}
MACE0ACE
(MaceAce)
November 25, 2020, 10:02am
7
Oh, Sorry. I understand now.
MACE0ACE
(MaceAce)
November 25, 2020, 10:32am
8
So unfortunately, I’m doing something wrong.
tom_l
November 25, 2020, 11:04am
9
What is the exact state of sensor.p30_lite_xxxx_next_alarm
?
MACE0ACE
(MaceAce)
November 25, 2020, 11:28am
10
sensor mobile app with the name sensor.mobil_name_next_alarm_clock
tom_l
November 25, 2020, 11:41am
11
State . Value. What it contains.
tom_l
November 25, 2020, 12:33pm
13
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
MACE0ACE
(MaceAce)
November 25, 2020, 1:05pm
14
Ok. Put it off. The template is not working.
Thank you for your time.
petro
(Petro)
November 25, 2020, 3:39pm
15
Use this format
%Y-%m-%DT%H:%M:%S.%fZ
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) }}
123
(Taras)
November 25, 2020, 4:01pm
16
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() }}
MACE0ACE
(MaceAce)
November 25, 2020, 4:14pm
17
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?
petro
(Petro)
November 25, 2020, 4:16pm
19
Uh nextalarm timestamp was always a timestamp I know this because I helped you write it