core icon indicating copy to clipboard operation
core copied to clipboard

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

Open Sesshoumaru-sama opened this issue 2 years ago • 5 comments

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

Sesshoumaru-sama avatar Dec 24 '23 16:12 Sesshoumaru-sama

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)

home-assistant[bot] avatar Dec 24 '23 16:12 home-assistant[bot]

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.

karwosts avatar Dec 24 '23 19:12 karwosts

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.

Sesshoumaru-sama avatar Dec 24 '23 20:12 Sesshoumaru-sama

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.

karwosts avatar Dec 24 '23 20:12 karwosts

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 avatar Dec 26 '23 13:12 Sesshoumaru-sama

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.

Sesshoumaru-sama avatar Dec 27 '23 15:12 Sesshoumaru-sama

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

  • platform: integration unique_id: c0f763d9-19ca-49eb-bc74-1becbe681ec4 source: sensor.stopcontact_plus2_vermogen method: left name: stk-plus2-kwh unit_prefix: k unit_time: h round: 2

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

bertarends avatar Mar 06 '24 09:03 bertarends

I can confirm this bug:

  • integration method cannot be changed after creating by GUI
  • spikes create jump in sensor value here, and it does not matter, which method is used:

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

AloisLohr avatar Apr 19 '24 10:04 AloisLohr

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?

i-am-shodan avatar May 07 '24 09:05 i-am-shodan

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

dgomes avatar May 07 '24 10:05 dgomes