Could not cast NULL

Hi guys,

I’m getting an error that I’m not sure how to fix?

2020-03-27 11:10:58.622 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘LilyRuleTemp’: Could not cast NULL to java.lang.Number; line 7, column 50, length 31

rule "LilyRuleTemp"
when
    Item LB_Temperature changed
or
    Item LB1_Temperature changed
then
    var t = ((LB_Temperature.state as Number) + (LB1_Temperature.state as Number)) / 2
    Lilys_Bedroom.postUpdate(t)
end

any help will be appreciated

Many thanks

All Items start life with a state NULL.
You cannot cast that to Number, etc.

You can avoid trying to do that sort of thing

then
    if ( LB_Temperature.state != NULL && LB1_Temperature.state != NULL) {
       var t = ((LB_Temperature.state as Number) + (LB1_Temperature.state as Number)) / 2
       Lilys_Bedroom.postUpdate(t)
   } else {
      logInfo("LBtemprule", "Unexpected NULL value")
   }
end

Rather than using a rule, you might want to try using group aggregation. Define Lilys_Bedroom like Group:Number:AVG. Then add the other temperature Items to it. The group will calculate the average for you.

Hello

i am having the same problem but i cannot solve it.

In the logs i saw the error:

[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Jalousien-4' failed: Could not cast NULL to org.openhab.core.library.types.OnOffType; line 64, column 37, length 48 in Jalousien

Then i tried to only send the update if it is not NULL:

rule "Status Automatische Beschattung (An/Aus) alle 5 Minuten senden"
        when 
                Time cron "0 0/5 * * * ?"   // alle 5 Minuten
        then
                var freigabedummy = Haus_Automatische_Beschattung.state as OnOffType
                if(Haus_Automatische_Beschattung.state != NULL)
                {
                sendCommand(Haus_Automatische_Beschattung,freigabedummy)
                }
end

But it does not work.
I still get the same errors in the log.

Of course not.

This line causes the error. You can’t make NULL into an OnOffType.

It’s too late now. The rule has already had the error and blown up, terminated.

Test for NULL before trying to do things that won’t work with NULL.

1 Like

That makes total sense.
Thanks a lot!

I fixed it but it’s not that pretty …

Basically i want to send the value periodically (some kind of persistence for my knx installation).


rule "Status Automatische Beschattung (An/Aus) alle 5 Minuten senden"
        when 
                Time cron "0 0/5 * * * ?"  // alle 5 Minuten
        then
                var freigabedummy = ON
                if(Haus_Automatische_Beschattung.state != NULL)
                {
                freigabedummy = Haus_Automatische_Beschattung.state as OnOffType
                Haus_Automatische_Beschattung.sendCommand(freigabedummy)
                }
end

That’s not doing anything useful; either you overwrite it, or you just don’t use it at all.
Remove that line and put the var inside the if()

If you wanted to command something even if NULL, use if-else structure.

1 Like

thank you.
i was trying this at first but vscode threw an error. don’t know why but now it works :slight_smile:

1 Like