Sensor to track when an entity was last in a desired state?

I’m trying to track when my car was last charged to 100%. I’ve currently got a stats sensor that tells me the max values within the past 2 weeks which is good enough for automations; but for my dashboard time since last at 100% is what I actually want to see.

I was thinking of creating a templated binary sensor that’s true when the battery is at 100% and using its last changed state but last changed doesn’t persist through restarts so that’s useless. :confused:

You need a helper for date and time and then you need to set up an automation for when the sensor for charging reach 100% to then set in the current time in the date_time helper.

This will give you the time the car was last fully charged.
If you need to then have it as a difference between current time and that helper value, then you can make a template sensor that just subtracts the helper value from current time or you can do that directly in the lovelace card also.

Create a Trigger-based Template Sensor that detects when the car’s charge is 100% and sets its state value to the current date and time. The value will be retained, even after a restart.

The relative_time function can be used to report elapsed time.

Thanks, this seems to be working great. For anyone else who comes across this my solution was:

  template:
    - trigger:
        platform: state
        entity_id: sensor.car_battery
        to: "100"
      sensor:
        - name: Car Full Battery
          device_class: battery
          state: "{{ now() }}"