Create a timezoned entity

If you have sensor.date then you can see its timezone.
How can I create a sensor/entity of the date (and time) in a different timezone to the one I am in?

If the other timezone follows the same DST convention as in your timezone, you can simply add/subtract hours from your local time:

{{ now() + timedelta(hours=3) }}

If it doesn’t follow the same DST convention then it gets more challenging. I don’t know of a way to convert a datetime object’s timezone in a template (mostly because all python timezone conversion examples I’ve seen rely on libraries that aren’t currently available for templating purposes).

Thanks for your thought, my aspiration is to get a sensor that tells me the date time in the UK (whilst I am in Australia). I want to use that timezone aware sensor to determine when that timezone switches to and from DST.
So adding/subtracting a number of hours from my timezone does not sort it.

Looks like you will have to use strptime to build the datetime object from scratch (%z is for timezone). What I wanted was to simply convert now() to a different timezone but the required library to achieve it is missing.

@123 I’ll give that a go, thanks

@123 I’m not winning. Can you assist?

I had expected at this point to get the tzinfo to say UTC not my timezone which is AEDT

This is what I’ve tried:

{% set mynow = as_timestamp(now()) %}
Mynow : {{mynow|int|float}}

{%set mynowstr = mynow|timestamp_custom("%Y-%m-%d %H:%M %z") %}
Mynowstr: {{mynowstr}}

{%set UTCnowstr = mynow|timestamp_custom("%Y-%m-%d %H:%M %z",false) %}
UTCnowstr: {{UTCnowstr}}

{%set tm =  strptime(UTCnowstr,"%Y-%m-%d %H:%M %z") %}
UTC now:   {{ tm|timestamp_custom("%d %m %Y %H:%M:%S %z",false) }}

{{ tm.astimezone().tzinfo }}

The results are:
Mynow : 1616387940.0

Mynowstr: 2021-03-22 15:39 +1100
UTCnowstr: 2021-03-22 04:39 +0000
UTC now: 2021-03-22 04:39:00+00:00
AEDT

It occurred to me that the UK’s timezone is simply UTC. :man_facepalming: To get the current UTC time, you can simply use this:

{{ utcnow() }}

To display the time only:

{{ utcnow().timestamp() | timestamp_custom('%H:%M', false) }}

Screenshot:
UTC

@123 UK has DST so is not simply UTC. The objective is to know when DST in that time zone
happens !

I’m beginning to feel like everything is conspiring to make this task as challenging as possible … :slight_smile:

For the moment, I’m out of ideas.

@123 thanks for your ideas!