Calculate droven kilometre since last charge

Hey,

as the subject already clearly describes are im trying to calculate the value how much kilometers i drove since the last charge.

sensor:
  - platform: template
    sensors:
      calculated_mileage:
        friendly_name: "Berechnete Kilometerzahl"
        unit_of_measurement: "km"
        value_template: >-
          {% set charging_state = states('sensor.id_4_pro_performance_150_kw_204_ps_charging_state') %} 
          {% set odometer = states('sensor.id_4_pro_performance_150_kw_204_ps_odometer') | int %}


          {%if is_state('sensor.id_4_pro_performance_150_kw_204_ps_charging_state', "charging") -%}
          {%set mileage = 0 %}
          {% set last_charging_odometer = odometer %}
          {{last_charging_odometer}}
          {% else %}
          {% set mileage = odometer - last_charging_odometer | default(0)  %}
          {%- endif %}


          {{mileage | default(0)}}

I’m really struggling with it. Every time I try to manually set the value of sensor.id_4_pro_performance_150_kw_204_ps_odometer to the value it had when I charged it, and also set sensor.id_4_pro_performance_150_kw_204_ps_charging_state to “charging,” I still get the value that is behind sensor.id_4_pro_performance_150_kw_204_ps_odometer instead of the calculated one from the “else” section.

Does someone has an idea what im doing wrong?

Greetings

Dany

Simply by looking I can’t find the error but have you tried the value template code in the developer tools?
It could help to debug

Dear @zioCristia,

thanks for your reply, i tried it as you suggested, but without any outcome:
ezgif-4-1a8f627ac9

It seems that the variable last_charging_odometer is missing whenever it comes to the else section.

Have you based on that Video any idea?

You’re defining last_charging_odometer in the if, so you can’t access it in the else. Move it out or just use odometer (but then your results will be 0).

I think what you need to do instead is to make an input_number helper to store the current odometer when it starts to charge, after calculating the previous delta (mileage) using the helper’s value and odometer.

Nice it works.

Here is my input_number configuration:

input_number:
  last_charging_odometer:
    name: Letzter Kilometerstand beim Laden
    min: 0
    max: 99999
    step: 1
    mode: box
    unit_of_measurement: km

and here my sensor:

sensor:

  - platform: template

    sensors:

      calculated_kilometre:

        friendly_name: "Berechnete Kilometerzahl"

        unit_of_measurement: "km"

        value_template: "{{ states('sensor.id_4_pro_performance_150_kw_204_ps_odometer') | int - states('input_number.last_charging_odometer') | int }}"


One question, is there any possibility to clean up the historic values of my new input_number helper?
I have from the testings many wrong ones.

1 Like

You can just wait for your DB to purge data according to your recorder’s purge settings, otherwise you’ll need to delete it manually from the DB.

Now that I look at my answer again: One can now call recorder.purge_entities service from the dev tab too.