Decimal precision for temperature in Awair binding

Hi

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">
		<item-type>Number:Temperature</item-type>
		<label>Temperature</label>
		<description>Temperature</description>
		<category>Temperature</category>
		<state readOnly="true" pattern="%.2f %unit%"/>
	</channel-type>

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">
		<item-type>Number:Dimensionless</item-type>
		<label>Humidity</label>
		<description>Humidity level</description>
		<category>Humidity</category>
		<state readOnly="true" pattern="%.2f %%"/>
	</channel-type>

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.