Value template, modify date format from restful sensor

Hi All,

I’ve got a sensor pulling using restful, the value returned is a date format as follows

I want to convert it to “01/01/2020” style but can’t get the custom date format to work, any ideas? It just returns “none”

{% set date = states('sensor.grey_bin') | regex_replace(find='"', replace='', ignorecase=False) %}
{{ as_timestamp(date) | timestamp_custom("%d/%m/%y", True) }}

n.b. the data comes back with double quotes hence the strip.


{{ strptime(states('sensor.grey_bin'), '"%Y-%m-%d"').strftime('%d/%m/%y') }}

or split and reverse it.

{{ states('sensor.grey_bin')[1:-1].split('-') | reverse | join('/') }}

Both do the same thing. @pnbruckner’s will give you more flexability if you want to do something like month day year.


Thanks Phil,

I’m getting Error rendering template: UndefinedError: ‘str object’ has no attribute ‘strftime’.

Can;t figure out why

Probably because states('sensor.grey_bin') is not returning a string formatted like you said. What do you get if you enter the following in the Template Editor?

{{ states('sensor.grey_bin') }}

I get “2020-01-13”

Maybe the quotes are different? Looks like your sensor.grey_bin value might have “ and ” instead of " and " ??

Maybe change @pnbruckner’s solution to:

{{ strptime(states('sensor.grey_bin'), '“%Y-%m-%d”').strftime('%d/%m/%y')

Just a guess.

Yeah, hard to say. @samtwilliams, when you post that sort of thing, please format it properly…


Sorry, my bad. SO its not the double quotes as this also fails;

{% set date = '"2020-01-01"' %}
{{ strptime(states(date), '"%Y-%m-%d"').strftime('%d/%m/%y') }}

Right, but the value of {{ states('sensor.grey_bin') }} you pasted above doesn’t have SHIFT-’ generated " quotes, but different ones. Maybe a quick screen snip, like @pnbruckner requested earlier of {{ states('sensor.grey_bin') }} in the template editor.

Need its EXACT value to know what mask to use in the strptime function.

Your post above:
does not contain " type quotes, which your post above does:


That fails because the states() function takes as a parameter an entity_id, and '"2020-01-01"' is not an entity_id. Can you post what I asked for, please?


Thanks. BTW, I think you already noticed and compensated for it, but I left the ending }} off my suggestion. I just went back and fixed that post.

Ok, so what does the following give you in the Template Editor:

{{ strptime(states('sensor.grey_bin'), '"%Y-%m-%d"') }}


Whoa! That’s weird. It works for me. How about this:

{{ strptime('"2020-01-13"', '"%Y-%m-%d"') }}

EDIT: FWIW, this is what I get:

2020-01-13 00:00:00

Which shows it’s converting the string to a Python datetime object. Can’t understand why it’s not for you.

Odd isn’t it. it like everything works except when you introduce the value from state.

2020-01-13 00:00:00

Ok, I give up. Use @petro’s suggestion. :slightly_smiling_face:

:rofl: ok thanks for Helping tho!.

1 Like

Trying to format the date and this is what I have… works great in Developers tools but not in Studio Code and it keeps throwing this error:

- platform: template
      {{ states('')[0:-1].split('-') | reverse | join('/') }}


Missed coma between flow collection entries

tried looking that up and still can’t figure out what’s missing here… suggestions?

Your yaml is incorrect. You’re missing value_template