OH4 how to define UoM in binding configuration - units can't be set manually


I’m totally confused, maybe I’m doing something wrong. Since the update to OH4 my binding only shows distances in m instead of km and pressures in hPa instead of bar. The values are technically correct (I set 1km and get 1000m), but this is according to the documentation not how it should be.

So I tried to set the right unit in the channel-config, but still the unit is wrong.

This is an example channel config:

	<channel-type id="range-fuel-channel">
		<label>Fuel Range</label>
		<state pattern="%d km" readOnly="true"/>

and in the code I set the value like this:

Unit<Length> lengthUnit = MetricPrefix.KILO(SIUnits.METRE);
QuantityType<Length> qtFuelRange = QuantityType.valueOf(rangeFuel, lengthUnit);
updateChannel(CHANNEL_GROUP_RANGE, RANGE_FUEL, qtFuelRange, channelToBeUpdated);

So in two places I tell the system to use km, but still the value is converted to m. I could for sure configure each distance channel/item unit to km, but every user of the binding would have to do it.

What’s even worse is that a custom unit is removed from this channel:

	<channel-type id="estimated-fuel-l-100km-channel">
		<label>Estimated Fuel Consumption l/100km</label>
		<state pattern="%.2f l/100km" readOnly="true"/>

I tested the behavior in a fresh OH4 installation.

Do I miss a thing?


According to which documentation.

In OH 4 the whole way units are handled on Items has changed (see the “Changes in UoM” in https://github.com/openhab/openhab-distro/releases/tag/4.0.0). Bindings no longer have the ability to dictate the units that an Item carries. I think they can still set the state pattern (as you show you are doing) but the actual state that the Item carries is defined either by the new unit metadata or, if that is not defined, by the system default, which is m for Number:Length.

Without unit defined and no override of the state description pattern on the Item, given the above XML, I would expect the Item’s state to be in m and where ever in MainUi that the state description pattern is used to show the state to be shown in km.

Thanks @rlkoshak , I hoped there would be a way to provide a default unit different to the system default so that not every user of the binding has to adjust the units manually…

I think I misunderstood this part

In some cases the system default unit may not be the most useful unit for a given quantity. For measuring precipitation km/h would be a quite uncommon unit, while mm/h would be the expected unit. You can set the unit metadata to a different unit to change the item’s unit

in combination with this one:

The state description pattern “%.1f %unit%” describes the value format as floating point with one decimal place and also the special placeholder for the unit

I thought replacing the %unit% with a different unit would result at least in a default state description if you create the item from the channel.

I think it should, but that is only going to be used for the display of the Item’s state. The actual Item’s state and value saved to persistence will be the one defined in the unit metadata, or the system default if the metadata isn’t defined.

So there might be something off going on here. I’m not sure. My understanding was that part UoM was left in place.

Ok maybe I’ll create a defect for that…

just for completeness: I got the state handling working fine with my code. The problem is that in the items overview the raw value is shown, so you don’t recognize that there is a different state shown in the UI.

1 Like