How to detect (And set a default) for a NULL item value

  • Platform information:
    • Hardware: R-Pi4
    • OS: OpenHabian
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: 3.0.2
  • Issue of the topic: Can’t set ITEM to value when it is NULL

I can shorten this. I’d give you my takes of woe with OH3.0.2 but it’ll be easier if I just ask outright.

How do I detect a NULL item state and set it to a sensible value? e.g. an item defined in /etc/openhab/items/heating.items

Number:Temperature Entrance_Target_Temp "Entrance Target"

And the code

  if(Entrance_Target_Temp.state === NULL ) {
    logInfo("Heating", "Set default entrance target 10")

is ignored… Because a few lines later

logInfo("Heating", "CALL "+CallFor+" TARGET ENTRANCE "+Entrance_Target_Temp.state+" ACTUAL "+HallEntrance_Temp.state)


2021-05-23 17:40:27.654 [INFO ] [rg.openhab.core.model.script.Heating] - CHECK Entrance target=NULL vs actual NULL

So how do you check for a NULL value in an item?

10 isn’t a valid state for a Number:Temperature type.
It’s also best to use documented methods.
Entrance_Target_Temp.postUpdate("10 °C")

This might help remind you that postUpdate is asynchronous, it takes time to affect the actual Item and the rule does not stop and wait for that to happen.
Read the Item state in the next line, probably still get the old state.

That’s okay, you already know what you just posted to it. So use that instead.

In your case

  var myReading = Entrance_Target_Temp.state
  if (myReading == NULL ) {
    logInfo("Heating", "Set default entrance target 10")
    Entrance_Target_Temp.postUpdate("10 °C")
    myReading = 10 | °C

Use == with NULL, it is very much not the same as null

Many thanks rossko57. That appears to work a treat.