Is there a better way to achieve this? Get rid of timezone offset details

{{ state_attr('binary_sensor.alert', 'updated')| as_datetime(0) }}

returns:

2025-02-12 11:53:50-05:00

the result reflects the correct timezone (local - EST) and I want to get rid of the timezone offset string, ie: “-05:00”

Since these filters are not implemented

| datetimeformat('%Y-%m-%d %H:%M:%S')
| strftime('%Y-%m-%d %H:%M:%S')

I decided to convert to string and truncate:

{{ state_attr('binary_sensor.alert', 'updated')| as_datetime(0) | string | truncate(16, true, '') }}

which works:

2025-02-12 11:58

But is there another way to do this while keeping it under a datetime format?
If I convert to timestamp, I get UTC and end up with issues managing DST…

Thanks!

{{ state_attr('binary_sensor.alert', 'updated') | as_timestamp | timestamp_custom }}

Quick way to trim off the seconds.

{{ (state_attr('binary_sensor.alert', 'updated') | as_timestamp | timestamp_custom)[:-3] }}

BTW, is the value of updated a string? Because if it is then you may simply be able to trim off however many characters you want (without conversion to a datetime object). Unless it’s a UTC datetime string and you want it in local time.

Thank you! glad to learn something new

The original value seems to be ISO 8601 datetime format
so I guess your second suggestion makes sense (seconds are not needed).

{{ state_attr('binary_sensor.alert', 'updated') }}

2025-02-12T12:58:50-05:00

btw, just curious, is there a way to evaluate a “string”
something like "| is_string " (which doesn’t work…)

1 Like

Experiment with this in the Template Editor.

{{ 'cat' is string }}
{{ 54 is string }}
{{ 54 is integer }}
{{ ['cat', 54] is list }}
{{ ('cat', 54) is tuple }}

Thanks,

interestingly enough it evaluates as a string?

{{ state_attr('binary_sensor.alert', 'updated') is string }}

True

btw this also does the trick:

{{ state_attr('binary_sensor.alert', 'updated')| as_timestamp | timestamp_custom('%Y-%m-%d %H:%M') }}

2025-02-12 13:28


Yes, but a bit longer than just lopping off the last 3 characters from the result of timestamp_custom alone.

1 Like