Does an openHAB Item exist?

Posted by ads' corner on Saturday, 2022-03-19
Posted in [Openhab]

In a more complex openHAB2 Rule I’m writing, I need to find out if a certain Item exist. If I try to access it when it does not exist, the Rule will fail. Not good.

I could pass in a parameter to the framework which builds the Rule, but that is cumbersome and requires changes on several places.

Let’s see if I can have the Rule figure this out.

There is the getItems() function in ScriptServiceUtil. If I call this function and don’t net a newArrayList back, the Item exist. That’s easy to code:

1
2
3
4
5
import org.eclipse.smarthome.model.script.ScriptServiceUtil

if (ScriptServiceUtil.getItemRegistry.getItems("myItem") != newArrayList) {
    myItem.postUpdate(OFF)
}

That’s all which is required to have the Rule not fail.


Categories: [Openhab]