Error in rule since OH2 (state == Undefined)


i get an error message from one of my rules, since i upgraded to OH2.

Here is my rule:

rule "Periodically check presence"
    Time cron "0 */5 * * * ?"
        if (Presence.state == ON)
                if(gMobiles.members.filter(s | s.state == ON).size == 0) {
                        logInfo("PresenceCheck", "No phone within reach, checking for flapping")
                        if(gMobiles.members.filter(s | s.changedSince(now.minusMinutes(5))).size == 0) {
                                logInfo("PresenceCheck", "Nobody is at home")
                                sendCommand(Presence, OFF)
                //For initialisation. If Presence is undefined or off, although it should be on.
                if(gMobiles.members.filter(s | s.state == ON).size > 0) {
                        sendCommand(Presence, ON)
                else if (Presence.state == Undefined || Presence.state == Uninitialized) {
                        sendCommand(Presence, OFF)


Error message, when nobody is at home:

2017-02-20 05:10:00.017 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Periodically check presence: An error occured during the script execution: The name 'Undefined' cannot be resolved to an item or type.

I think, Undefined is no longer working with OH2. Do i have to change this? Is NULL the right thing?

If I’m not mistaken these are changed in OH2, you are supposed to use “NULL” instead.
As in :
else if (Presence.state == NULL) {

So i can remove both, undefined and unitialized and replace them with just NULL ?

I’m actually not 100% sure if it’s enough to check for NULL (and not Uninitialized as well) , I only know it because of this

1 Like

Yes, that’s my understanding

More info here:

So I have to check for NULL and null?

No, just NULL :slight_smile:

if (RailRun1Time88.state==NULL) RailRun1Time88.sendCommand(56.7)

NULL does not work for me. (OH2.1)

rule "Test UNDEF"
        Item avr1Z2_Volume changed
        logDebug("Test", "avr1Z2_Volume: " + avr1Z2_Volume.state)
        if ( avr1Z2_Volume.state == NULL ) {
            logDebug("Test", "NULL")
        if ( avr1Z2_Volume.state == UNDEF ) {
            logDebug("Test", "UNDEF")

This is the output:

2017-07-01 14:45:46.129 [DEBUG] [.eclipse.smarthome.model.script.Test] - avr1Z2_Volume: UNDEF
2017-07-01 14:45:46.140 [DEBUG] [.eclipse.smarthome.model.script.Test] - UNDEF

I’m seeing the same behavior.
When I compare the item state with UNDEF it return true, if I compare the item state to NULL if get false as the result.

I’m using the latest snapshot from today (1037).
Does anybody have an update on this?


Don’t know if it is related but I have encountered a warning in my rules which said that for NULL values need to use “===” instead of “==”. It started to warn me only in the latest snapshot build.

Yes, me too.

I changed some of my rules, but the warning was not in all rules with “==”, so i only changed the ones with warning.

I’ve also seen this message in the log files while using the latest snapshot 1037, but the comparison of a state with NULL was already failing in the previous build I was using (build 999).

I think I get the “use ===” message when I compare to null, but not when I compare to NULL. Are those two different tests?

I believe the diference can be found in == performing type conversions as necessary and === NOT performing any conversions before the comparison:

(and links therein)

Yes, but the syntax checker only gives that message (so far as I can tell) for null, not for NULL. And when I tried using === with NULL it broke a bunch of rules, so I had to revert them. I haven’t tried that again for a while, but it suggests to me that null !== NULL, and I’m not sure when to use which.

Use lowercase “null” when using variables declared in rules files and use uppercase NULL when checking item states.

if (myVar === null)

if (myItem.state == NULL)

That’s atleast my understanding of it…but I agree it’s confusing.

1 Like

I tried to extend this rule so that there can be more than one calendar entry per day. In my case, maximum three bins can be disposed at the same day. It works IF there are 3 events in the preload time set in caldavio.cfg. But when there are less than three entries the statement(s) if( After…) generate an error because it cannot compare an undefined state with the current time.
I tried to overcome the problem as proposed here and in other threads - to check whether the state is undefined before comparing the times. For this, I check the calendar entry itselfs (not the time), declared in the rule as a string.

var String Abfallart_3=CalDav_Muelltonne_3.state.toString
var Number ab_beginn = 13
      var Number ab_end = 15 
         logInfo("kalender.rules", "kalenderrule getriggert")
         logInfo("Tonne.2", Abfallart_3)

If there is something, it is getting the right string (for example “schwarze Tonne”)
The code inside the rule is:

if (Abfallart_3 === null)   {logInfo("kalender.rules", "no entry")}
       else {
         logInfo("kalender.rules", "entry")
        (now.isBefore(new DateTime((CalDav_Muell_Date_3.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli()).minusHours(ab_beginn)) && 
        now.isAfter(new DateTime((CalDav_Muell_Date_3.state as DateTimeType).getZonedDateTime.toInstant.toEpochMilli()).minusHours(ab_end))) {
        sendPushoverMessage(pushoverBuilder("morgen:  " + Abfallart_3+ " rausstellen"))  }

The problem is the first IF-statement which should check whether there is an entry or not. In the latter case it should skip the else branch, therefore avoiding the type mismatch error.

I tried any combination of ==, ===, null, NULL, undefined, Undefined, UNDEF, “” etc. but it goes every time into the else branch (see logInfo) and gives an error message in the log file:

2019-05-10 13:38:45.363 [INFO ] [org.eclipse.smarthome.model.script.kalender.rules                     ] - kalenderrule getriggert
2019-05-10 13:38:45.365 [INFO ] [org.eclipse.smarthome.model.script.Tonne.2                            ] - UNDEF
2019-05-10 13:38:45.381 [INFO ] [org.eclipse.smarthome.model.script.kalender.rules                     ] - entry
2019-05-10 13:38:45.383 [ERROR] [rg.eclipse.smarthome.model.rule.runtime.internal.engine.RuleEngineImpl] - Rule 'Abfallkalender': Could not cast UNDEF to org.eclipse.smarthome.core.library.types.DateTimeType; line 36, column 37, length 41

Which syntax should work in OH 2.4? Thanks for any help.

NULL is a valid state for any Item. Usually found in an Item that has not had any update since system boot.

If you take myItem.state.toString from that state, you will get a string (like you asked for) with a value of “NULL”, i.e. a string of four characters.

Likewise, UNDEF is a special state, usually set by a binding in some error case. Taking a string version of that yields “UNDEF”.