Actual energy calculations with electric car

I have some solar panels (solarEdge) and an electric car. I tried to calculate the usage at home and the usage for the car seperate like this:

afbeelding

The values are calculates like this:

  template:
    sensor:
    - unique_id: EF11AFDF3EC24EAE96097DFA0CD8E7A0
      name: grid_now
      icon: mdi:solar-power
      unit_of_measurement: 'W'
      state: >
        {{ states("sensor.solaredge_current_power") | float(0) |round(0) }}

    - unique_id: 4CEC0376985D49B4AD096A1898236431
      name: current_power_in
      icon: mdi:import
      unit_of_measurement: 'W'
      state: >
        {% set current_usage = states("sensor.power_usage") | float(0) |round(0) %}
        {% if current_usage < 0 -%} 0 {%- else -%}{{current_usage}}{% endif %}

    - unique_id: A728D0766B4145E1ABB94F2C3887585B
      name: current_power_out
      icon: mdi:export
      unit_of_measurement: 'W'
      state: >
          {% set current_delivery = states("sensor.power_usage") | float(0) |round(0) %}
          {% if current_delivery > 0 -%} 0 {%- else -%}{{current_delivery * -1}}{% endif %}

    - unique_id: 2679DDE03BEA4090B268D142259C94E0
      name: total_usage_now
      icon: mdi:flash
      unit_of_measurement: 'W'
      state: >
        {% if states("sensor.grid_now") |float(0) > 0 
              and states("sensor.grid_now") | float(0) + states("sensor.current_power_in") |float(0)
               > states("sensor.extra_usage") |float(0) + (states("input.number.power_baseline") |float(0) * 1.10) 
        %}
          {{ states("sensor.grid_now") |float(0) + states("sensor.current_power_in") | float(0) |round(0) }}
        {% else %}
          {{ states("sensor.extra_usage") |float(0) + (states("input.number.power_baseline") |float(0) * 1.10) |round(0)}}
        {% endif %}

    - unique_id: 0AD04EF5B2314A0D90AA942818F6F055
      name: total_usage_home
      icon: mdi:flash
      unit_of_measurement: 'W'
      state: >
        {% set current_home = 
              (states("sensor.total_usage_now") |float(0)
              - states("sensor.extra_usage") |float(0) 
              - states("sensor.current_power_out") |float(0)) |round(0) %}
        {%- if current_home < states("input.number.power_baseline") |float(0) %}
          {{ states("input.number.power_baseline") |float(0) |round(0) }}
        {% else %}
          {{current_home}}
        {% endif %}

The underlying calculations are sometimes wrong, because the solar-edge integration does not suppluy a live value, but periodic update. this is why all sorts of corrections are built in, avoiding negative values and even using a minimum baseline. However, al of it is garbaga as it does not relyable reflect the actual usage.

How can I get better results?

Power usage is an “energy” value, in Watts per hours, or more often killowatts per hour (kWh), while you only have power values (in Watts)

To “transform” power values into energy, use the “Riemann” integration