Offsetting datetime reported by sensor

I have a REST sensor, which grabs JSON data, one piece of data outputs a datetime stamp as follows:

“last_submit”: “Tue, 23 Apr 2019 15:22:31 GMT”,

I do not live in the GMT timezone however and would like it to be offset to the timezone I am in, is this possible with a template? Do I manually have to set the offset in hours or can I specify my timezone and will a function automatically keep in mind DST as well?

Thanks

Paste this into Home Assistant’s Template Editor:

{% set x = 'Tue, 23 Apr 2019 15:22:31 GMT' %}

{{ as_timestamp(strptime(x, '%a, %d %b %Y %H:%M:%S %Z')) | timestamp_local }}

Result is:

2019-04-23 15:22:31

for some reason GMT isn’t recognized by the strptime built into home assistant, so you need to change it to it’s offset.

So assuming that you build this directly into your rest sensor…

sensor:
  - platform: rest
    resource: http://IP_ADRRESS
    name: Last Submitted
    value_template: >-
      {% set t = value_json.last_submit.replace('GMT','+0000') %}
      {{ as_timestamp(strptime(t, '%a, %d %b %Y %H:%M:%S %z')) | timestamp_custom('%a, %d %b %Y %H:%M:%S %Z') }}

That will get you the exact same timestamp, but in your TZ.

1 Like

That doesn’t work properly. No matter what timezone moniker you use, it doesn’t work. Not sure why to be honest. Gotta replace it with the timezone as a number and it will work.

1 Like

Bummer! :frowning:


EDIT

Did some light-reading about it and, in short, strptime’s timezone conversion (%z %Z) seems to be hit or miss, depending on the implementation (i.e. as you said, it’s a complete miss in this case). Anyone wishing to learn more about, this is a good starting point: