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:
If I do something like this, it fails:
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
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:
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.