UoM calculations

I’d like to calculate with temperature items. so I thought I must convert the UoM to a simple Number, but it doesn’t work. I also tried as DecimalType, as QuantityType, .toString, nothing seems to work.

Number:Temperature      Buderus_Aussentemperatur      "Aussentemperatur [%.1f %unit%]"        <temperature>               {channel="km200...
var Number temp= (Buderus_Aussentemperatur.state as Number)
var Number hyst = temp + 0.3
logInfo("loggerName", "message: "+  hyst + "  " + temp)
[se.smarthome.model.script.loggerName] - message: 276.85  3.4 °C

whats wrong here?

Number still will hold the unit. I guess you want to get rid of the unit, so:

var Number temp= (Buderus_Aussentemperatur.state as Number).floatValue

should work.

or you can do

val Temperature = (Weather_Temperature.state as QuantityType<Temperature>) 

if(Temperature > 15|°C){
   // do stuff
}

but i personally like more

var Number temp= (Buderus_Aussentemperatur.state as Number).floatValue

it’s cleaner I think

Better to calculate with the quantities with units.

Also shows you how to get “just the number” in the unit of your choice, if you rally must go that way.

var Number tempC= (Buderus_Aussentemperatur.state as QuantityType<Temperature>).toUnit("°C")  
// that gets temp in C regardless of original unit
var Number hyst = temp + 0.3 | °C
// that adds a quantity in C, result is a quantity in C
logInfo("loggerName", "message: "+  hyst + "  " + temp)

EDIT - my mistake, do not use var Number here, just var will do

thanks for the explanation!

doing averageSince drops the unit.

var Number avgtemplast10min = Buderus_Aussentemperatur.averageSince(now.minusMinutes(10))

is it also possible to add the unit again somehow?

It shouldn’t, in OH3.1

But don’t do that -
var Number avgtemplast10min = ...
let it be the quantity type it wants to be
var avgtemplast10min =

I shouldn’t have left those in my earlier example either.

Just remembered - persistence retrieval reconstructs the unit from the default of the Item. That is set in the Item’s ‘pattern’ metadata, check that.

ah, ok, I’m still on OH2.5. maybe thats also the reason why

var Number hyst = (temp1 + 0.3 | °C)

does not work:

var temp1 = Buderus_Aussentemperatur.state
var Number hyst = (temp1 + 0.3 | °C)
logInfo("loggerName", "temp1: "+  temp1 + " hyst: " + hyst)

throws error:

2021-12-03 10:48:34.487 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ' test2': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_plus(java.lang.Object,java.lang.String) on instance: null

however, holidays are coming, time to move to OH3… :slight_smile:

EDIT:

var temp1 = (Buderus_Aussentemperatur.state as QuantityType<Temperature>).toUnit("°C")

works!

OH2 persistence disregards UoM altogether, so yes .averageSince() will return only numbers.
There is no way to determine what units were in use when the number-only part was recorded, so you have to hope for the best (and not fiddle with the units over time).

This works in OH2, though validators may complain

var xx = new QuantityType("0.3°C")
// so by extension
var xxx = new QuantityType( randomNumber.toString + "°C")

probably more elegant ways to do it

1 Like