openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

Use Quantity types for historic state functions. #281

Closed mherwege closed 1 month ago

mherwege commented 11 months ago

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.

florian-h05 commented 11 months ago

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.

mherwege commented 11 months ago

Indeed. I will open a core issue.

mherwege commented 1 month ago

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