Decimal precision for temperature in Awair binding


I’ve pretty much finished my Awair air quality sensor binding, but I had to force the precision of the temperature value to two decimal places in code:

return new QuantityType<>(new BigDecimal(value).setScale(2, RoundingMode.HALF_UP), stateUnit);

value, which contains the temperature reading in this case, has a crazy level of precision (10 decimal places or more). The state unit is SIUnits.CELSIUS (when using metric measurements). The corresponding channel is defined in thing-types.xml as follows:

	<channel-type id="temperature">
		<state readOnly="true" pattern="%.2f %unit%"/>

but the formatting seems to be ignored. I had the same problem with humidity, which came with a ton of decimals. Humidity has the state unit SmartHomeUnits.PERCENT. This is the channel definition:

	<channel-type id="humidity">
		<description>Humidity level</description>
		<state readOnly="true" pattern="%.2f %%"/>

I set the precision to zero in code in the same way as temperature.

Is this the right way to do it, or is there some other configuration somewhere? Thanks.

You can add metadata to each item - something like state description

In the xml file?

You are doing it the right way. The value internally is whatever value you pass to the updateState. Therefore if the device has a very high resolution and you want less precision you need to reduce it yourself.
Without knowing what values you have. Sometimes ‘high precision’ is caused by float to double or the other way around conversion problems. Then the values have lots of 9’s likely. It can be the device does this or your code mixes floats and doubles. In such cases see what you can get by trying different types.
The definition in the xml is for visualisation. So pattern is used in the ui (unless the user configures a different pattern)

1 Like

It really does seem to be the API that is returning those two values to 15 decimal places(!).

I’m still not clear what the pattern does. What would happen if I removed the formatting from the pattern? I’ll try it later.