Does the system switch Number:Temperature between imperial/metric in all bindings?

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:

  1. 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.
  2. 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.

1 Like

Ahh, OK. I’ll give it a test.

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. :+1:

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? :thinking:

Anybody know why the units wouldn’t appear? This is literally the last thing that needs fixing before I can publish the addon.

It’s very hard to comment without seeing the code, the item definition, and where the units are not showing up.

Where are you looking at? (And I don’t mean just saying it’s the ui…) :sob: 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?)

Here’s where the units don’t appear (PaperUI):

The thing-types.xml definitions (which I thought I’d posted :man_facepalming:) are as follows:

	<channel-type id="time">
	<label>Last reading date</label>
	<description>The last time the sensor data was uploaded to Awair</description>
	<state readOnly="true"/>
<channel-type id="pm">
	<label>Particulate matter (PM 2.5 µg/m3)</label>
	<description>Particulate matter level (micrograms per cubic metre)</description>
	<state readOnly="true" pattern="%.1f %unit%"/>
<channel-type id="temperature">
	<state readOnly="true" pattern="%.2f %unit%"/>
<channel-type id="humidity">
	<description>Humidity level</description>
	<state readOnly="true" pattern="%.2f %unit%"/>
<channel-type id="co2">
	<label>Carbon dioxide (ppm)</label>
	<description>Carbon dioxide Level (parts per million)</description>
	<state readOnly="true" pattern="%d ppm"/>
<channel-type id="voc">
	<label>Volatile organic compounds (ppb)</label>
	<description>Volatile organic compound level (parts per billion)</description>
	<state readOnly="true" pattern="%d ppb"/>
<channel-type id="score">
	<description>Awair aggregate air quality score out of 100</description>
	<state readOnly="true" pattern="%d"/>

They’re a straight copy of the Foobot channels. Temperature and PM2.5 have the %unit% placeholder, and the others write the units in directly.

This is the item definition for temperature in PaperUI:

I’ve compared it to other temperature items that are showing the unit correctly in other bindings, and it seems to be the same.

And the code of the device handler which return the QuantityType is:

            logger.debug("Unit for {}: {}", sensor.getDataKey(), stateUnit);
            logger.debug("Final QuantityType value: {}",
                    new QuantityType<>(new BigDecimal(value).setScale(nDecimalPrecision, RoundingMode.HALF_UP),
            // Force decimal precision here
            return new QuantityType<>(new BigDecimal(value).setScale(nDecimalPrecision, RoundingMode.HALF_UP),

and in the log, the output is:

2021-01-07 17:05:47.819 [DEBUG] [.internal.handler.AwairDeviceHandler] - Unit for pm25: µg/m³
2021-01-07 17:05:47.823 [DEBUG] [.internal.handler.AwairDeviceHandler] - Final QuantityType value: 4 µg/m³
2021-01-07 17:05:47.833 [DEBUG] [.internal.handler.AwairDeviceHandler] - Unit for temp: °C
2021-01-07 17:05:47.835 [DEBUG] [.internal.handler.AwairDeviceHandler] - Final QuantityType value: 21.56 °C
2021-01-07 17:05:47.851 [DEBUG] [.internal.handler.AwairDeviceHandler] - Unit for humid: %
2021-01-07 17:05:47.852 [DEBUG] [.internal.handler.AwairDeviceHandler] - Final QuantityType value: 28 %
2021-01-07 17:05:47.867 [DEBUG] [.internal.handler.AwairDeviceHandler] - Unit for co2: ppm
2021-01-07 17:05:47.869 [DEBUG] [.internal.handler.AwairDeviceHandler] - Final QuantityType value: 633 ppm
2021-01-07 17:05:47.881 [DEBUG] [.internal.handler.AwairDeviceHandler] - Unit for voc: ppb
2021-01-07 17:05:47.883 [DEBUG] [.internal.handler.AwairDeviceHandler] - Final QuantityType value: 350 ppb

I could and probably should use %unit% for all the channels, but the units don’t appear for any of them.

Ah well. If anyone is interested in the binding, let me know and I’ll return to this problem. For now, it suits my purposes.

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.

In that case I suppose I’d better try upgrading.

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 had it in mind, yes. I need to get the v3 version aligned with the v2.5 version and get it on GitHub

Hi , Are you still working on the 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.

Yes, the 2.5 version is working. I just need to update the 3.0 code and get it on GitHub. I just haven’t had time.

1 Like