As_timestamp(now()) - strange behaviour - time not calculating properly with timezone offset

I’m gonna blame Telstra and the NBN. lol.

If you’re not on a T$ contract, lots of better options…

I’ve been with them since Telecom days. But getting sick of how restrictive they are and all-controlling. Probably time to consider a change. Have a good day!

1 Like

HI All
Sorry to hijack an old thread, but I am experiencing a similar issue to above.
I upgraded from Resin to HassOS, by doing a snapshot and importing it in.
It mostly finished without any issues, however I did notice some of my times appear to be in UTC
I ran the above script and got the below
2019-08-03 07:32:02.022739+00:00
2019-08-03 17:32:02.022834+10:00
2019-08-03 07:32:02.022964+00:00
2019-08-03 07:32:02.023368+00:00
UTC
Australia/Sydney
UTC

BTW, I don’t live in Sydney, I live in Melbourne, I change the name to see if it would force the correct time. However it didn’t work.

Another interesting thing is that the home-assistant.log time stamp is also in UTC
Finally I checked on the console by running date and it showed the correct time and date
So there must be a setting I am missing somewhere. I am about to comment out:
time_zone: Australia/Melbourne in the configuration.yaml to see if that help

Does anyone have any advise. It’s terrible to think I can’t have a beer yet, since HA is telling me the wrong time :stuck_out_tongue:

Some weird behavior here. When I run the following in the templates section of the developer tools I get weird results:

{{ now().strftime('%d-%m-%Y %H:%M:%S') }}
{{ utcnow().strftime('%d-%m-%Y %H:%M:%S') }}

{{ as_timestamp(now()) }}
{{ as_timestamp(utcnow()) }}

This returns me:

07-01-2020 16:16:25
07-01-2020 15:16:25

1578410185.202144
1578410185.202044

As you can see the formatted times are nicely one hour apart (I’m in Amsterdam timezone), but the unix times are not. Anybody a clue why this is happening?

Why you tell this is weird? Both now() and utcnow() represent the same UNIX timestamp (number of seconds elapsed since 1rst January 1970 00:00:00 UTC).

ah ok. guess that makes sense. Is there another way to get the delta of timezone and utc in seconds? substracting one from another (e.g. {{ as_timestamp(now() - utcnow()) }} ) doesn’t seem to work.

{{ now().strftime(‘%z’) }}

Returns +1100 here so you could use that and then massage that value in a template to calculate seconds.

1 Like

thanks! I’ll give it a try and report back if it succeeds

For anyone interested herewith some back and forth with the timezone calculations (not tested in multiple timezones)

{% set secs = now().strftime('%z')[1:3]|int*3600  + now().strftime('%z')[3:5]|int*60 %}
{% if now().strftime('%z')[0] == '+' %}
{{(as_timestamp(utcnow())+secs)|timestamp_custom ('%Y-%m-%d %H:%M')}}
{% elif now().strftime('%z')[0] == '-' %}
{{(as_timestamp(utcnow())-secs)|timestamp_custom ('%Y-%m-%d %H:%M')}}
{% else %}
{{as_timestamp(now())}}
{% endif %}

{{(as_timestamp(utcnow())+secs)|timestamp_custom ('%Y-%m-%d %H:%M')}}
{{(as_timestamp(utcnow())-secs)|timestamp_custom ('%Y-%m-%d %H:%M')}}