Display the last charge to 100%

Hi there, I would like to display the last charge to 100% of my EV in my dashboard.

I’ve got a sensor that gives me the actual charge level of the car, and would like to use this information to trigger a timestamp update of a new sensor.
This newly created sensor should be displayed on my dashboard like “2 days ago”.

So I’ve done a bit of researches, and I could manage to put this code into my template.yaml and after a restart of HA I can see my new sensor “sensor.car_last_100”

- trigger:
    - platform: state
      entity_id:
        - sensor.car_battery_level
      to:
        - 100
  sensor:
    - name: "Car last 100%"
      unique_id: sensor.car_last_100
      device_class: timestamp
      state: "{{ now() }}"

I’m stuck at the point that my new sensor is created but the value is “unavailable”.
So I tried to put a value with the Dev tools to see if my trigger is doing something.
The trigger works fine when the battery goes to the specified value, but the value written in my new sensor changes from the manual specified state to “unavailable”.

So I think there is something wrong with this part of the code:

  sensor:
    - name: "Car last 100%"
      unique_id: sensor.car_last_100
      device_class: timestamp
      state: "{{ now() }}"

Would appreciate every tips I can get :slight_smile:

There is nothing wrong with your code. Your sensor will update next time the state of the battery level changes to 100%.

If you want to update it now, don’t change the template sensor in Developer tools → States. That is for testing only and is not permanent, as you discovered.

Instead change the battery level sensor. Change it to something other than 100% (only if it is 100% already), then change it to 100%. This will trigger your template sensor and update it. This value will stick and will even be restored after a restart.

The battery level sensor will change back to whatever it was before you manually changed it the next time it receives an update.