home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.54k stars 30.72k forks source link

Riemann integration calculates wrong values when subsequent data points have equal values #74434

Closed Andreas2430 closed 2 years ago

Andreas2430 commented 2 years ago

The problem

I have encountered a bug in the Riemann integration: If several subsequent data points of the source sensor have the same value, then the Riemann integration will calculate the integral value wrongly.

The problem occurs, e.g., when I use the Riemann integration to calculate my appartment's energy consumption from the public electricity grid, using power measurements from my appartment's electricity meter.

I have a small photovoltaics system, therefore on sunny days my appartments' energy consumption from the grid is zero during daytime. However, in the evening (or when a cloud blocks the sun), when consumption from the grid ramps up again, due to this bug the Riemann integration sensor value will show a sudden increase with a way too high step size.

To explain the bug, I have drawn the following image:

Home Assistant Riemann Integration Problem

To avoid this bug, the Riemann integration should always use the most recent value of the source sensor, regardless of whether it was equal or unequal to the previous values:

Home Assistant Riemann Integration Problem Correct

What version of Home Assistant Core has the issue?

2022.6.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

integration

Link to integration documentation on our website

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

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

probot-home-assistant[bot] commented 2 years ago

integration documentation integration source (message by IssueLinks)

probot-home-assistant[bot] commented 2 years ago

Hey there @dgomes, mind taking a look at this issue as it has been labeled with an integration (integration) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)

dgomes commented 2 years ago

this is a limitation of HA internal API async_track_state_change_event

in core.py EVENT_STATE_CHANGED is only fired if state is different in function async_set

There is no other event to listen to.

Andreas2430 commented 2 years ago

Thanks for the feedback. That is unfortunate.

I think this limitation should be clearly stated in the documentation of the "integration" integration: If multiple subsequent equal values of the source sensor are expected, users should avoid choosing the trapezoid or right Riemann sum settings. Only the left Riemann sum will work correctly in these cases.

clowrey commented 2 years ago

Thanks for finding this bug and documenting it!! I just noticed the same errors in my data and was really wondering where the extra power came from as I knew it wasn't real... Maybe this should default to the Left Riemann sum if it will remain the only accurate type with repeating data as my errors were significant, over 40% "extra" kWh for one hour that wasn't real.