Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to left method as default for rieman sum #116736

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

elupus
Copy link
Contributor

@elupus elupus commented May 3, 2024

Proposed change

Let the rieman sum integration default to left instead of trapezoidal. Trapezoidal is mostly wrong since home assistant does not update data when value is unchanged. There is attempts to resolve this, but as of yet the integral sensor does not take those into account. Defaulting to left is a better default.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented May 3, 2024

Hey there @dgomes, mind taking a look at this pull request 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 close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign integration Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@dgomes
Copy link
Contributor

dgomes commented May 3, 2024

I don't think this is the right path.

There is #110685

And ultimately left does not provide a better approximation of the integral, it is just an over simplification for resistive loads.

@elupus
Copy link
Contributor Author

elupus commented May 3, 2024

left is much much much better for anything that keeps a constant value over a long time since we don't update the underlying sensor then. I for example ended up with HUGE electrical usage in my data, since my electrical heater wasn't used and at 0 for several weeks, then kicked up to 6 kw for half an hour. Since the last changed value was way way back, it was really a huge trapezoidal.

I fully agree that this is not a full solution. But it's much better default than trapezoidal until we start updating periodically.

@elupus
Copy link
Contributor Author

elupus commented May 3, 2024

Ps. I suspect a very common use case is just power to energy, where the power value could remain constant for some periods. I didn't read the docs careful, since i thought I just need a simple integration of this, i don't care if it's 100% accurate.

I do care when i get values of 100kwh added to the intergral, when it was just 6kw over 30 minutes, ie 3kwh. So a magnitude wrong value.

@dgomes
Copy link
Contributor

dgomes commented May 3, 2024

I understand your struggle, but this is IMHO not the right approach.

The PR for time based integration trigger would solve your issue as an example.

@rkistner
Copy link

rkistner commented May 4, 2024

The integration trigger mostly will solve the issue when it is configured correctly. I suspect most users won't configure it until they run into the issue, and even then their configuration may not be optimal.

Trapezoidal makes sense outside home assistant - if you are polling a sensor say every minute, and want to integrate that value, trapezoidal is probably the best method.

But home assistant does not work that way. Sensor values are not updated when the value hasn't changed. It's an assumption everywhere else in home assistant that the best approximation at any point in time is the last sensor value before it (what left does), not some interpolated value (like what trapezoidal does). This is how sensor values are stored (ignoring updates that don't change the value), and the way it's rendered in the history graph.

When you use the new max_sub_interval option, and configure it with the same (or smaller) interval as your sensor, then trapezoidal may give you a better approximation.

In all other cases, if your sensor can ever give you the same value twice in a row, left is a better approximation. And in cases such as energy sensors, which is the probably the main use case for the integration sensor (it's the only documented example), duplicate values are very common which makes trapezoidal very wrong.

max_sub_interval is a great addition. But there is no good universal default for max_sub_interval, and we should have good default behavior when users don't configure it. So I really think we need both changes.

@dgomes
Copy link
Contributor

dgomes commented May 4, 2024

The integration trigger mostly will solve the issue when it is configured correctly. I suspect most users won't configure it until they run into the issue, and even then their configuration may not be optimal.

"configured correctly" depends on each one use case, for many the current integration fits perfectly their needs

Trapezoidal makes sense outside home assistant - if you are polling a sensor say every minute, and want to integrate that value, trapezoidal is probably the best method.

That is the case for many people with sensors that only provide the power readings and update quite often, you can find that for many solar inverters for example, and even for some other smart devices.

But home assistant does not work that way. Sensor values are not updated when the value hasn't changed. It's an assumption everywhere else in home assistant that the best approximation at any point in time is the last sensor value before it (what left does), not some interpolated value (like what trapezoidal does). This is how sensor values are stored (ignoring updates that don't change the value), and the way it's rendered in the history graph.

That's why #113869 is under review.

When you use the new max_sub_interval option, and configure it with the same (or smaller) interval as your sensor, then trapezoidal may give you a better approximation.

It will certainly will, and you can figure out a value easily enough and start with a sensible default of couple minutes.

In all other cases, if your sensor can ever give you the same value twice in a row, left is a better approximation. And in cases such as energy sensors, which is the probably the main use case for the integration sensor (it's the only documented example), duplicate values are very common which makes trapezoidal very wrong.

max_sub_interval is a great addition. But there is no good universal default for max_sub_interval, and we should have good default behavior when users don't configure it. So I really think we need both changes.

See #113869

@rkistner
Copy link

rkistner commented May 4, 2024

That is the case for many people with sensors that only provide the power readings and update quite often, you can find that for many solar inverters for example, and even for some other smart devices.

Solar inverters are a perfect example of where this is broken. You get zero values for the entire night, causing a wildly incorrect value when the first non-zero value is reported in the morning.

See #113869

I wasn't aware of that PR. I think that should fix the issue of "the defaults give completely broken results", avoiding the need to change the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Riemann Sum integration uses broken defaults
3 participants