openhab-js
openhab-js copied to clipboard
Use Quantity types for historic state functions.
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.
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.
Indeed. I will open a core issue.
Solved with https://github.com/openhab/openhab-js/pull/331