Calculating charged kwhs for my electric vehicle

Hi, HA newbie here (again)

I am switching from domoticz to HA, so converting all my existing automations/sensors, and get stuck on the converting the script which calculated how much my car charges at home.

My car does not report this, but it does report the SoC percentage, so in domoticz i had a script that whenever the SoC percentage rises, it assumes it is charging and increments the kwh counter for the car with 1% of the battery pack size * the number of percent the SoC has changed.

The ingredients are there:
I made a template trigger which reports whenever a the Soc Value is increased:

template:
  - trigger:
      - trigger: state
        entity_id: sensor.volvo_yv1xzefvkn2722937_battery_charge_level
    sensor:
      - name: "diff_volvo percentage_level"
        unique_id: "diff_volvo percentage_level"
        state: >
          {% set delta = trigger.to_state.state | float - trigger.from_state.state | float %}
          {{ delta }}

and now i want to add some kind of automation or template sensor which then calculates the total charged kwh’s. (every time the template sensor reports a difference it should increased this counter).

I tried creating another template sensor which uses the above sensor as trigger:

  - trigger:
      - trigger: template
        value_template: '{{ states("sensor.diff_volvo_percentage_level") | float(0) > 0 }}'
    sensor:
      - name: "Volvo charged kwhs"
        unique_id: "Volvo charged kwhs"
        state: >
           {{ states("sensor.volvo_charged_kwhs") | float + (states("sensor.diff_volvo_percentage_level") | float * 0.67) }}

But the sensor remains unavailable, so who can help me?

Try this (untested):

- trigger:
    - trigger: state
      entity_id: sensor.diff_volvo_percentage_level
      not_to:
        - unknown
        - unavailable
  condition:
   - condition: numeric_state
     entity_id: sensor.diff_volvo_percentage_level
     above: 0
  sensor:
    - name: "Volvo charged kwhs"
      unique_id: "Volvo charged kwhs"
      state_class: total
      device_class: energy
      state: >
         {{ states("sensor.volvo_charged_kwhs") | float(0) + (states("sensor.diff_volvo_percentage_level") | float * 0.67) }}

Problems with your attempt:

  1. The trigger template has to change from false to true to trigger. Meaning this template result: {{ states("sensor.diff_volvo_percentage_level") | float(0) > 0 }} would only trigger when going from 0 or below to above 0. e.g. it would not trigger when going from 45 to 50.

  2. This template states("sensor.volvo_charged_kwhs") | float would initially be unknown. Thus generating an error as it could not be converted to a floating point number and you did not specify a default for this.

tx a lot. big step forward. The sensor now gives a reading when i start charging. But unfortunately only once when i start charging. During the charing process no further updates…

I analyzed and i think this is caused by the fact that the delta sensor remains at value 1 during charging (and stays at 1 after finishing the charging). I think this is due to HA not triggering anything when the value does not change, even if there was a new value (the same one) reported.

So e.g. when the car is charging from 55-60%, SoC level measurements will be resepectively 55%, 56%, 57%, 58%, 59% and then 60%.

The delta sensor only registers 2 measurements: 0 (when not yet charging), and then 1 (when the cars goes from 55-60%) and then stays 1.

Alternative would be that i use the change of the SoC percentage as trigger. Only issue is that i have 2 template sensors and i need to be 100% sure the Delta template sensor is calculated before the charged kwh template sensor.

how can I enforce that?