Problem when i create a sensor to know when i made the last backup

HI,
I need help to create a sensor that extracts the date and time from the sensor of the “Home Assistant Google Drive Backup” addon

if I ask the sensor

{{state_attr('sensor.backup_state', 'last_backup')}}  

it gives me this result : 2023-06-07T02:00:00.370737+00:00

from this I would like to extract the date and time formatted like this: 07/06/2023 04:00

I tried these 3 ways and it always gives me a correct result in Developer Tools:

1)

{% set s = state_attr('sensor.backup_state', 'last_backup') %}
{% set dt = s | as_datetime %}
{{ dt.strftime('%Y/%m/%d') }} {{ dt.strftime('%H:%M:%S') }}

2)

{{ as_timestamp(state_attr('sensor.backup_state','last_backup')) | timestamp_custom('%d/%m/%y - %H:%M') }}

3)

{{ as_timestamp(as_datetime(state_attr('sensor.backup_state', 'last_backup'))) | timestamp_custom("%d/%m/%Y %H:%M") }}

when I go to insert one of these 3 strings of code into the sensor I don’t get a result, see below

for example:

sensor:
  - platform: template
    sensors:
      ultimo_backup_google_drive:
        friendly_name: 'Ultimo Backup Google Drive'
        value_template: >
          {% set s = state_attr('sensor.backup_state', 'last_backup') %}
          {% set dt = s | as_datetime %}
          {{ dt.strftime('%Y-%m-%d') }} {{ dt.strftime('%H:%M:%S') }}

result

image

I don’t understand why the sensor extracts the result from me.
who can help me?
Thanks

Add an availability template to ensure that the value is not None.

availability_template: >
  {{ state_attr('sensor.backup_state', 'last_backup') is not none }}

That’s a legacy format template sensor: you should use modern format instead.

Legacy should still work though, and even if the template doesn’t return what you want, the entity should at least show up — where have you put that sensor configuration, and have you restarted HA? Are there any errors in the logs?

You can put the two strftime calls into one:

{{ dt.strftime('%Y-%m-%d %H:%M:%S') }}

thanks for the replies,
I don’t understand why but, I did a restart home assistant and the sensor appeared, and clearly there are no errors in the log.

Since it was implemented I do the quick reload if I have changes to a file and I’ve never agreed that it didn’t load everything.

I have everything saved in the packages folder and each project has its own sensors, I also keep them divided to help me if I have changes.

another question
Honestly, I have all my sensors with the old format.
does it make sense that I update them all to the modern format?

At the moment, it doesn’t matter — legacy format is supported. I did convert all of mine and write all new ones in modern format, but for now, there is no practical reason to do so other than keeping up to date with recommended practices.

I was looking at the new template creation method and indeed it’s leaner to write, although for others like light , switch, it’s slightly different and sure I’d be wrong to compile

I know I’m wrong not to stay up to date with the changes that developers make, but lately I don’t do a lot of things other than the occasional addition that I need, or if some configuration gets deprecated.
thanks again for your replies

In the end how did you create the definitive sensor?

this in my last solution

  - platform: template
    sensors:
      ultimo_backup_google_drive:
        friendly_name: 'last Backup Google Drive'
        value_template: >
          {% set s = state_attr('sensor.backup_state', 'last_backup') %}
          {% set dt = s | as_datetime %}
          {{ dt.strftime('%Y/%m/%d') }} {{ dt.strftime('%H:%M') }}
1 Like