ltechdnb
(LTechdnb)
May 4, 2023, 8:39pm
1
Hi,
I’m having issues with timers being one hour out of sync, I’m in the UK GMT with DST.
Wake Window Timer via dev tools states finishes at 21:31
duration: '3:00:00'
editable: true
finishes_at: '2023-05-04T21:31:48+00:00'
remaining: '2:25:04'
restore: true
icon: mdi:baby-face
friendly_name: Wake Window Timer
But the timer itself states;
Attributes
Duration
3:00:00
Finishes at
May 4, 2023 at 10:31:48 PM
Logs are and all other aspects of HA are showing correct time - Just this that is out and casuing issues when trying to report\use via jinja.
Via templates I get this;
{{ now() }}
{{ utcnow() }}
Result type: string
2023-05-04 21:36:05.746473+01:00
2023-05-04 20:36:05.746483+00:00
Any help would be appreciated.
Thanks
WallyR
(Wally)
May 5, 2023, 6:23am
2
You need to pay attention to the + in the timestamps.
21:31+00:00 is 10:31pm in GMT with DST active, because GMT with DST is +01:00
ltechdnb
(LTechdnb)
May 5, 2023, 8:14am
3
Thanks bud. I do see that in the templates as well - sorry noob over here but do you know how I would resolve it?
This is one of my usages;
service: input_datetime.set_datetime
entity_id: input_datetime.wake_up_time
data_template:
datetime: "{{ state_attr('timer.wake_window_timer', 'finishes_at') }}"
Thanks again
Troon
(Troon)
May 5, 2023, 8:31am
4
There’s nothing to resolve — it’s just different representations of the same time.
If you’d like the time formatted in a specific way in your front end, then provide full details including hte code that’s generating it.
Note that you should use data:
instead of data_template:
these days.
ltechdnb
(LTechdnb)
May 5, 2023, 9:17am
5
Thanks for your time and comments mate.
So I have a 3 hour timer via a helper. I’m looking to send a notty to my phone when it starts the finish time. I have the below so far.
service: notify.mobile_app_pixel
data:
message: "{{ state_attr('timer.wake_window_timer', 'finishes_at') }}"
This gives me the 3 hour duration but an hour out (i.e. less one hour)
"2023-05-05T11:41:34+00:00"
So I tried adding it to a time helper as below;
service: input_datetime.set_datetime
entity_id: input_datetime.wake_up_time
data_template:
datetime: "{{ state_attr('timer.wake_window_timer', 'finishes_at') }}"
But again this results in same issue as below;
This is what the timer is reporting under attributes;
Again sorry for the noob questions just can’t work it out, cheers.
WallyR
(Wally)
May 5, 2023, 9:28am
6
You need to format your time.
I can not make the code where I am, but look at the first post in this thread.
I use this value as a condition for my Google Home alarm in automation.
{{ (as_timestamp(now()) - as_timestamp(strptime(states('sensor.bedroom_speaker_alarm'), '%Y-%m-%dT%H:%M:%S+00:00')))|int }}
It tracks how many seconds till alarm triggers. But I would like to make it a template sensor. When I use it as a value_template it doesn’t update. Only after restarting Hassio and then nothing. How should I make it update state?
ltechdnb
(LTechdnb)
May 5, 2023, 10:52am
7
Thanks so much so been trying my best but can’t get the formatting correct, what am I missing.
service: notify.mobile_app_pixel
data:
message: "{{ state_attr('timer.wake_window_timer', 'finishes_at', '%Y-%m-%dT%H:%M:%S+00:00'))) }}"
Invalid action
template value should be a string for dictionary value @ data[0][‘data’]
I also just added it like the below but again get an error;
service: notify.nursing
data:
message: "{{ state_attr('timer.wake_window_timer', 'finishes_at', '%Y-%m-%dT%H:%M:%S+00:00'))) }}"{{ (as_timestamp(now()) - as_timestamp(strptime(states('timer.wake_window_timer', 'finishes_at'), '%Y-%m-%dT%H:%M:%S+00:00')))|int }}"
Invalid action
template value should be a string for dictionary value @ data[0][‘data’]
OK
Thanks again
Troon
(Troon)
May 5, 2023, 10:57am
8
service: notify.mobile_app_pixel
data:
message: "{{ strptime(state_attr('timer.wake_window_timer', 'finishes_at'), '%Y-%m-%dT%H:%M:%S+00:00') }}"
Break it down to see what’s going on. Your version had way too many close brackets. This is equivalent and makes it easier to read:
message: >
{{ strptime(
state_attr('timer.wake_window_timer', 'finishes_at'),
'%Y-%m-%dT%H:%M:%S+00:00'
) }}
ltechdnb
(LTechdnb)
May 5, 2023, 11:06am
9
Brilliant and thanks gain, so that now works but still give me the wrong time an hour behind.
message: >
{{ strptime(
state_attr('timer.wake_window_timer', 'finishes_at'),
'%Y-%m-%dT%H:%M:%S+01:00'
) }}
So I added “01:00” as assumed that would correct it but get the below error;
Error running action
Error rendering data template: ValueError: Template error: strptime got invalid input ‘2023-05-05T13:49:05+00:00’ when rendering template ‘{{ strptime( state_attr(‘timer.wake_window_timer’, ‘finishes_at’), ‘%Y-%m-%dT%H:%M:%S+01:00’ ) }}’ but no default was specified
Again so sorry for the pain. cheers
Troon
(Troon)
May 5, 2023, 11:38am
10
I still think you’re not understanding what’s going on here, and I’m not clear what the actual problem or request is.
Firstly, strptime
reads in a string and turns it into a datetime object. The format (all that %Y-
stuff) tells it what the bits in the string mean. Your finishes_at
attribute is as follows, and I’ve laid out the format below it, spaced out for clarity:
Attribute: 2023 - 05 - 05 T 13 : 49 : 05 +00:00
Format: %Y - %m - %d T %H : %M : %S +00:00
So strptime
reads in 2023 for the year, 05 for the month and so on. Everything that isn’t a %_
format specifier must match exactly: the hyphens, the T and the +00:00
timezone at the end. If you change that timezone marker, it no longer matches what it’s being fed and throws an error.
To be clear: the attribute time is 13:49 in UTC / GMT time (+00:00), which is 14:49 in current local UK time (+01:00). As I understand it, that time is correct but you want to display it in UK timezone.
I’ve just created a test timer, started at 12:35 BST for two hours.
Those templates are:
{{ state_attr('timer.test_timer','finishes_at') }}
{{ state_attr('timer.test_timer','finishes_at')|as_timestamp|timestamp_custom('%H:%M') }}
Does that give you what you want?
Reference for most of this:
1 Like
ltechdnb
(LTechdnb)
May 5, 2023, 1:34pm
11
Mate! Thank you so much for the extra details and in depth advise, that has indeed worked a treat and can’t thank you enough for all your time and effort. Hope you have an amazing day and thanks again. Cheers
For anyone else this was my final output thanks @Troon for being a legend and putting up with my noobness.
service: notify.mobile_app_pixel
data:
message: >-
{{
state_attr('timer.wake_window_timer','finishes_at')|as_timestamp|timestamp_custom('%H:%M')
}}
1 Like
Troon
(Troon)
May 5, 2023, 1:36pm
12
Time is never a simple subject:
Someday, some big historical event will happen during the DST changeover, and all the tick-tock articles chronicling how it unfolded will have to include a really annoying explanation next to their timelines.
1 Like
ltechdnb
(LTechdnb)
May 5, 2023, 1:41pm
13
That is truly accurate! What I thought was “straightforward” snowballed real quick. Thanks to the timelord that be you