Got errors on my rule

Hi!

I got errors on my rules. I have tried this on openhab2.5 and openhab3 on pi4.
rule “Ute_Temperatur_Kompensasjon”

Here is the log:

2021-05-03 10:20:47.278 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘rule_varmekasett’ failed: ___ rule ___ “Ute_Temperatur_Kompensasjon”

when
Item energi_EN_UteTemperatur received up ___ date
then
var ten= 0.0
var ute = (energi_EN_UteTemperatur.state as DecimalType).doubleValue
var tto = ((21- ute) * 0.5) .doubleValue
var temp = 0.0

if (ute >= 21) {
temp = (5) .doubleValue
}
else if (ute >= 11) {
temp = (tto + 21) .doubleValue
}
else if (ute >= 0) {
ten = (11 - ute) .doubleValue
tto = (ten * 0.82) .doubleValue
temp = (tto + 26) .doubleValue
}
else if(ute < 0) {
ten = (ute * -1).doubleValue
tto = (ten * 0.2).doubleValue
temp = (tto + 35) .doubleValue
}
if (temp > 45) { temp = 45}
postUpdate(temperature_VK_SP_Inn, temp)
end

  1. The method or field rule is undefined; line 1, column 0, length 4
  2. The method or field when is undefined; line 2, column 35, length 4
  3. The method or field received is undefined; line 3, column 73, length 8
  4. The method or field update is undefined; line 3, column 82, length 6
  5. The method or field then is undefined; line 4, column 91, length 4
  6. The method or field end is undefined; line 28, column 675, length 3
  7. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 1, column 5, length 29
  8. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 3, column 44, length 4
  9. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 3, column 49, length 23

I dont know where to start since I got the error on the first line.

I tried a simple rule from the documentation, and the same happened.

Rune

Where are you placing this rule?

Troubleshoot the simple rule, instead of your complex one.

Hi rossko57.

I am using the UI and make it under script.

ROS

Here is one from the documentation

2021-05-03 12:55:28.928 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘test1’ failed: var Number counter
rule “startup”
when
System stated
then
counter = 0
end

  1. The method or field rule is undefined; line 2, column 19, length 4
  2. The method or field when is undefined; line 3, column 36, length 4
  3. The method or field stated is undefined; line 4, column 52, length 6
  4. The method or field then is undefined; line 5, column 61, length 4
  5. The method or field end is undefined; line 7, column 82, length 3
  6. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 2, column 24, length 9
  7. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 4, column 45, length 6

and the same happens here.

ROS

Don’t paste complete example rules meant for xxx.rules file format into the UI rules editor.
See -

Only the part between, but not including, “then” and “end” belongs in a UI script section.

Hi rossko57.

That helped a lot :slight_smile:

Hi.
I stil got problems.
2021-05-04 16:02:47.916 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘test’ failed: var ten = 0.0
var ute = KNXDevice_Energi_EN_UteTemperatur
var tto = ((21.0 - ute) * 0.5)
Type mismatch: cannot convert from NumberItem to byte; line 3, column 77, length 3

How do I get the right number type?
The KNX device give me the temperature and then I shall make som calculations.

I am running OH1 today, it have been running for 5 years without problem.
I having a new Raspberry PI 4 for OH 3.

This is the rule in OH 1 that I try to run on OH 3:

rule “Ute Temperatur Kompensasjon”
when
Item energi_EN_UteTemperatur received update or System started
then
var double ten = 0
var double ute = (energi_EN_UteTemperatur.state as DecimalType).doubleValue
var double tto = ((21- ute) * 0.5) .doubleValue
var double temp = 0
if (ute >= 21) {
temp = (5) .doubleValue
}
else if (ute >= 11) {
temp = (tto + 21) .doubleValue
}
else if (ute >= 0) {
ten = (11 - ute) .doubleValue
tto = (ten * 0.82) .doubleValue
temp = (tto + 26) .doubleValue
}
else if(ute < 0) {
ten = (ute * -1).doubleValue
tto = (ten * 0.2).doubleValue
temp = (tto + 35) .doubleValue
}
if (temp > 45) { temp = 45}
postUpdate(temperature_VK_SetPunkt, temp);
end

ROS

That would never have worked in OH1 either.
Aren’t you interested in that Item’s state ?

I changes line 2 to be equal the one in OH1:
var double ute = (KNXDevice_Energi_EN_UteTemperatur.state as DecimalType).doubleValue

I got this error:
2021-05-04 17:50:47.581 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘test’ failed: Could not cast 8.5 °C to org.openhab.core.library.types.DecimalType; line 2, column 19, length 54

Your Item is not the same type that it was in OH1.
You have a Number:Temperature type now.

Thanks.

I changed to:
var ute = (KNXDevice_Energi_EN_UteTemperatur.state as QuantityType).toBigDecimal

and no errors, yet :slight_smile:

I am getting closer.

This is the code now:

var ute = (KNXDevice_Energi_EN_UteTemperatur.state as QuantityType).toBigDecimal
var tto = ((21.0 - ute) * 0.5)
var temp = 0.0
var ten = 0.0
if (ute >= 21) {temp = 5.0}
else if (ute >= 11) {temp = tto + 21.0}
else if (ute >= 0) {
ten = (11.0 - ute)
tto = ten * 0.82
temp = tto + 26
}
else if (ute < 0) {
ten = ute * -1.0
“for this line I get this error:
Type mismatch: cannot convert from BigDecimal to double; line 13, column 326, length 10”
tto = ten * 0.2
temp = tto + 35
}
if (temp > 45) {temp = 45}

I hope it is no problem.

Hi rossko57

When I have this code : ten = ute * -1.0
I get this eror:
Type mismatch: cannot convert from BigDecimal to double; line 16, column 350, length 10

When I write it like this: ten = -1.0 * ute no error and everything is working.

Can there be a bug here?

No, it’s just DSL rules being picky about types.
When it sees an expression like
ute * -1.0
there are no types, it has to guess what to use.
First thing it finds is variable ute, earlier on that was declared as a BigDecimal type, so that’s what it will try to use to evaluate the expression.
Next is the operator -, minus, that’s okay it can do minus with a BigDecimal.
Next is the text “1.0”, the rules interpreter defaults to parsing that as a double type.
Oh dear, types don’t match, have to do a conversion - it tries BigDecimal to double which doesn’t work (that’s a curious Java omission).

Written the other way
-1.0 * ute
the types come up in a different order, and a double to BigDecimal works okay.

In general in DSL it is best to not type things and let the interpreter sort it out. But sometimes you just have to play with types in these situations.