poudenes
(Poudenes)
March 24, 2020, 5:38pm
1
Hi ALl,
Im testing a new automation. But im little confuses. The value is good. But cant understand how to let it work.
I want 1 hour before the start_time a notification. I thought this was the script how to work…
for example start time is: 14:00 so when I do -3600 is 1 hour then I get a “true” right?
But with this automation I get every hour a notification also when its more then 1 hour before the time.
- alias: "Notification - Goto Work"
trigger:
platform: time_pattern
minutes: "/1"
condition:
condition: template
value_template: "{{ as_timestamp(now()) | int < (as_timestamp(states.calendar.werk.attributes.start_time)-3600) | int }}"
action:
service: script.pushover_engine
data_template:
message: "Met een uur moet je naar je werk"
priority: "0"
sound: "bike"
title: "HA Wwerken"
1 Like
tom_l
March 24, 2020, 5:41pm
2
Change the less than operator <
to greater than >
value_template: "{{ as_timestamp(now()) | int > (as_timestamp(states.calendar.werk.attributes.start_time)-3600) | int }}"
poudenes
(Poudenes)
March 24, 2020, 6:35pm
3
Thanks
Tried it but still its sending me very minute a message.
now its 19:38 and test start is 21:00
22:00 - 3600(sec) is 21:00
So it should fire the automation …
changed it from -3600 to +3600 and now no notification. Lets see if 1 hour before start I get something …
value_template: "{{ as_timestamp(now()) | int > (as_timestamp(states.calendar.werk.attributes.start_time)+3600) | int }}"
tom_l
March 24, 2020, 6:51pm
4
That will give you an alert 1 hour after your start time.
Use the template editor to work out what is going on. e.g what does this give you?
{{ states.calendar.werk.attributes.start_time }}
tom_l
March 24, 2020, 9:12pm
6
What about:
{{ as_timestamp(states.calendar.werk.attributes.start_time) }}
tom_l
March 24, 2020, 9:23pm
8
So do the same for now() and see if you can work out why your template is always true.
It’s probably because your calendar date has no timezone information, but now() does. So you end up comparing a local timestamp to a UTC timestamp.
poudenes
(Poudenes)
March 25, 2020, 8:45am
9
tom_l:
now()
this is output:
2020-03-25 09:44:36.148851+01:00
Im happy you help me. Many scripts I copy try to understand. But if its goes wrong im lost… haha
https://www.epochconverter.com this link can show me from timestamp to human readable text
then only what I can think is change the )+3600) part with the extra more seconds to make the both outputs equal ? and then add the extra seconds to go 1 hour before the time…
tom_l
March 25, 2020, 9:04am
10
That could cause you issues if your location has daylight savings time. Does it?
poudenes
(Poudenes)
March 25, 2020, 9:25am
11
ohh damn, yes we have… more code troubles… haha
tom_l
March 25, 2020, 9:30am
12
Instead of using as_timestamp(now())
use as_timestamp(utcnow())
then you should be able to compare that to the calendar timestamp.
poudenes
(Poudenes)
March 25, 2020, 9:34am
13
tom_l:
as_timestamp(utcnow())
the utcnow() show this time. its 1 hour later then here now its “2020-03-25 10:33:27.180974+00:00” in NLD
“2020-03-25 09:33:27.180974+00:00”
poudenes
(Poudenes)
March 25, 2020, 9:41am
14
add this in develop tool at 10:36:01 NLD time.
Calendar time 14:00
add -12180 thats 203 minutes before 14:00
"{{ as_timestamp(utcnow()) | int > (as_timestamp(states.calendar.werk.attributes.start_time)-12180) | int }}"
"{{ utcnow() }}"
output was
"False"
"2020-03-25 09:36:01.275422+00:00"
at moment it went 10:37:00 this was output
"True"
"2020-03-25 09:37:01.275422+00:00"
so -3600 should be work for 1 hour before 14:00 then?
tom_l
March 25, 2020, 9:43am
15
That is what it is supposed to be. It will be 2 hours in daylight savings time.
Put this in the template editor:
hours difference = {{ (as_timestamp(states.calendar.werk.attributes.start_time) - as_timestamp(utcnow()) )/3600 }}
It should return the number of hours diffrence between your start time and now.
poudenes
(Poudenes)
March 25, 2020, 9:45am
16
output
hours difference = 3.2548936972353193
tom_l
March 25, 2020, 9:46am
17
Is that correct?
Is it 3.25 hours until you start work?
poudenes
(Poudenes)
March 25, 2020, 9:47am
18
yes it is. now 10:45 so plus 3.25 makes 14:00
(.25 means 15min I guess, quarter of a hour)
But when daylight saving change I need change the script again?
tom_l
March 25, 2020, 9:49am
19
No, we are using UTC times, which are unaffected by daylight savings.
poudenes
(Poudenes)
March 25, 2020, 9:50am
20
Clear thanks! Thank you for the help! I get every dat little smarter. Not only my house but also me as person