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
72.76k stars 30.47k forks source link

Vastly wrong calculation in Riemann Integration in case of spikes in input #106363

Closed Sesshoumaru-sama closed 5 months ago

Sesshoumaru-sama commented 10 months ago

The problem

Hi

I am reading the current active power from a Varta Element 6 via Modbus. Positive and negative values are split into charge and discharge sensors. These sensors (W) are then input to the Riemann Sum Utility to track the energy (kwh). In arbitrary intervals, maybe once a week or so, the energy calculated by the Riemann Integration is vastly deviating from the actual value (see below) veerr

A very short small pike of 700W suddenly causes a jump of 28 kwh in the energy.

What version of Home Assistant Core has the issue?

core-2023.12.2

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

Riemann Sum 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?

I see nothing unusual in the logs. So if there is additional info needed, it would be good to know how to obtain it.

Additional information

No response

home-assistant[bot] commented 10 months 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!

Code owner commands Code owners of `integration` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign integration` Removes the current integration label and assignees on the issue, 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 issue. - `@home-assistant remove-label needs-more-information` Remove 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)

karwosts commented 10 months ago

Sounds like you probably have the wrong integration method:

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

It should be set to left. The default will give incorrect values for sparsely updated sensors. From your graph I'm guessing it's trapezoidal.

Sesshoumaru-sama commented 10 months ago

I see no way to change the method and even no statement of which method is being used. Is there a magic trick to change this configuration? (It should be left, but as I said I can't see it anywhere)

Still: Something be that wrong must be avoidable. Thats a million percent error.

karwosts commented 10 months ago

The only way I know to check is to look in your .storage folder, I think it is in core.config_entries.

e,g.:

      {
        "entry_id": "bfca90a5d96433682d2df6b9889d93fa",
        "version": 1,
        "domain": "integration",
        "title": "test",
        "data": {},
        "options": {
          "name": "test",
          "source": "sensor.tr951_temperature",
          "method": "trapezoidal",
          "round": 2.0,
          "unit_prefix": "none",
          "unit_time": "h"
        },
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": null,
        "disabled_by": null
      },

Unfortunately AFAIK it's not changeable or even viewable in the frontend, it's only set when you create it.

Sesshoumaru-sama commented 10 months ago

I will check. Would it be a feature request with with good prospects to ask to have the integration method configurable? The error is so big that something has to be done.

Sesshoumaru-sama commented 10 months ago

I am sure I selected "left", but the config entry actually said "trapezoidal". I changed it in the config now, restarted and will watch. Thanks for the input.

bertarends commented 7 months ago

You have more control if you use yaml instead of the helper. In my sensor.yaml I have an entry like this:

I have noticed that after a change a restart of HA core is necessary.

AloisLohr commented 6 months ago

I can confirm this bug:

If I check last sensor values I guess that the integration is calculating between the actual and the last value. If the sensor was updated long ago, the calculation is using a too long period of time to calculate the update value.

image

i-am-shodan commented 5 months ago

This helper seems to have a lot of issues. As a user of this I shouldn't really be expected to make a good choice about what configuration options fit and then be burnt when they don't work out. This is the job of the integration. What if I have a sensor that sometimes changes all the time and then for a lot of the day never does?

At the moment, in a lot of my use cases, this helper is pretty much garbage.

Could you run the left and trapezoid algorithms at the same time and use the most sensible result that given the number of updates that have happened in a second?

dgomes commented 5 months ago

You can now change the integration method https://github.com/home-assistant/core/pull/116740