Strptime got invalid input

I’m trying to convert a date to a more friendly date in my configuration.yaml. This used to work, but after an update (2022.5 or 2022.6) this suddenly broke. I use the following syntax:

{{ as_timestamp(strptime(state_attr("sensor.afvalwijzer_gft", "year_month_day_date"), "")) | timestamp_custom('%a %d') }}`

It recognizes the input (2022-06-22), but somehow this is not accepted as a valid date anymore (as said, it used to work). Some questions:

  • Which format does strptime expect (maybe I can concatenate the year_month_day_date with a dummy time (which is not relevant for this sensor)
  • Is there an alternative to strptime that converts this string to a date, which in turn I can convert to a friendly format?

As said: it used to work, so I wonder what broke it…

Are you seeing errors in your logs about missing template defaults?

If the attribute is not available (e.g. when home assistant starts) the template will fail to load unless you define a default. How to:

Hi Tom,

it doesn’t seem to be a defaulting issue, since it fetches the correct date (it is actually 2022-06-22, this is not a default). This is the error message:

ValueError: Template error: strptime got invalid input '2022-06-22' when rendering template '{{ as_timestamp(strptime(state_attr("sensor.afvalwijzer_gft", "year_month_day_date"), "")) | timestamp_custom('%a %d') }}' but no default was specified

The template you have shown would never have worked.

strptime() requires a format. strptime(string, format) you have not supplied one.

Did you delete it by accident perhaps?

Try this:

{{ as_timestamp(strptime(state_attr("sensor.afvalwijzer_gft", "year_month_day_date"), "%Y-%m-%d")) | timestamp_custom('%a %d') }}

That works, thank you very much! Understand the syntax better now.

It did work before though, strangely enough…for several months.

Well then something or someone deleted the format from strptime(), because it won’t work without it.

For future reference, you can use the as_datetime filter because the date value is in a recognizable format (YYYY-MM-DD).

{{ state_attr('sensor.afvalwijzer_gft', 'year_month_day_date')
   | as_datetime | as_timestamp | timestamp_custom('%a %d') }}

Hello Taras,
Your last suggestion helped me with another template, but in this case I have difficulties:

value_template: "{{as_timestamp(strptime(state_attr('weather.home', 'forecast')[0]['datetime'], '%Y-%m-%dT%H:%M')) | timestamp_custom('%a') }}

How i handle this squenz? [0]['datetime‘]

Thanks

I don’t understand your question. Does the example you posted work or not?

Sorry … don’t work Error Message in Log
TemplateError('ValueError: Template error: strptime got invalid input '2022-11-29T11:00:00+00:00' when rendering template

Try this:

{{ state_attr('weather.home', 'forecast')[0]['datetime']
   | as_datetime | as_timestamp | timestamp_custom('%a') }}
1 Like

Thanks works now :slight_smile:

epaper_weather_forecast:
        value_template: "{{state_attr('weather.home','forecast')[0]['datetime']| as_datetime | as_timestamp | timestamp_custom('%a') }}"