Boolean variable in a rule: problem with OH2?

I have a problem with a rule working in OH1 but not in OH2.

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

var boolean initMinimoteCourt = true

rule "Minimote appui court"
when
    Item MiniMoteSceneActivated received update
then
    if (initMinimoteCourt) {
        initMinimoteCourt = false
    }
    else {
        var Number bouton = MiniMoteSceneActivated.state as DecimalType
        ...
    }
end

I get this error at startup:

21:42:57.614 [ERROR] [.script.engine.ScriptExecutionThread] - Error during the execution of rule 'Minimote appui court': The name 'initMinimoteCourt' cannot be resolved to an item or type.

What could be wrong ?

Please try var Boolean instead of var boolean :slight_smile:

So does OH1’s rule language support native types but the equivalent in OH2 does not support native types?

I can confirm that using boolean or Boolean is identical.

Doing new tests, I discovered that the rule is working well, even if I have this error in the logs. So this error is not major at all.

I tried to comment the if in the rule and now the error is the same but relative to MiniMoteSceneActivated rather than initMinimoteCourt. So the problem has finally nothing to do with the boolean variable declaration.

My hypothesis is that the rule is run while nothing is “initialized” including the boolean variable.
I see this log entry only after the error:

23:08:27.041 [INFO ] [marthome.event.ItemStateChangedEvent] - MiniMoteSceneActivated changed from NULL to 1

This rule is the only one of kind “Item xxxxxxxxxxx received update”.
Is there a reason that these rules would be called very early in OH2 while things (item itself AND variables) are not yet defined ?

I had a bit of play with booleans (in OH1) with unexpected results

The bottom line seems to be -
Don’t use var boolean XXX = …
Use instead var XXX = new Boolean ( … )

1 Like