You are right, I was imprecise. But let me express my frustration (which may be similar to the one felt by the OP author) by detailing how I figured out a solution. If this is not the right thread you can delete it or move it elsewhere.
I knew from OpenHAB1 times that the item state has to be converted to DecimalType or Number: but why should I convert it twice? and why doesn’t the interpreter complains with a type mismatch error or something similar?
Let me detail my situation: in this way maybe I (an possibly others) will learn something more about Java object oriented programming.
Items definitions
Number Te_Br_CameraAnna "Temp Anna [%.1f %unit%] " <temperature> {channel="broadlink:a1:3a0e28c9:temperature"} // actual temperature
Number:Temperature TempAnnaLow "Stanza Anna Temp Bassa [%.1f]" //set point
sitemap definition of the set point
Setpoint item=TempAnnaLow label="Temp Bassa [%.1f %unit%]" minValue=15 maxValue=25 step=0.5
NON WORKING thermostat rule (excerpt)
var Number ActualTemp
var Number Delta=0.2
var Number setPoint
var String tmp
var Number Temp
setPoint = (TempAnnaLow.state as Number)
ActualTemp = (Te_Br_CameraAnna.state as Number)
Temp=ActualTemp - (setPoint - Delta)
tmp="This condition gives a wrong result " + Temp
logInfo("ThermostatRule",tmp)
if((ActualTemp - (setPoint - Delta)) <= 0 {
// switch on thermostat etc...
}
WORKING thermostat rule (excerpt)
var Number ActualTemp
var Number Delta=0.2
var Number setPoint
var String tmp
var Number Temp
setPoint = (TempAnnaLow.state as Number).floatValue // added further conversion
ActualTemp = (Te_Br_CameraAnna.state as Number).floatValue // added further conversion
Temp=ActualTemp - (setPoint - Delta)
tmp="This condition gives the right result " + Temp
logInfo("ThermostatRule",tmp)
if((ActualTemp - (setPoint - Delta)) <= 0 {
// switch on thermostat etc...
}
I figured out the solution of adding “.floatValue” by debugging line by line and thanks to the the fact that the conversion to string works for almost everything and let you print out the content of the variables no matter what they are. But I am confused and let me explain why.
setPoint, ActualTemp and Delta are declared as “Number” variables
When I assign a value to setPoint and ActualTemp I convert to “Number” with the “as Number” syntax.
Delta is a “Number” that I directly defined in the rule (though without units).
I was very surprised to see that SetPoint-Delta was giving a wrong result: after all, they were two “Number” variables!
I found somewhere in the forum (I was unable to find it in the docs, probably because this is obvious to Java programmers) about the required further conversion to float by adding “.floatValue”.
To me this behavior appears more like a bug than a feature, but I’m not a Java programmer therefore I ignore a lot of subtleties.
In any case, let me conclude as in my previous post: OpenHAB2 is an open source project developed for free by a lot of skilled developers willing to share their knowledge and used by experienced users willing to help less experienced ones: the drawback is that documentation is not complete or fully up-to-date, and an help-desk is not available. This is ok for me, but I understand that someone else may not be happy with that.
Lionello Marrelli