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.
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
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…
EDIT:
var temp1 = (Buderus_Aussentemperatur.state as QuantityType<Temperature>).toUnit("°C")
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")