Unexpected results from Rieman sensor?

I have a template sensor that measures the monetary value of power consumption in EUR/h, based on the current rates, and an integration sensor to convert that to EUR on the energy bill.

template:
  - sensor:
      - name: "Power Consumption Monetary Value"
        unique_id: power_consumption_monetary_value
        state: "{{ states('sensor.power_consumption') | float(0) * states('sensor.energyzero_today_energy_current_hour_price') | float(0) }}"
        unit_of_measurement: "EUR/h"
        state_class: measurement

sensor:
  - platform: integration
    name: Energy Consumption Monetary Value
    unique_id: energy_consumption_monetary_value
    source: sensor.power_consumption_monetary_value
    unit_time: h

This works somewhat as expected but sometimes I see unexpected results. Below is a plot of both sensors.

In this graph I see two short periods of power consumption. I’d say the integral increases way too much while the second one seems more realistic. (Didn’t check the exact value.) Also, the value seems to increase even before the power peak ends.

What happens here? My guess would be that the sample frequency of the Rieman sensor is too low. Any other thoughts or how to change this?

There is no “sample frequency”. The Riemann Sum sensor updates whenever the source sensor updates.

You are using the wrong approximation method. Trapezoidal will have large errors for signals that are on/off like that.

Use method: left in your Riemann Sum sensor.

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

Thanks, I changed the method to “left”. I had read that comment in the docs but didn’t understand why. Let me try to explain what happens. When a sensor value does not change, it is not updated, and does not trigger an update of the integral. Therefore, when it finally changes, the previous data point is actually the previous data point that was different, and the trapezoid is calculated over a longer period, yielding a large area. That explains why the first peak in power causes a large increase in energy, while the second peak doesn’t. It also explains why the increase occurs at the beginning of the peak. The graph visualization actually corresponds to the “left” method.

1 Like

There’s a good explanation here: