How to convert a time string with UTC offset information?

I want to show a time stamp in my front end, when a certain script has been recently triggered.

script.__my_script_name__.attributes.last_triggered

delivers a time string with timezone offset information like

2019-03-30T17:07:26.035306+00:00

I want to convert this string into a more readable format with the help of strftime or strptime and write it to an input_text field.

  - service: input_text.set_value
    entity_id: input_text.last_reset_motion_count_einfahrt
    data_template:
      value: '{{ (script.reset_motion_counter_einfahrt.attributes.last_triggered).strftime("%A %d %b %H:%M") }}'

does not work.

Has anybody a hint for me, how to code the value part in order to receive a formatted timestring?

Try this?

value: “{{ states.script.reset_motion_counter_einfahrt.attributes.last_triggered|replace(’ ',‘T’) }}”

Never mind, this is UTC, not formatted that you wanted.

How about this:
value: ‘{{ states.automation.tell_time.attributes.last_triggered.strftime("%A %d %b %H:%M") }}’

I got something like this:
Saturday 30 Mar 18:00

If you don’t want anything fancy, this will do the trick:

{{state_attr('script.reset_motion_counter_einfahrt', 'last_triggered') | as_timestamp | timestamp_local }}

If you want control over the date and time’s appearance then you can use this as a starting point:

{{state_attr('script.reset_motion_counter_einfahrt', 'last_triggered') | as_timestamp | timestamp_custom("%Y-%m-%d %H:%M", true) }}

Python date/time formatting codes

1 Like

Thanks to “JTPublic” and “123”!

Your examples are working perfectly and I could create a solution for my UI - THANKS!

1 Like