openhab-core icon indicating copy to clipboard operation
openhab-core copied to clipboard

Search order of deltaBetween()/deltaSince() not correct / Additional ordering parameter for historicState()

Open shikousa opened this issue 2 years ago • 2 comments

I wanted to calculate the energy consumption of this year using the persisted energy readings over the function deltaBetween() or deltaSince(). As start date I have used January 1st, 2023. Both functions always reported NULL, which should not be the case.

For determining the startItem the functions deltaBetween() and deltaSince() are using historicState(). But the issue in historicState() is, that it always uses Ordering.DESCENDING to search persisted values. That means in my case it was looking for an energy reading before January 1st, 2023, which was not existing, but it should have looked after January 1st, 2023.

deltaBetween(): https://github.com/openhab/openhab-core/blob/7af02598ef59b4998b19b31b49023b7f54e9cc39/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java#L978

deltaSince(): https://github.com/openhab/openhab-core/blob/7af02598ef59b4998b19b31b49023b7f54e9cc39/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java#L953

historicState(): https://github.com/openhab/openhab-core/blob/7af02598ef59b4998b19b31b49023b7f54e9cc39/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/extensions/PersistenceExtensions.java#L120

Expected Behavior

The startItem of deltaBetween() and deltaSince() should be searched with Ordering.ASCENDING Searching the stopItem in deltaBetween() with Ordering.DESCENDING is correct.

Current Behavior

The startItem of deltaBetween() and deltaSince() should not be searched with Ordering.DESCENDING

Possible Solution

Create a new function historicState() with additional parameter "boolean ordering" If true the search order should be Ordering.ASCENDING, otherwise like before it is Ordering.DESCENDING

HistoricItem historicState(Item item, ZonedDateTime timestamp, String serviceId, boolean ordering) 
{
...
  if (ordering == true) {
     filter.setOrdering(Ordering.ASCENDING);
  } else {
     filter.setOrdering(Ordering.DESCENDING);
  }
...
}

This function with additional parameter "ordering" should also be available in rules.

It should be decided, if deltaSince() and historicState() get corrected as proposed. If they won't be updated due to legacy reasons, the availability of this new function gives the possibility in rules to do the calculation of deltaSince() and historicState() manually.

Your Environment

  • Version used: openHAB 4.0.4

shikousa avatar Nov 17 '23 20:11 shikousa

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/how-calculate-deltasince-firstdayofyear/144407/10

openhab-bot avatar Nov 17 '23 20:11 openhab-bot

I don't think this is a bug. If we don't know the value at the start of the timespan, we can't calculate the delta.

J-N-K avatar Dec 27 '23 17:12 J-N-K