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

Use Quantity types for historic state functions.

Open mherwege opened this issue 1 year ago • 2 comments

I have following code (generated by Blockly, but that is not relevant in the context):

updateTime = items.getItem('PVEtotalGS1').history.lastUpdate( 'influxdb').plusMinutes(1);
prevTotalGS1 = items.getItem('PVEtotalGS1').history.historicState(updateTime, 'influxdb')?.state;
deltaGS2 = String(items.getItem('PVEtotalGS2').history.deltaSince(updateTime, 'influxdb')) + ' kWh';
totalGS1 = (Quantity(prevTotalGS1).add(Quantity(deltaGS2).multiply(Quantity(cache.shared.get('GS_RATIO')))));

PVETotalGS1 and PVETotalGS2 are both Number:Energy items.

for history.historicState a state is returned, which can then be used for Quantity calculations in line 4. history.deltaSince return a numeric (not a QuantityType), so I had to explicitely attach the unit. To be consistent, it would be more logical to return a Quantity Type instead for this function as well.

The same argument would apply to other history functions.

mherwege avatar Jul 27 '23 12:07 mherwege

We only can return a Quantity or provide the unit, if openHAB core provides it, but it currently does not, see https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/extensions/persistenceextensions.

florian-h05 avatar Jul 27 '23 21:07 florian-h05

Indeed. I will open a core issue.

mherwege avatar Jul 28 '23 08:07 mherwege

Solved with https://github.com/openhab/openhab-js/pull/331

mherwege avatar May 15 '24 19:05 mherwege