Rules and Mathematics

Hello,

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: https://www.wetterochs.de/wetter/feuchte.html

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?

Please share the code you are using that produces this error.

@rtvb here it is:

rule "tb"
when
	Item    HUM_AUS_Garage          received update or
	Item    TEMP_AUS_Garage         received update
then
	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)
end

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.

I quickly can see some things from the code

  1. You do not explicitly declare the variable types form temp, hum, sdd, dd and af. From personal experience Java might require some type conversion
  2. I don’t know what type temp and hum will become if you implicitly assign them to the state of the item
  3. 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

1 Like

Thanks @ThomDietrich

that did the trick!

Hello Antares,

is it possible that you posed the working code?

That would help me!

Thank you!