number:Temperature in calculations

Platform: OH 4.0.0.M3

I have a Sonoff TH10 flashed with Tasmota and a DS18B20 temperature probe attached. Here is the channel definition:

  - id: Temperature
    channelTypeUID: mqtt:number
    label: Temperature
    description: null
      stateTopic: tele/aqs/SENSOR
      transformationPattern: REGEX:(.*Temperature.*)∩JSONPATH:$.DS18B20.Temperature

The Temperature channel is linked with a number:Temperature item. OH correctly assigned a unit of °C to this item. I am able to use item.state in comparisons but I don’t know how to get its decimal value, perform some calculations, and return a int value in rules DSL (to be used as a pulsetime).

Can you pls help me ? Thanks

If your Item is of type Number:Temperature, it’s an UoM (Units of Measurement) Item.

First things first, it would be better to set a proper unit in the channel (Advanced Options)
Set unit to °C to get proper values (given that the value is in °C)

Second, a state is a state and not a number. You’ll have to cast the state as a Number.
To make things worse, you’ll have to check that the state is of type Number, before casting.

Not to mention, an UoM Item will provide the unit in addition to the value, e.g. 15.4 °C instead of 15.4.
So you’ll have to get rid of the unit:

if(!(item.state instanceof Number))                     // is the state not of type N umber?
    return;                                             // then stop the rule

var Double dMyValue = item.state as Number).doubleValue // get rid of unit

Edit: lowercased letter d :slight_smile:

1 Like

Thanks. Done some more investigation (using this thread). This works for me:

var int pulse
var numericC = (aqs_temp.state as QuantityType<Number>).toBigDecimal
pulse = (100 + (45 - numericC) * 180).intValue

Edit: var numericC = (aqs_temp.state as Number).DoubleValue as suggested by @Udo_Hartmann fails with error The method or field DoubleValue is undefined for the type Number

Typo, should have been a small d :slight_smile: .doubleValue

Number has methods for different Types, .doubleValue, .floatValue, intValue

1 Like

Confirmed to work. Thanks.