Unit of Measurement and inline expressions

I’m trying to configure some labels in the main UI overview to be a bit more dynamic based on conditions, but I’m struggling how to deal with units of measurement being bundled into the state by default on OH4.

For example, if I want to do a ternary operator on whether a temperature is above a certain threshold, I can’t because it’s a string:
image

If I do something like this, it fails:
image

I’m struggling to find the documentation to handle this in these expressions.

Running on openHAB 4.1.0.M1 / Linux 5.14 / Java 17.0.8

This works (I think).

=items.testitem.state > "0 °C"

As I looked up in one of my rules, handling units in comparisons should work like: item.state > 0 | "°C"

The widget expressions do not use a full javascript engine and the js-like expression parser that is used has no concept of OH Quantity types or UoM. You simply have to use the standard javascript methods for extracting a number value from a string. In this case, your best option is probably:

=Number.parseFloat(items.Temp5_Temperature.state) > 0
2 Likes

That will be a String comparison. As a result “10 °C” < “2 °C” because “2” comes after “1” alphabetically.

Widgets do not use Rules DSL. The | syntax is specific to Rules DSL.

It’s worth noting that Number.parseFloat stops parsing when it encounters the first non-number character and returns the value parsed thus far instead of throwing an error. This effectively trims the units off of the String giving you the raw number as a number. That’s why this approach works.

1 Like