openhab / openhab-google-assistant

openHAB Google Assistant: Actions on Google for openHAB
Eclipse Public License 2.0
176 stars 86 forks source link

Parse states as float for Number:Dimensionless items with units #534

Closed mgr01 closed 7 months ago

mgr01 commented 7 months ago

Item.state returns number+unit for items with units of measurement, e.g. "10.5 %", "22 °C". When using units, humidity should be represented by a Number:Dimensionless item with a "%" unit set in its metadata[1]. For those: Number("10.5 %") is NaN, but parseFloat("10.5 %") returns 10.5.

[1] See https://www.openhab.org/docs/concepts/units-of-measurement.html#list-of-units

michikrug commented 7 months ago

Thanks for your contribution.

Do you might know if we maybe face a similar issue with other items / devices too?

mgr01 commented 7 months ago

The only other Number-type item that's not passed through parseFloat() is chargerCapacityRemaining in functions/devices/charger.js.

Maybe it would make sense to call item.numericState everywhere instead of parseFloat(item.state) for all numeric items? The former uses parseFloat on .state anyways.

michikrug commented 7 months ago

Maybe it would make sense to call item.numericState everywhere instead of parseFloat(item.state) for all numeric items? The former uses parseFloat on .state anyways.

While this is available on the JS library... Is it also exposed through the REST API, where we are getting the state info from?

AFAIK, the API only returns state.

Just saw we also have such values on e.g. temperatures.

michikrug commented 7 months ago

I may just go through the rest of the code and also replace Number with parseFloat where it makes sense and add it to this PR

mgr01 commented 7 months ago

While this is available on the JS library... Is it also exposed through the REST API, where we are getting the state info from?

Right, makes sense. For some reason I thought that you're using JS library here.

Thanks.

michikrug commented 7 months ago

Done. Ready to merge I would say.