openhab / openhab-core

Core framework of openHAB
https://www.openhab.org/
Eclipse Public License 2.0
930 stars 428 forks source link

Persistence Extensions: Riemann Sums #4461

Open mherwege opened 14 hours ago

mherwege commented 14 hours ago

Closes https://github.com/openhab/openhab-core/issues/4439

At the moment, it is not possible to easily calculate the integral value of the curve represented by persisted values. This is useful for calculating e.g. total energy consumed (in kWh) from instantaneous power meter readings (in W).

The current sum persistence actions calculate a sum without considering the time dimension, and therefore are only useful when we have a guaranteed constant interval between persisted values for this.

The average actions do use a time weighting in their calculation. Multiplying the average with the total duration considered would give an approximation. However, the average calculations assume a constant value over the bucket and take the value at the start of the bucket. Using this average to calculate an integration would also mean an extra calculation (already divided in the action code and multiplied again in the rule). An alternative approach to get an integral value is by setting up an integrated item and a rule that adds value * duration since previous change/upate, triggered on base item change. This works and is flexible in its calculation (value can be approximated based on previous and new item state). It suffers from things like unexpected shutdowns, where the base item and its aggretation may run out of sync.

This PR proposes to create a group of new actions, riemannSum , that will calculate the RiemannSum as an approximation for the integral value. Analoguous to the other actions, there are variants taking (or not) startDate, endDate and serviceId as input. There is one extra key parameter, the Riemann type, representing the type of approximation used. Valid values are:

The existing average, variance and deviation actions have been enhanced to use the results of the Riemann sum calculations. They now have variants with an extra parameter where this type can be selected. For backward compatibility and consistency between defaults of the different actions, the action versions for riemannSum, average, variance and deviation without a Riemann type parameter all use RiemannType.left as a default.

Tests have been enhanced end confirmed the existing actions with the existing parameters still behave as before.

If this PR is accepted the scripting libraries should be enhanced to cover these new actions and the new parameters in the existing actions.

mherwege commented 13 hours ago

Tests run fine locally. I am trying to figure out why they do not work in the PR.

mherwege commented 7 hours ago

I put in some extra test logging to try to analyze where the difference is between my succeeding local tests and the failing online tests.