How to check if an item exists?

Hi,

I’m trying to keep my rules rather generic.
This means that I don’t always refer to item by their direct name. E.g. something like

"sendCommand(KNX_GV_Keuken_Sonos_10_1_0, ON)"

But sometimes the item name is composed out of some other variables. An example:

val itemName = gAllecontacten.allMembers.filter[sw|sw.name.contains(in_UDPmsg.substring(1, 5))].head
    postUpdate(itemName, in_UDPmsg.substring(5))

If I created my items correctly, this should not be a problem. But I just want to have a check if there really is an item meeting the conditions defined in “itemName”.
If the itemname exists => proceed with the code.
If the itemname does not exist => loginfo + execute alternative code.

So how do I check if e.g. item abcd really exists in my OpenHAB setup?

You could put them in a group, and search the group for item name

You might find this contains relevant ideas

Thanks!
I assumed there was an “if exist item” option (I had this in another programming language).

val RelatedWindowContact = gRaamcontact.allMembers.filter[sw|sw.name.contains("something here")].head
		if (RelatedWindowContact == null) {
				// some logic
		} else {
				// some other logic	
		}
1 Like