[SOLVED] - How to convert / format sensor "last_boot" datetime object into a string?

I have a sensor called sensor.last_boot which shows me the values in this format

2021-06-05T13:41:30+00:00

I would like to convert it for display in lovelace in 2 strings 05.06.2021 and time 13:41

It is no text cause I could not extract the characters so it might be a datetime object.

value_template: "{{ states ('sensor.last_boot') }}"

I found just solutions for the other way around converting a string into a date time object which i do not need, just simple plain text cause I will need it in 2 different cases.
Any ideas or examples?

thx a lot !

1 Like

yo can use format field in an entities card.

How the state should be formatted. Currently only used for timestamp sensors. Valid values are: relative, total, date, time and datetime.

Hi,
thanks , but that solution would only work for card entity and that is too limited for my purpose. I need a conversion in the config file to have access to those strings everywhere.

any other idea for that ?

Are you certain it isn’t text? The state value of all entities is normally a string value.

Paste this into the Template Editor and see if it reports true or false.

{{ states('sensor.last_boot') is string }}

I overlooked to mention that if you want to display the date and time you can do this (paste this into the Template Editor to confirm it produces the desired results):

{{ as_timestamp(states('sensor.last_boot')) | timestamp_custom('%d.%m.%Y') }}
{{ as_timestamp(states('sensor.last_boot')) | timestamp_custom('%H:%M', false) }}
2 Likes

#1
yes, it is text … even though I had tried to get the 10 first ten characters but it did not work out as expected cause I got “unknown”
“{{ as_timestamp(states(‘sensor.last_boot’))[0:10] }}”

#2
and both lines worked perfectly in the template editor I had known about but never used before. Saves a lot of time to check such things. Great help for me that you had mentioned that cause I had used it only for Cards.

#3
And this did the trick for me - now I have to check a few other points to get it implemented right.

  - platform: template  
    sensors:
      pi_4_last_boo1t_time:
        friendly_name:  'Pi 4 LETZTER BOOT'
        value_template: "{{ as_timestamp(states('sensor.last_boot')) | timestamp_custom('%d.%m.%Y - %H:%M', false) }}"

THANKS A LOT ! :grinning:

1 Like

That’s because a timestamp isn’t a string. it’s a float.

leaving off the “as_timestamp” makes it a string.