Integral sensor: trapezoid method, strange output?
The problem
To start, idk if this is a bug or expected, but I thought I would point it out just in case.
I have an esphome device with 2 CT clamps. Esphome converts current to power and sends the data to Hass every 5 seconds. I noticed that the Hass integral trapezoid sensor seemed to be over reporting kWh usage based on the energy dashboard and the individual devices graph. My dryer was showing 3.45 kWh of consumption between 6-7am when my whole house consumption was only 3.14 kWh for that time period.
To test, I setup 2 integral sensors in esphome and 2 integral sensors in hass (left and trapezoid method). After testing for awhile, the 2 esphome sensors and the Hass left method sensor all report the same state. Only the Hass trapezoid sensor is over reporting.
Esphome converts the current to watts and sends the power data every 5 seconds. This means Hass and esphome are receiving the same data at approx the same interval (esphome isn't getting more data to work with).
Here is a screenshot of the power sensor, there doesn't seem to be any inconsistencies or spikes.
The reason I think something is wrong is because the Hass trapezoid sensor is the only one over reporting, the esphome trapezoid sensor is reporting the same state as the esphome left and Hass left sensor.
My apologies if this is expected behavior, I thought I should bring it up in case something is going on.
More screenshots:
What version of Home Assistant Core has the issue?
core-2024.9.2
What was the last working version of Home Assistant Core?
Unknown
What type of installation are you running?
Home Assistant OS
Integration causing the issue
Integral sensor: trapezoid method
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
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!
Code owner commands
Code owners of integration can trigger bot actions by commenting:
-
@home-assistant closeCloses the issue. -
@home-assistant rename Awesome new titleRenames the issue. -
@home-assistant reopenReopen the issue. -
@home-assistant unassign integrationRemoves the current integration label and assignees on the issue, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the issue. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.
(message by CodeOwnersMention)
integration documentation integration source (message by IssueLinks)
Trapezoidal and Left should not report the same value.
You can look at wikipedia article https://en.wikipedia.org/wiki/Riemann_sum and clearly understand there is a difference between methods.
That being said, depending on the use case any of the methods might over report. But mathematically speaking the trapezoid should be a better approximation to loads which are curves and not plateaus (like for instante a light bulb)
Nice, thank you for the reply! So, it would seem that there may be some issue with esphome's trapezoid method?
I haven't done any real math for a long time, so I can't really help or point out any mathematical inconsistencies, I just thought I should bring it up to someone who can. I spent some time trying to understand things on a basic level, and chatted with ChatGPT for a bit on the subject. I may put in a PR for the integral docs to show some clear-cut examples of what methods work best for different sensors. I got tripped up on the trapezoid method saying that it works best for sensors that fluctuate, which I considered the power sensor to do. Now I understand the power sensor spikes, plateaus, falls and repeats (square wave).
I'll close this issue and open one in the esphome repo to see if there is an issue with their calculations.
Thank you for your work and reply!