From f5eded6370704cb9cec2da8f14097e1e20d040fc Mon Sep 17 00:00:00 2001 From: Henning Treu Date: Mon, 14 May 2018 20:47:40 +0200 Subject: [PATCH] Add QuantityType conversion to rules documentation (#675) Signed-off-by: Henning Treu --- configuration/rules-dsl.md | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/configuration/rules-dsl.md b/configuration/rules-dsl.md index 0af516065..286af0103 100644 --- a/configuration/rules-dsl.md +++ b/configuration/rules-dsl.md @@ -512,11 +512,13 @@ val PointType location = Device_Coordinates.state as PointType ##### Number Item -A Number Items carries a **DecimalType**. -A DecimalType is also a java.lang.Number so all the conversions listed above under Dimmer Item apply to Number Item as well. +A Number Items carries either a **DecimalType** or a **QuantityType** in case the Number Item has a dimension attached (e.g. `Number:Temperature`). +DecimalType and QuantityType are also java.lang.Number so all the conversions listed above under Dimmer Item apply to Number Item as well. Here some other commonly needed conversions: +* For DecimalType states: + ```java //convert integer_number to string containing hex_code var String hex_code = Long.toHexString(integer_number); @@ -530,9 +532,30 @@ var MyNumber = Long.parseLong(hex, 16) as Number var DecimalType parsedResult = DecimalType.valueOf(Long.parseLong(hex_code, 16).toString); ``` +* For QuantityType states: + +```java +// define a QuantityType variable +var myTemperature = 20 [°C] + +// convert a quantity state into a different unit: +var fahrenheit = myTemperature.toUnit("°F") + +// convert scalar value to DecimalType +var myDecimal = new DecimalType(fahrenheit.doubleValue) // myDecimal == 68 + +// access scalar values as int, double, float +var myInt = fahrenheit.intValue +var mydouble = fahrenheit.doubleValue +var myfloat = fahrenheit.floatValue + +// check if a number item state is a QuantityType +var isQuantity = myItem.state instanceof QuantityType +``` + Other useful conversions can be found under Dimmer Item. -One warning comes with DecimalType. +One warning comes with DecimalType (also holds true for QuantityType). The full explanation is [beyond the scope of this introduction](https://community.openhab.org/t/ambiguous-feature-call-whats-wrong-designer-user-or-bug/9477/4). To avoid an error mentioning an "Ambiguous Method Call" always cast the state of a DecimalType to a Number, not DecimalType.