Why i got error in my OH2 rule? (in OH1 is not)

I moved to OH2, in OH1 this rule works ok.
In OH2 i got error - why?

if (ESP_Gas_Ping.state == ON && ESP_Gas_Lightness.state as DecimalType <= 10) { ..... }

18:21:10.925 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'test': An error occured during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.BooleanExtensions.operator_and(boolean,boolean) on instance: null

But if i try without “as DecimalType” - no errors.

if (ESP_Gas_Ping.state == ON && ESP_Gas_Lightness.state  <= 10) { ..... }

Something changed in OH2?

Hello @martiniman

Try the if this way

if ((ESP_Gas_Ping.state == ON) && (ESP_Gas_Lightness.state as DecimalType <= 10))

Maybe you don’t need the “as DecimalType” as well…

regards
Michael

Hi, @michael_sutter - I tried your way - error.

Are you certain that ESP_Gas_Lightness has a state (i.e. isn’t NULL)? The error and behavior points to the state not being a DecimalType which, if ESP_Gas_Lightness is a Number Item means that its state is NULL.

These kinds of errors tripped me up a couple of times when I transitioned to OH 2. Some rules I wrote involving Items that have had a state for months couldn’t handle it when the state was NULL.

I add logWarn line before, ESP_Gas_Lightness.state=3, and same error.

00:13:50.995 [WARN ] [ipse.smarthome.model.script.Lighting] - ESP_Gas_Ping.state=ON ESP_Gas_Lightness.state=3
00:13:51.001 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'test': An error occured during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.BooleanExtensions.operator_and(boolean,boolean) on instance: null

When you try it without DecimalType, you say there are no errors but does it work? Does the condition evaluate properly?

Yes it work. I try change ESP_Gas_Lightness.state to 11 rule not fire, to 0 - it fires.

Hmmmm. My only theory is that some minor changes have been made to DecimalType which makes it not work as expected when using it with math.

Personally I recommend casting it to Number instead of DecimalType. You almost always want to treat it as a Number after casting it anyway and I believe casting to Number will work in this case.

However, as you are seeing, apparently we no longer need to cast it to anything in order to successfully do math with it so perhaps that is the more correct answer.

OH designer shows error:

Casting to Number - same error.
If i change <= to == designer shows no error.