Integration of a sensor looks odd

Hello,
i measure the electric power surplus with this template code

      - name: ueberschuss
        unique_id: TEST1000
        device_class: power
        state_class: measurement
        unit_of_measurement: W
        state: >
          {% if states("sensor.tasmota_sm_16_7_0")|float > 0 %}
          0
          {% else %}
          {{ states('sensor.tasmota_sm_16_7_0') | float * (-1) }}
          {% endif %}

after this i divided it by 1000 to get the unit kW with this code

      - name: ueberschuss_kW
        state: "{{ states('sensor.ueberschuss') | float / 1000 }}"
        unique_id: TEST55
        device_class: power
        state_class: measurement
        unit_of_measurement: kW

and finally i integrate the result with this code to get the energy

  - platform: integration
    name: ueberschuss_kW_int
    source: sensor.ueberschuss_kW
    unique_id: TEST999

The results are stored i a counter of all time and for days. I thought it works but i noticed something strange yesterday.
I looked at the load curve and on the integration, a little after 9:00 the integration jumps from 0 to over 1,5 kWh from a little amount of electric power surplus.
Did i anything wrong with my sensors or is this a bug ?

Best regards
Kai

You need to use left integration method instead of the default of trapezoidal. Search the forum for lots of similar posts if you want a detailed explanation of why (combination of math and how data is stored in HA).

1 Like

The above mentioned is definitely very important.

Also note that you need frequent update of the wattage sensor for this to be accurate if wattages fluctuate a lot (which they tend to do). The problem is even worse if you calculate overshoot by subtracting sensors with different update intervals/timestamps. If you can also do the calculation from kWh sensors, you should definitely go that way.

1 Like

Thank you for your super fast reply’s.
I made a new sensor for the integration:

  - platform: integration
    name: ueberschuss_kW_left_integration
    source: sensor.ueberschuss_kW
    unique_id: TEST1234
    method: left
    unit_time: h
    unit_prefix: k
    round: 2

I also have the idea to add this option

    max_sub_interval:
      seconds: 1

maybe it makes the result more accurate but it wont work.
The option “max_sub_interval” is not valid for integration.
Did you know why this is not valid ?

Best regards
Kai