Python: String empty or null?

Hey guys,

one maybe very easy question:
I try to check if a string item is empty, therefore I try it with:

if len(ir.getItem(presenceLightAlter).state.StringValue()) > 0:

But I get an error, because the item state is NULL and so there is no string value.
How can I check both, if it is NULL or empty?

I see a bunch of potential issues.

  1. presenceLightAlter is a string variable? Or is that the name of your Item. If it’s the name of your Item you need to put it into quotes. ir.getItem takes a String representing the Item’s name.

  2. This one isn’t a problem, but you don’t need to pull an Item to check it’s state. Just use the items dict. items["presenceLightAlter"] will give you the state of the Item named “presenceLightAlter”.

  3. I’m unfamiliar with StringValue() and I don’t see it mentioned anywhere in the openHAB classes. Do you mean toString()?

  4. NULL is an actual Item state and calling toString() on an Item’s state that is NULL will give you “NULL” as a String. UNDEF is another potential state your Item can be. If the Item exists, the only way that the string representation of the Item’s state would be an empty string is if it’s a String Item and you set the state to the empty string. If it’s never been initialized the string representation of the state will be “NULL”. If the binding determined it can’t know the state of the Item, the string representation of the state will be “UNDEF”.

Therefore, if you want to check that the Item’s state is not UNDEF, not NULL, and has a non-zero length string representation you would use

if isinstance(items["presenceLightAlter"], UnDefType) and items["presenceLightAlter"].toString().length() > 0:

Note that Items, states, and all of that are Java Objects so all the same Java stuff will apply. So in this case because the Item’s state is a Java Object it has a toString() method which returns a Java String which in turn has a length() method.

The Python len function should work as Jython is pretty good converting between Java stuff and Python stuff for the basic classes like String.

Thank you for your quick response.

  1. I have stored the item name is the variable presenceLightAlter, so I don’t need quotes.
  2. I found that in the python helper docu, but seems that it is working without the pull.
  3. I think, I found that in the helper docu, too. Or I’m complety wrong
  4. Understood. But If I’m using you expression, then my string with the NULL value is true.
    .length() was not working for me, switched that back to len()
  File "/etc/openhab/automation/lib/python/core/log.py", line 96, in wrapper
    return function(*args, **kwargs)
  File "/etc/openhab/automation/lib/python/core/rules.py", line 108, in execute
    self.callback(inputs.get('event'))
  File "<script>", line 30, in switchLightOnOff
AttributeError: 'unicode' object has no attribute 'length'

I gave every item a default value with a rule after starting the system. Then I don’t need to check if the item value is NULL. Not nice, but working for me now

Why not use restoreOnStartup then?

You still need to check to see if the Item’s state is valid and not the initial value so I’m not sure what you are saving here. Depending on the behavior of the bindings you use, your approach won’t work in all cases. For example, if the MQTT binding loses its connection to the MQTT broker, it sets all the Items linked to its Channels to UNDEF to indicate that it cannot know the state of the Item at this time.

It is best to use the NULL and UNDEF states for what they are intended to mean in your Rules. The resultant rules end up being less brittle and you can take more nuanced actions in the various conditions.