Access specific item information via rules

Hi there,

I’m new to Openhab and already ran into a problem.

I have several items in my configuration, mostly switches. I want to assign special ids to these switches with whom I can get direct access to them in the rules. In my case a get an input via serial binding, parse it and its containing information in the rules and then do special actions later on.

Switch myMansionSwitch1 “Test1” (myMansion) {pin=“8”, source=“11”}
Switch myMansionSwitch2 “Test2” (myMansion) {pin=“9”, source=“11”}

My goal is to “find” the Switch with f.e. pin=8 (which can change, depending on the serial bindings input) within the code.
I’m accessing all these items/switches with this code:

myMansion?.members.forEach(theitem|…)

Is there something like

theitem.getattr(“pin”)

available to get access to its attributes? Haven’t found anything yet. If not, which would be the correct way to do something like this?

Thanks for your help!

You wouldn’t really want to peer inside the binding config strings in your rules, as you are trying to be at a level of abstraction higher than the bindings. But there is a lot of power in setting up maps as data you can access within your rule files.

val itemsPins = newHashMap( 
  "myMansionSwitch1" -> 8, 
  "myMansionSwitch2"  -> 9
)

rule x
when something
then
  var pin = itemsPins.get("myMansionSwitch2") as Integer // sets pin to 9
  // ...
end

Have a look at this wiki page:

1 Like

thanks, this really helped a lot!

I now used a hashmap with all my items in it:

var HashMap<org.openhab.core.library.items.SwitchItem, LinkedHashMap<String, Integer>> itemBase =
newLinkedHashMap(
mySwitch1 → (newLinkedHashMap(“id” → 8, “source” → 11, “laststate” → -1 ) as LinkedHashMap<String, Integer>),
mySwitch2 → (newLinkedHashMap(“id” → 9, “source” → 11, “laststate” → -1 ) as LinkedHashMap<String, Integer>),
mySwitch3 → (newLinkedHashMap(“id” → 8, “source” → 12, “laststate” → -1 ) as LinkedHashMap<String, Integer>)
)