The EPIC Time Conversion and Manipulation Thread!

strptime(): - converts a string to a datetime object - strptime(date_string, format)

NOTE: I’ve tried to make all of these posts able to be copied as is into the template editor and work.
Due to some of the examples I’ve included you will get errors when you copy this post until you make corrections and use your correct entities in those examples.
I’ll annotate those sections that need to be modified in bold and with a “@@”.

Convert the strings ‘07:32’ & 00:30 to time datetimes and subtract:

Set t1 = ‘07:32’
{%- set t1 = '07:32' %}

Set t2 = ‘30’ (in minutes)
{%- set t2 = '30' %}

{{ t1 }} - {{ t2 }} = {{ strptime(t1, '%H:%M') - strptime(t2, '%M') }}

Or you can use a string instead of variables in the function:

07:32 - 00:30 = {{ strptime('07:32', '%H:%M') - strptime('30', '%M') }}

@@ The following section uses current values based on the sensors created by the Time & Date component in HA @@

date sensor as datetime object = {{ strptime(states('sensor.date'), '%Y-%m-%d') }}

date sensor year only as datetime object = {{ strptime(states('sensor.date'), '%Y-%m-%d').year }}

date & time sensor concatenated as a datetime object = t3 = {% set t3 = strptime(states.sensor.date.state+' '+states.sensor.time.state,'%Y-%m-%d %H:%M') %}

t3 = {{ t3 }}

t3 formatted with strftime = {{ t3.strftime("%b %d %Y %H:%M:%S") }}

/@@

Set time_string = 2013, 9, 30, 7, 6, 5
{%- set time_string = '2013, 9, 30, 7, 6, 5' %}

time_string as a string = {{time_string}}

time_string as a datetime object = {{ strptime(time_string, '%Y, %m, %d, %H, %M, %S') }}

Set time_string_2 to time_string as a datetime object
{%- set time_string_2 = strptime(time_string, '%Y, %m, %d, %H, %M, %S') %}

time_string_2 as a timestamp = {{ as_timestamp(time_string_2) }}

Year from time_string_2 = {{ time_string_2.strftime('%Y') }}

Set time_string2 = 2013, 9, 30
{%- set time_string2 = '2013, 9, 30' %}

time_string2 as a string = {{time_string2}}

time_string2 as a datetime = {{ strptime(time_string2, '%Y, %m, %d') }}

Set time12 to time_string2 as a datetime
{%- set time12 = strptime(time_string2, '%Y, %m, %d') %}

time12 as a datetime = {{ time12 }}

time12 as a timestamp = {{ as_timestamp(time12) }}

Set time_4 to time_string_2 as a timestamp
{%- set time_4 = as_timestamp(time_string_2) %}

time_4 = {{ time_4 }}

Set time_5 to now as timestamp
{%- set time_5 = as_timestamp(now()) %}

time_5 = {{ time_5 }}

{{ time_5 }} - {{ time_4 }} = {{ (time_5 - time_4) }}

Set time_6 = time_5 - time_4
{%- set time_6 = (time_5 - time_4) %}

time_6 = {{ time_6 }}

t6 as a custom timestamp = {{ time_6|timestamp_custom('%Y-%m-%d') }}

This section shows the time 0 as used in the UNIX Epoch

Set time_7 to 0 as a floating point number
{%- set time_7 = 0.0 %}

Then we can use it in the same way as the result of converting a date/time string to a timestamp with as_timestamp()

time_7 (as timestamp = 0) as custom timestamp = {{ time_7|timestamp_custom('%Y-%m-%d %H:%M:%S') }}

Checking to verify that each increment of 1 in the UNIX Epoch timestamp is equal to one second we can do the following:

Set time_8 to 1 as a floating point number
{%- set time_8 = 1.0 %}

time_8 (as timestamp = 1) as custom timestamp = {{ time_8|timestamp_custom('%Y-%m-%d %H:%M:%S') }}

Which is 1 second later than the time 0 above.

@@ The examples in this section will work as long as you pick an entity_id that are datetime objects:

(But I’m not sure which entities in HA are true datetime objects so you’ll need to experiment. And until you use good entities in these examples this section will cause this post to not render correctly in the template editor. YOU WILL GET ERRORS BECAUSE OF USING THIS SECTION UNMODIFIED.)

Google Calendar event start_time attribute as a string = {{ states.calendar.mycalendargmailcom.attributes.start_time }}

Google Calendar start_time attribute converted to datetime using strptime = {{strptime(states.calendar.mycalendargmailcom.attributes.start_time, '%Y-%m-%d %H:%M')}}

date__time sensor as a string = {{ states.sensor.date__time.state }}

date__time sensor converted to datetime using strptime = {{ strptime(states.sensor.date__time.state, '%Y-%m-%d, %H:%M') }}

automation last_triggered time as a string = {{ states.automation.deck_light_off_at_1am.attributes.last_triggered }}

BUT - This doesn’t work:

(NOTE: this isn’t in a template because it will blank out the result column due to the formatting error)

strptime(states.automation.deck_light_off_at_1am.attributes.last_triggered, ‘%Y-%m-%d %H:%M:%S.%f%z’)

I think it’s because the format of the UTC offset (+00:00) doesn’t match the expected format (+0000) but I haven’t been able to verify that.

But if you set a variable (t) to the above attribute then use it in strftime (covered in the next post) it will work.

t set to the attribute from above:
{%- set t = (states.automation.deck_light_off_at_1am.attributes.last_triggered) %}

t = {{ t }}

t formatted in strftime = {{ t.strftime('%Y-%m-%d %H:%M:%S.%f') }}

set t2 = t

{%- set t2 = (t.strftime('%Y-%m-%d %H:%M:%S.%f')) %}

t2 = {{t2}}

t2 converted to datetime with strptime = {{ strptime(t2, '%Y-%m-%d %H:%M:%S.%f') }}

Or you can nest them like this without the need to create another variable:

{{ strptime(t.strftime('%Y-%m-%d %H:%M:%S.%f'), '%Y-%m-%d %H:%M:%S.%f') }}

Does anyone know the format for incorporating the %z into the strptime() instruction?

7 Likes