Riemann Sum Integral Calculating Wrong Value for Lamp Energy

I have a lamp that, according to the specs, uses 25.6 watts when turned on. I have a Zigbee switch actuator connected to it, so my Home Assistant instance knows when it’s turned on or off, so I should be able to measure its energy consumption without an additional metering device.

I have a template sensor configured as follows:

template:
  sensor:
    - name: kitchen_island_lamp_power
      unique_id: d7a22810-9a60-4a83-880f-7e79c8431e86
      unit_of_measurement: 'W'
      device_class: power
      state: >
        {% if is_state('light.kitchen_island', 'on') %} {{ 25.6 | float }} {% else %} {{ 0 | float }} {% endif %}

To test it, I turned on the light for about 1 minute, and the template sensor yields the correct values:

Next, I’m using the Riemann sum integral (I copied the configuration of the “Energy” section of the docs) to compute the lamp’s energy consumption in kWh.

sensor:
  - platform: integration
    unique_id: b3bb2e79-f9e0-4d6d-8201-302e61ccfa62
    source: sensor.kitchen_island_lamp_power
    name: kitchen_island_lamp_energy
    unit_prefix: k
    unit_time: h
    round: 2

First, let’s calculate how much we expect the kWh value to rise when the lamp is turned on for 1 minute:

  • Duration: 1 minute = 60 seconds ~= 0.01666667 hours
  • Power: 25.6 watts = 0.0256 kilowatts
  • Energy: 0.0256 kW * 0.01666667 h = 0.00042666 kWh

Now the problem is that the Riemann sum integral computes 0.29 kWh in the same time (which cannot be correct from my point of view):

Does anyone see what I’m doing wrong? Any hint is very much appreciated.

I’m using the following version of Home Assistant:

  • Home Assistant 2022.8.7
  • Supervisor 2022.08.5
  • Operating System 8.5
  • Frontend 20220802.0 - latest

Interestingly, the Riemann sum integral sensor already increases in the moment the light is turned on, not afterwards. That’s weird, because it cannot know yet how long it will be turned on.

Also, the factor between the real energy used and the computed value is ~679.7, which is kind of random. It does not look like there’s a simple confusion of watts with kilowatts, or seconds with hours.

My take on your problem.

Riemann Integral is an approximation of the area under a curve by summation of small parts. To do this requires calculation of the area of lots of rectangles, and the approximation improves as the width of each rectangle reduces.

The HA integration Riemann Sum provides three methods of calculating the areas, and the default method is ‘trapezoidal’. The other methods build a rectangle, the height of which is either the start of the sample, or the end of the sample (left, right). In trapezoidal approach the area is not a rectangle but an area taken from the sample height at the start and end of the sample period.

For HA to do its work, the integration will only update when the source entity updates. HA has no way of repeatedly sampling the power entity, it just waits for the power entity to change. For this reason, the accuracy of the integration approximation is much improved the faster the source entity updates.

Now, looking at your situation, I see two potential issues.

The first issue is that your power is Watts. We know this because your template sensor says unit of measurement is ‘W’. HA will take this as the base power unit for the integration. When you set up your integration the unit time is ‘h’, so the integration will scale by the hour and return Watt-hours. For your lamp, we are looking at 26 watts for 1 minute, or ~0.4 watt-hours.

The second issue is that your set up and calculation method will result in the source sensor (kitchen lamp) only updating at each point it is turned on or off. Also, HA will update the sensor for the first time it appears, so in your test you have 8:25 = 0 , 8:29 = 26, 8:30 =0. This is not going to generate a good result based on the trapezoidal rule, as it actually ends up with two triangles.

Here is how it probably works. The first area is 4 minutes wide by 0 → 26, ie a triangle of area 4 * 26 * 0.5 = 52 (in Watt-minutes). The second area will be 8:29 = 26 to 8:30 = 0, ie a triangle of area 1 * 26 * 0.5 = 13 (Watt-minutes). Total power? 65 watt minutes or ~ 1 watt hour.

The result is a) the calculation shows power between the start up of the sensor and the light going on, and b) a value for the period of the light that is half what it should be.

What are you doing wrong?
You need a source sensor that updates more regularly.
You could (also) try one of the other integration methods. Trapezoidal is a good approximation in many cases, but for tricky situations like this, the HA documentation suggests
In case you expect that your source sensor will provide several subsequent values that are equal, you should opt for the left method to get accurate readings.

The other alternative is that, as you calculate the power based on on=26, off =0, you could extend the calculation to do the summation yourself. Off → On is no power, On → Off is (time between on and off) * 26. Scale as appropriate.

I hope this makes sense!

Edit - yes, my mistake. Your integration does include the ‘k’ unit, so the output would be scaled to kWh.
Adding ‘method: left’ might just work.

2 Likes

You need to use the left method as per this in the documents:

https://www.home-assistant.io/integrations/integration/#method

1 Like