Template - charging time - restore last state after restart

Hi all!

I would ask for some help here and maybe get some guidance.
I have configured sensor which counts time from the beggining of charging like this:

  - platform: template
    sensors:
      ev_charging_time:
        friendly_name: EV Charging Time
        value_template: >
          {% set x = states('sensor.time') %}
          {% set t = 0 if states('binary_sensor.ev2_charging_status') == 'off' else now().timestamp() - states.binary_sensor.ev2_charging_status.last_changed.timestamp() %}
          {{ t | timestamp_custom('%H:%M', false) }}

But if I reboot HA, charging time sets to zero.

How can I configure sensor which will track charging time from the begining of charging but still in hh:mm:ss?

I hope I was clear enough…:slight_smile:

You all have a nice day and thank you in advance for your help.

Last_changed is resetted at boot , so what you have to do is to create an “input_datetime” entity that you set to “now()” when you are starting to charge, so when your binary_sensor is becoming “on”… and you modify your sensor “ev_charging_time” accordingly…

Thank you @browetd for your help and solution. Since I quite a noob and not near with my knowledge of programming I would appreaciate some more help.
I managed to create sensor input_datetime.start_charging_time with helper and I get the time 13:23:11 which is OK I think because it stays after reboot.

Now I’m stuck with configuring sensor which states elapsed time of charging. You mentioned I should modify sensor sensor.ev_charging_time accordingly…

I really don’t know how to do this…what part of the template should be input_datetime.start_charging_time?

Replace in your sensor template:
“states.binary_sensor.ev2_charging_status.last_changed.timestamp()”
by
“as_timestamp(states.input_datetime.start_charging_time.state)”
but your input_datetime should include the date too and not only the time…

1 Like

The value of Trigger-based Template Sensor is restored at startup.

Here’s your legacy-format Template Sensor converted to a modern-format Trigger-based Template Sensor.

template:
  - trigger:
      - platform: state
        entity_id: sensor.time
    sensor:
      - name: EV Charging Time
        state: >
          {% set t = 0 if is_state('binary_sensor.ev2_charging_status', 'off') else now().timestamp() - states.binary_sensor.ev2_charging_status.last_changed.timestamp() %}
          {{ t | timestamp_custom('%H:%M', false) }}
1 Like

Thank you @browetd :partying_face: :love_you_gesture: :+1:!!! It works now!!

It’s unclear what you have chosen to solve your problem. Are you using the Trigger-based Template Sensor I suggested or something else?

Thank you @123 for your help. I was banging my head with input_datetime and then after @browetd suggestion it worked.
I tried your approach with template in developers tools and I got the value (time) after reboot. I’ll try to configure the sensor and see how it works ‘live’.
I’ll get back when the EV will be charging again so I can test in “real life”.