[SOLVED] Test for NULL


Why does my “!== null” test not skip the NULL string elements?

rule "Test Thing Status"
    Item testRule changed
    var String output = ""
    var String filter = "$..[?(@.statusInfo.status=='OFFLINE')]"

    val allThings = sendHttpGetRequest("http://localhost:8080/rest/things")
    val thingStatus = allThings.split('"statusInfo"')

    thingStatus.forEach[thing |
        if (thing != "[{")
            output = transform("JSONPATH", filter, '{"statusInfo"'+thing)
            if (output !== null)
                logInfo("test","Test Thing Status - Output: {}", output)

Log output:

2018-10-05 14:50:21.134 [INFO ] [pse.smarthome.model.script.test] - Test Thing Status - Output: NULL
2018-10-05 14:50:21.166 [INFO ] [pse.smarthome.model.script.test] - Test Thing Status - Output: {statusInfo={status=OFFLINE, statusDetail=COMMUNICATION_ERROR, description=No route to host (Host unreachable)}, editable=true, label=Bedroom Chromecast...

NULL is not the same thing as null.

NULL is a state that indicates that an Item is uninitialized. null is something that is only meaningful in Rules and means a variable doesn’t have a value.

For some reason the transform is returning NULL instead of null, probably to avoid errors in the log when there is no value that matches the JSONPATH. Instead of erroring out it sets the Item to NULL.

I don’t use JSONPATH very much so can’t offer why it works this way.

Arrgh… I tested for NULL all caps. I just realized that the log is showing the text string “NULL”.