What does this report on your system?
{{ (utcnow() - as_datetime(state_attr("sensor.my_sensor_name","LastUpdated"))).seconds }}
Same result or different?
What does this report on your system?
{{ (utcnow() - as_datetime(state_attr("sensor.my_sensor_name","LastUpdated"))).seconds }}
Same result or different?
Is there? GMT = UTC, but Iād be surprised if a source defines it as UTC+1
Yes, the stackoverflow thread that āconfirms itās zulu timeā has recent posts from 2 separate wikipedia pages. 1 for normal time computing which lists it as zulu time, and 1 page on nautical time that references zulu as gmt and z meaning zero. Youāre welcome to google it yourself āz at the end of timestampā, just donāt accept the āmost upvotedā answer and read all the solutions. Youāll find that people donāt agree on the subject.
From what it looks like, the āsolutionā post is old, and has more upvotes. However the link to the nautical page is less than a few months old with a climbing upvote.
This site indicates Zulu time is zero hours after GMT. Scroll past the calculator and the description states:
Z - is the zone designator for the zero UTC/GMT offset, also known as āZuluā time
+00 - basic short
+0000 - basic
+00:00 - extended
+0000 - sign character (+) followed by a four digit time providing hours (00) and minutes (00) of the offset. Indicates zero hour and zero minutes time differences of the zero meridian.
Zulu - Military abbreviation for GMT
Z - short form of āZuluā
Output for
{{ (utcnow() - as_datetime(state_attr("sensor.my_sensor_name","LastUpdated"))).seconds }}
is
82838
How does it compare to the value (currently) computed using your original template?
Now as timestamp: {{ as_timestamp(now()) }}
Attribute as timestamp: {{ as_timestamp(state_attr("sensor.my_sensor_name","LastUpdated")) }}
123-Test: {{ (utcnow() - as_datetime(state_attr("sensor.my_sensor_name","LastUpdated"))).seconds }}
Attribute - now: {{ (as_timestamp(state_attr("sensor.my_sensor_name","LastUpdated")))- as_timestamp(now())}}
displays as:
Now as timestamp: 1628169363.302744
Attribute as timestamp: 1628172942.0
123-Test: 82821
Attribute - now: 3578.697114944458
I donāt reach the same stackoverflow page as yours, apparently.
But the ISO 8601 except available defines the āZā as āthe UTC designator, added to the end of a time representation to indicate that a time of day is represented as UTC of dayā (https://www.iso.org/obp/ui/fr/#iso:std:iso:8601:-1:ed-1:v1:en), which leaves little place to interpretation
which is GMT or UTC1 , the debate is weather it means āZULUā or not.
Lol. The āz=zuluā, you mean? Well, not really important, is it
Not sure what you mean by UTC1, though. Is it a synonym for UTC, or is it UTC+1?
Itās just what the wikipedia page said. I assume it means utc seeing that GMT is UTC. Using GMT as a reference should have clued you in.
Could you link me to where āUTC1ā is used?
Never saw that notation, and if it does indeed exists, quite confusingā¦
I had interpreted LastUpdated to represent a datetime in the past, but the timestamp values shown above indicate it is greater than now, so itās in the future. If thatās true then the order of the subtraction in my template should be reversed.
{{ (as_datetime(state_attr("sensor.my_sensor_name","LastUpdated")) - utcnow()).seconds }}
itās probably a typo on the page and should be UT1, which is universal time aka UTC aka GMT
Now as timestamp: {{ as_timestamp(now()) }}
Attribute as timestamp: {{ as_timestamp(state_attr("sensor.my_sensor_name","LastUpdated")) }}
123-Test: {{ (as_datetime(state_attr("sensor.my_sensor_name","LastUpdated")) - utcnow()).seconds }}
Attribute - now: {{ (as_timestamp(state_attr("sensor.my_sensor_name","LastUpdated")))- as_timestamp(now())}}
displays:
Now as timestamp: 1628171580.01422
Attribute as timestamp: 1628175169.0
123-Test: 3588
Attribute - now: 3588.9856350421906
It doesnāt make sense that āLastUpdatedā is in the future.
The trick is probably to remove the āZā, as that datetime is actually in local time, not UTC
{{state_attr("sensor.my_sensor_name","LastUpdated").split('Z')[0]}}
That looks to be working for me:
{{ (as_timestamp(now()) - as_timestamp(state_attr("sensor.my_sensor_name","LastUpdated").split('Z')[0]))}}
displays
19.010035037994385
Perfect; the two techniques should produce the same result.
Itās not wrong. Your local time is definitely offset by one hour from UTC whereas the attributeās time is in UTC. If the offset is due to DST, and the attribute never employs DST, then you will have to normalize the values (add/subtract DST when appropriate).
If this doesnāt report -1
then DST is in effect (and can be employed as a test for normalizing the datetime value):
{{ now().timetuple().tm_isdst }}
Thanks - guess Iāll have to wait until October 31st to find out!
Do you know for certain that the attributeās value, despite indicating Zulu time, always represents your local time regardless of DST? Can the developer supply that information ā¦ or will you truly have to wait until the next DST change to find out?
BTW, if the goal is to slice off the last character then it can be done neatly like this (instead of using split('Z')[0]
):
state_attr("sensor.my_sensor_name","LastUpdated")[:-1]