Time_stamp and 1 hour before

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

Change the less than operator < to greater than >

value_template: "{{ as_timestamp(now()) | int > (as_timestamp(states.calendar.werk.attributes.start_time)-3600) | int }}"

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 … :frowning:

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 }}"

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 }}

This is the output:

2020-03-24 22:00:00

What about:

{{ as_timestamp(states.calendar.werk.attributes.start_time) }}

Than I get this output:

1585083600.0

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.

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…

That could cause you issues if your location has daylight savings time. Does it?

ohh damn, yes we have… :wink: more code troubles… haha

Instead of using as_timestamp(now()) use as_timestamp(utcnow()) then you should be able to compare that to the calendar timestamp.

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”

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?

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.

output

hours difference = 3.2548936972353193

Is that correct?

Is it 3.25 hours until you start work?

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?

No, we are using UTC times, which are unaffected by daylight savings.

Clear thanks! Thank you for the help! I get every dat little smarter. Not only my house but also me as person :smiley: