DSL rules order of evaluation like C?

I have a DSL rule IF statement in OH2 like this:

        if (barlights.state !== null && barlights.state as Number == 0)

If the first half of the ‘if’ fails, will the second half be evaluated?

I am getting an error on this:

Rule 'sensor1 lux': Could not cast NULL to java.lang.Number; line 243, column 41, length 25

The line/column reference points to the start of the second part. In fact, barlights.state is indeed NULL.

In C, if the first part of an AND fails, it won’t evaluate the second half. Does the rules DSL do the same?

No, it won’t.

But the state of your Item is not null, the void condition for java. It’s never ever null.

Your Item exists, and just as it has a name property, it has a valid state property.
New born Items are created with a state of NULL. That can’t be cast as a number.
Note also that any Item can also be given the state UNDEF by rule or binding.
Recent discussion -

Thank you for the reference. This is meaningful:

Indeed, when I print out “barlights” this is what I get:

barlights (Type=DimmerItem, State=NULL, Label=Bar Lights, Category=null)

So to fix my IF clause, is it as simple as this?

if (barlights.state != NULL && barlights.state as Number == 0)
1 Like

No, it’s not, as both conditions will be checked, regardless if the first condition is met or not. Instead you have to do it this way:

if(barlights.state instanceof Number) { // better than test for NULL
    if((barlights.state as Number) == 0) {
        // both conditions are met
    }
}

Please be aware that a number Item can hold more than numbers and NULL (UNDEF), so a test if the state is of type Number is far more easy.

1 Like