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

Add methods to Item to get the state as Quantity or Number #231

Closed rkoshak closed 1 year ago

rkoshak commented 1 year ago

Opening the issue so I don't forget and, if someone gets to it before me that's good too.

HistoricItem has a numericState method to return the state already cast/converted to a value we can do math with. This would be very useful to have on the Item class too.

In addition, this method should be Quantity aware or we should also provide an alternative quantityState method to make it easier to deal with the new Quantity API.

Or maybe it makes sense to bring back the whole getStateAs() discussion which can work for this as well as stuff like getting a Dimmer Item's ON/OFF state and the like.

florian-h05 commented 1 year ago

Coming from https://community.openhab.org/t/mathematical-operations-on-item-rawstate-using-javascript-keeping-uom/143967/7?u=florian-h05.

florian-h05 commented 1 year ago

I would propose to add both a numericState and a quantityState for convenience. The getStateAs() method is still not as simple to use as having those two properties, but I guess still worth to discuss it.

Since I am currently spending some time on the library, I'll take over that issue.