Rule with Unit of Measurement Speed [km/h] => slash to escape?

Hi all,

I have a rule and want to compare the maximum Wind speed of the last 15 Minutes to a defined max value to know if I want to re-open my awning after automatic closing due to wind. Nothing too special, but I get into issues for the comparison with the unit of measurements.

My item definition (using metric values, hence km/h):

Number:Speed            WetterstationWindgeschwindigkeit                  "Windgeschwindigkeit"                                                  <wind>           (gWetterstation)                                                                               ["Measurement","Wind"]           {channel="homematic:HmIP-SWO-PL:ccu3:xxx:1#WIND_SPEED"}

Principle rule part I think I want to use and output shows values without units (why?):

logInfo("Test",WetterstationWindgeschwindigkeit.maximumSince(now.minusMinutes(15)).state.toString)

I can not compare the way I did e.g. for temperature considering unit correctly as I got no solution escaping the slash of km/h

 if (WetterstationWindgeschwindigkeit.maximumSince(now.minusMinutes(15)).state <= 40 |km/h) {

I also took time in the community already and found this statement; working in my test rule but not when the items triggers a change itself!?

    val Number maxWind = (WetterstationWindgeschwindigkeit.maximumSince(now.minusMinutes(15)).state.toBigDecimal as DecimalType)

Any hint how to solve this reliable for unit “km/h”?

Thanks!

I don’t think the persistence actions return a value with units. So you should be able to just use 40 in the comparison, assuming the Item state is km/h and saved to persistence as km/h.

You can easily tell this is the case by logging the result of the maximumSince. If it doesn’t have anything after the number, it doesn’t have units.

You might need to cast the value to Number but I’m not certain.

WetterstationWindgeschwindigkeit.maximumSince(now.minusMinutes(15)).state as Number

Thanks!
This is somehow unexpected but easy to solve then.

Just for curiosity: Does someone know - assuming I use the item instead of historic values - how I can compare correctly with km/h as unit?

I don’t really use Rules DSL anymore and my knowledge is getting rusty. But if the standard escaping doesn’t work using a \ you will have to create a new QuantityType.

val fourtyKmph = new QuantityType("40 km/h")

and then compare to fortyKmph.

Thanks

Not sure how I would use this! What is even more strange: The same (!) log shows sometimes with and sometimes without units, making any logic impossible to react on it reliable.
As I can trigger the test rule via testitem state change, I just guess there might be some caching involved that changes if there is a unit involved.
Any hint? Is this intended/a bug?

val fourtyKmph = new QuantityType("40 km/h")
 if (WetterstationWindgeschwindigkeit.maximumSince(now.minusMinutes(15)).state <= fourtyKmph) {

This is impossible in OH 4.

What’s updating this Item? If it’s a binding and only a binding it’s a bug in the binding. If it’s updated from multiple sources, all updates should include units or not include units.

Note, the values pulled from persistence do not include units, ever.

Hi,

persistance gives me units as well as no units for same item! This made me freak out. Currently I convert to string, kill km/h if available and convert back to number. Works but that is probably the most ugly thing I ever did!

You’ll need to post some examples. Persistence doesn’t save units. The docs for all the persistence actions indicate that they return either a HistroicState or a Number, never a QuantityType.

It might be the case that if it returns a HistoricState, that state is converted to a QuantityType where there’s a State Description pattern telling it to. But it’s going to be deterministic. Either the HistoricState always returns a QuantityType or never does for a given Item. But actions like minimumSince, sum, average, et. al. will never return a QuantiyType.

At least in OH 3.x. I need to double check in OH 4 with the new changes to UoM handling. It’s now more reasonable for persistence to apply units even in these cases but I don’t recall seeing a PR that does that yet.

Thanks for the answer. I’ll share code soon. My system is stillmon OH 3.4

Best