I want to calculate the absolute humidity in a room to decide about ventilation. Therefore I use calculations that require exponents for formulas as described here:
Unfortunately when I have a line like
val sdd = 6.1078 * 10^((7.5*temp)/(237.3+temp))
I get the error
Configuration model 'air_climate.rules' has errors, therefore ignoring it: [33,24]: no viable alternative at input '^'
[35,14]: no viable alternative at input '^'
I didn’t find a description or guide about mathematics in rules, so I would be glad if somebody could give me a hint whether this is possible at all.
Thanks a lot in advance!
Okay - figured it out myself: good old Java does it with Math.pow(x,y) for x^y.
But now I get an error that says
[ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule tb: An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.DoubleExtensions.operator_multiply(double,byte) on instance: null
What could be the cause for that?
August 29, 2017, 9:21am
Please share the code you are using that produces this error.
@rtvb here it is:
Item HUM_AUS_Garage received update or
Item TEMP_AUS_Garage received update
val temp = TEMP_AUS_Garage.state
val hum = HUM_AUS_Garage.state
logInfo("AUTOMATION", "TEMP: " + temp + " / HUM: " + hum)
val sdd = 6.1078 * Math.pow(10,((7.5*temp)/(237.3+temp)))
logInfo("AUTOMATION", "SDD = " + sdd)
val dd = hum/100 * sdd
logInfo("AUTOMATION", "DD = " + dd)
val af = Math.pow(10,5) * (8314.3/18.016) * dd/(temp+273.15)
logInfo("AUTOMATION", "AF = " + af)
It must happen already in the line that defines sdd, as the log doesn’t show the subsequent output. Yet, the log shows the preceding output for temp and hum:
[INFO ] [se.smarthome.model.script.AUTOMATION] - TEMP: 23.5 / HUM: 63
Thanks for your help!
EDIT: TEMP_ and HUM_ are defined as Number in the items file.
August 29, 2017, 9:33am
I quickly can see some things from the code
You do not explicitly declare the variable types form temp, hum, sdd, dd and af. From personal experience Java might require some type conversion
I don’t know what type temp and hum will become if you implicitly assign them to the state of the item
For logInfo you might need to convert numeric to string to get it to work, like
"SSD = " + ssd.toString.
I’ve declared them now as doubles which leads to a slightly different error:
[ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule tb: An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.DoubleExtensions.operator_multiply(double,double) on instance: null
The parameters are now double, double but still I don’t get where the “null” comes from as the variables have values which are shown in the preceding log output.
One example of how to go from an item to a simple float value:
val actTemp = (Heater_Act_Temp.state as DecimalType).floatValue
This and much more can be found at
Attention: This article is now also available on the openHAB 2 documentation page. If you want to add improvements or additions, please do so there.
Interacting with Items in rules
A frequent operation in rules is to calculate other values from Item states or compare Item states against other values or perform other operations that are either responses to changes in item states or intended to change the item state. For a complete…
February 17, 2022, 6:46pm
is it possible that you posed the working code?
That would help me!