Enumerating all Items from a rule?

Hello,

First time here to forgive me if I’m posting to the wrong part of the forum.

I wonder if there is a way to enumerate all defined Items from a rule?

Instead of addressing each item manually i.e. Item1.state == ON etc
it would be nice to do a for each over all items and check the state of each.

Thank you for any assistance.

Br,
Freddy

Might be handy, not for all items, but certainly for items in a particular group.

In order to do this, each item would have to expose its type, as different state properties have differing types (string and number).

I’ve not seen this done, as I am not aware we can access the item collection (if there even is such a thing). Or the thing collection in order to access its channel collection. All very confusing.

I actually found a solution that works for my purposes.

I added all relevant items into a Group (in my case I added all Switch items).
In a rule I then iterate over all items in the Group using this code:

Switch_Items.allMembers.forEach[switch_item|
		logInfo(RuleName, "Item [" + switch_item.name + "] is in state [" + switch_item.state + "]")
	]

The output in the openhab.log is similar to this:

Item [LightSwitch1] is in state [ON]

1 Like