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