In my Awair binding, I copied the Foobot binding which has a configuration setting for switching the temperature to Fahrenheit. But now I see that there’s a system setting for the measurement unit system, so this setting seems unnecessary.
My question is: does each binding have to access this system setting and change the temperature value accordingly (if so, could anyone point me to where you access this system setting in Java?). Or does the system automatically update all “Number:Temperature” values to reflect the global setting?
The system takes care of the conversion based on what the user has defined in their Regional Settings. In your binding, you just need to update the channel’s state using QuantityType.
I think (I’ll need to check the code) that it’s passing a different state unit to QuantityType depending on whether the setting is Fahrenheit. Is that what you mean? I’ll check it tonight.
I didn’t see a (user) configuration settting? What I see in the foobot binding is that it checks what the foobot device returns. Depending on what the unit returns it sets the value in QuantityType.
Once it’s in a QuantityType, what is shown depends on user settings/configuration. But this is independent on what the bindings stores as value.
You may be right. I may be misremembering the Foobot binding.
There are two aspects I want to confirm:
The Awair API allows you to request Fahrenheit values with a parameter in the URL. As I understand it, this is not required, because the system does the conversion.
When I create the QuantityType, I need to check the system setting for Imperial/metric and set the unit accordingly, correct?
Correct. With the foobot binding the user can configure the foobot to set what unit it uses. The generic api returns the value in that configured unit. So it must be determined from the data returned what the unit is. If you can control what unit should be returned by the Awair device such a check is not needed as you always know what unit the returned value is in.
No this is not needed. You can simply set the value in the QuantityType using the unit you have the data in. So if the value is in Celsius set the QuantityType with unit Celsius. If the user wants to see it in Fahrenheit openHAB will do the conversion even if you had set the value in Celsius.
I removed the Fahrenheit option and all the other gubbins, and it does work as you suggested - the temperature unit adapts to the system setting.
However, I now notice that no unit symbols appear for the values (C, %, etc.). I’ve compared the items to other items from other devices, like the Tado, and the definitions appear the same (e.g. Number:Temperature). Also, using this log entry in the binding:
logger.debug("Final QuantityType value: {}", new QuantityType<>(new BigDecimal(value).setScale(nDecimalPrecision, RoundingMode.HALF_UP), stateUnit).toString());
I see the output that I would expect to see, e.g. 27 % for humidity, 563 ppm for CO2, 21.25 °C for temperature, etc. (except that the log, which is not Unicode, shows 21.25 °C). Since this QuantityType is returned as the state, why don’t I see the units?
Where are you looking at? (And I don’t mean just saying it’s the ui…) And maybe also post your configuration or what you did configured. (Did you use text configurations and didn’t you put the %unit% in the label text?)
The code looks fine to me. So I suspect it be a PaperUI problem. You could check if it is also a problem when showing items in BasicUI. Also PaperUI isn’t a good test environment since it has been removed in openHAB 3. So if you see anything wrong in PaperUI better check other UI’s or switch to openHAB 3.
Are you planning to create a PR for this? I have an Awair device and now I just created a small Python script which does the integration, but I’m interested in this binding.
I am looking for an openhab compatible air quality monitor. As far as I can tell so far the Awair seems to bethe best option. If there is someone actively integrating it into openhab then thats a big plus for me.