I do get the timezone info from the companion app sensor state_attr('sensor.<phone>_current_time_zone', 'time_zone_id')
. But this gives a string, which I can’t use with the astimezone
function:
{{ now().astimezone('Asia/Dubai') }}
This gives an error saying: TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'str'
.
Any idea how I can convert the string to type tzinfo using Home Assistant templates only (i.e. without using python scripts)?
I played around with this before but I couldn’t figure out a way to do the type conversion either… or even if this Jinja implementation supports type tzinfo.
What about something like:
{% set millis_off = state_attr('sensor.<phone>_current_time_zone','utc_offset') %}
{{ (utcnow() + timedelta(milliseconds = millis_off)).strftime('%-I:%M:%S %p on %-d-%b-%Y') }}
EDIT: Corrected for milliseconds
Thanks! Yeah that works just fine (with some fixes). The offset seems to be in milliseconds. Here’s the final version (based on your suggestion) that I used:
{% set seconds_offset = state_attr('sensor.komodo_current_time_zone', 'utc_offset') / 1000 -%}
{% set time_at_utc = utcnow() + timedelta(seconds= seconds_offset) -%}
{{ as_timestamp(time_at_utc) | timestamp_custom("%-I:%M:%S %p on %-d-%b-%Y", local=False) }}