mcwieger
(Wieger )
June 22, 2022, 7:56am
1
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…
tom_l
June 22, 2022, 7:59am
2
mcwieger:
this suddenly broke.
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:
I keep seeing the same question about the new default argument for the templating. This post will cover all possible ways to define default.
All the methods that were changed:
acos
as_timestamp
asin
atan
atan2
cos
float
log
round
sin
sqrt
strptime
tan
timestamp_custom
timestamp_local
timestamp_utc
Each of them have the exact same default functionality but a different number of arguments. There are multiple ways to use these functions, and this is where all the confusion lies.
Understandi…
mcwieger
(Wieger )
June 22, 2022, 8:22am
3
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
tom_l
June 22, 2022, 8:44am
4
mcwieger:
This used to work
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') }}
mcwieger
(Wieger )
June 22, 2022, 8:54am
5
That works, thank you very much! Understand the syntax better now.
It did work before though, strangely enough…for several months.
tom_l
June 22, 2022, 9:01am
6
Well then something or someone deleted the format from strptime(), because it won’t work without it.
123
(Taras)
June 22, 2022, 11:57am
7
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') }}
quito96
(Quito96)
November 28, 2022, 1:19pm
8
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
123
(Taras)
November 28, 2022, 1:53pm
9
I don’t understand your question. Does the example you posted work or not?
quito96
(Quito96)
November 28, 2022, 3:27pm
10
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
123
(Taras)
November 28, 2022, 3:31pm
11
Try this:
{{ state_attr('weather.home', 'forecast')[0]['datetime']
| as_datetime | as_timestamp | timestamp_custom('%a') }}
1 Like
quito96
(Quito96)
November 28, 2022, 3:50pm
12
Thanks works now
epaper_weather_forecast:
value_template: "{{state_attr('weather.home','forecast')[0]['datetime']| as_datetime | as_timestamp | timestamp_custom('%a') }}"