openhab / openhab-docs

This repository contains the documentation for openHAB.
https://www.openhab.org/docs/
Other
268 stars 682 forks source link

Explain how to compare QuantityType values when using UoM #782

Open wborn opened 5 years ago

wborn commented 5 years ago

The documentation should explain how to compare QuantityType values when using UoM.

It's unknown to many that QuantityType values can be used directly in comparisons. Often users think they should get hold of the numerical value by using a method or parsing the string value. Instead you can use comparison operators directly, e.g.:

if (TemperatureItem.state >= 20|°C) {
    // insert code here
}

For examples, see the following community threads:


Eclipse SmartHome has a UoM Blog where this is explained in the "Scripts & Rules" section.


In the openHAB documentation this should probably be explained in: https://www.openhab.org/docs/configuration/rules-dsl.html#number-item

For discoverability a reference could be added to that section in: https://www.openhab.org/docs/concepts/units-of-measurement.html

CWempe commented 5 years ago

It should also be mentioned that using double quotes is the preferred way, because some units (like `W/m²") won't work otherwise.

Another scenario that might be interesting for users is "math operations".

Personally, I am not sure how to add or subtract two of these new values. Can I just use Temp3 = Temp1 - Temp2? Or do I need to use .state like Temp3 = Temp1.state - Temp2.state? Or do I even need to use *.state as QuantityType<Number>).doubleValue? Will the result be a normal Number or an UoM?

For now I can workaround this by using Temp.historicState(now.minusMinutes(2)).state in my case.

But these examples would really be useful in the documentation.

Confectrician commented 5 years ago

Hey @wborn,

I would like to take care of this topic.

What we can't do here is taking care of the concepts article. The whole concepts section is cloned from and maintained in the eclipse/smarthome repo during the website build process.

Maybe we should think about what to improve over there and what to add in this repo. Maybe it would be the best to add some general information in the concepts article and just add som small example to the rules article.

Same goes for the suggestion from @CWempe. From what i read, this sounds like a generic information which is useful for all implementations and not only openHAB.

Would love to hear your opinions about this.

BR Jerome

wborn commented 5 years ago

It would be really helpful if you can add some docs about this @Confectrician!

Since it is a direct copy and applies to every product using ESH, I agree it's probably best to add some information about this in both the concepts and rules articles.

cubistico commented 5 years ago

I second this issue! I've just started to work with UoM, and I find it very confusing. It's hard to use UoM items in rules, and there seems to be no comprehensive description of sensible approaches. Searching the web, I find a lot of people complaining about this. I'm honestly concerned that it might severely discourage users .

How could I help?

mstormi commented 3 years ago

@Confectrician any progress?

Some example code would be really beneficial here. Showing how to assign, how to do math, how to compare two QuantityTypes or a Quantity type against a number.

The reference to the ESH docs is no longer valid as that eclipse project is archived.