[SOLVED] Converting UoM units in rules (e.g. m/s to km/h)

I want to display a minimum - maximum range of values having UoM (Units of Measurement).

As far as I know, there’s no way to attach a (min, max) tuple to an Item where the Item value could hold 2 placeholders, as in the fictitious example below:

Number:Speed	Wx_OWM_Forecast_Wind_Speed_Range_Day0 "Wind Speed [$1%.0f km/h - $2%.0f km/h]" <wind>	(gWeatherForecastDaily)

(This would assume that a channel could return tuples)

Right now I resort to rules to generate a String item state value, as in:

val String itemName = "Wx_OWM_Forecast_Wind_Speed_Range_Day0"
val String valueMin = wind_speed_min_day_0
val String valueMax = wind_speed_max_day_0
val String valueRange = valueMin + " - " + valueMax
val GenericItem forecastRangeItem = ScriptServiceUtil.getItemRegistry.getItem(itemName) as GenericItem
postUpdate( forecastRangeItem, valueRange )

Where these items are defined as:

String			Wx_OWM_Forecast_Wind_Speed_Range_Day0	"Wind Speed [%s]"				<wind>
Number:Speed	Wx_OWM_Forecast_Wind_Speed_Min_Day0		"Min Wind Speed [%.0f km/h]"	<wind>	(gWeatherForecastDaily)
Number:Speed	Wx_OWM_Forecast_Wind_Speed_Max_Day0		"Max Wind Speed [%.0f km/h]"	<wind>	(gWeatherForecastDaily)

When displaying these items in Paper UI, I get the following UoM output:

  • Wx_OWM_Forecast_Wind_Speed_Min_Day0: km/h (as expected)
  • Wx_OWM_Forecast_Wind_Speed_Max_Day0: km/h (as expected)
  • Wx_OWM_Forecast_Wind_Speed_Range_Day0: m/s (want to see as km/h)

I’m looking into ways to convert the units from Number item values that have units, e.g; from m/s to km/h.

Can this be achieved?

Yes, see https://www.openhab.org/docs/configuration/rules-dsl.html#number-item



I see I first have to cast Number to QuantityType<Number> before I can use the toUnit("km/h") conversion, as in:

val String minStr = ( valueListMin.get(index) as QuantityType<Number> ).toUnit("km/h").toString()