Templating with date/time: TemplateAssertionError

i am desperately trying to format a date correctly with a template. The date is in this form: "02/11/23 18:01:29".
My attempts look like this:

value_template: >-
  {% set date_string = states("sensor.urbackup_rp4_lastbackup") %}
  {% set parsed_date = strptime(date_string, '%m-%d-%y %H:%M:%S', None) %}
  {{ parsed_date | strftime("%Y-%m-%d %H:%M:%S") }}

The error message: TemplateAssertionError: No filter named 'strftime'.) for dictionary value @ data['sensors']['last_urbackup_rp4']
I have already tested various variants from other articles, but so far without success. Many thanks for tips!

Try this

{{ parsed_date.strftime("%Y-%m-%d %H:%M:%S") }}

Yipeeh, you made my day! The mistake was done and then I just had a very stupid one in the line before. The date separator was simply wrong. Now I get 2023-02-11 09:42:53 and should be able to calculate a diff easily.

1 Like

One other thing you may have to consider:

https://www.home-assistant.io/docs/configuration/templating/#time

Thank You! That’s quite important…
I have solved the task in such a way that I get the duration as hours for the use in an automation.

- platform: template
  scan_interval: 5
  sensors:
    last_urbackup_rp4:
      value_template: >-
        {% set date_string = states("sensor.urbackup_rp4_lastbackup") %}
        {% set parsed_date = strptime(date_string, '%m/%d/%y %H:%M:%S', None) %}
        {% set changed = parsed_date.strftime("%Y-%m-%dT%H:%M:%S+00:00") %}
        {% set delta = (as_timestamp(now()) - as_timestamp(changed)) %}
        {{ (delta / 3600) | int }}
      friendly_name: 'Letztes UrBackup vom RP4 (HA)'
      unit_of_measurement: 'h'