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…
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…
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) }}
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”.