Multiple things in rules

Hi there again,

It seems that referring to things inside rules is not so well defined as items.

My task is to apply a rule to every thing when its status is changed (on to off or off to on).

Solutions i came up with but i think they are not ideal

  1. Using the rest service and quering the status of things with polling
  2. Writing dynamically every time a new thing is discovered (or removed) inside java zwave binding code the rules file.

I don’t like any of the above solutions and i would like to benefit from event mechanism of openhab.

Is there any way i can apply rules on every thing that exists with event logic?

p.s. I searched about groups but i found that they work for items.

Again any help would be much appreciated!

Thanks a lot in advance!

Things are not meant to be used in rules. The ‘event mechanism’ as you call it is based on items only.
In fact you should not need to refer to things as you can always have items to be mapped to things (well, via channels).

thanx for the reply. Ok!

Is there an elegant way to apply a rule to all existing items?

I don’t understand what you mean by “apply a rule” as you only ever apply actions or send commands to items from within a rule.
If that’s what you want to do then put all items you want to refer to into a group and use groupname.sendCommand().

Specifically what i want to do is to send to an mqtt broker a message every time a thing changes its state from online to offline and reverse.

As i saw item does not have this information (statusinfo).

Check the documentation here.
As far as I know, you can do something like this:

rule myRule
when
Thing “myThing” changed from ONLINE to OFFLINE
then
//do something 
end

Thanks. This is why i am doing now and its working!

But i am looking for how to apply //do something to every thing

What do mean everything?
What do you want to happen when the rule runs?

I think he wants to fire a rule like the one above for each of his things.

Unfortunately, I don’t think that’s possible. If only we had a triggeringThing (like the triggeringItem), you could do this with just one rule.

It also would be helpful to have an API call to get the thing name…

Exactly.
So thanks anyway i ll try to patch it up with a custom solution!

If you do something custom, please do 1. so it works with ALL bindings. Then create a new posting under Tutorials and Solutions to share your solution.

This might be helpful

I am aware that this is old, but looking for a similar solution I found this thread and thought my workaround would be helpful for some people.

I have some things (dash buttons) I would like to monitor:

Group:Switch:AND(ON,OFF)    G_DashThing        "amazondashbutton"                               (G_jdbc,G_Things)       	
Switch                      DashThing1         "Dash1 (Alexa)"                                  (G_jdbc,G_DashThing)    	
Switch                      DashThing3         "Dash3 (Standheizung)"                           (G_jdbc,G_DashThing)    	
Switch                      DashThing4         "Dash4 (CloseNGo)"                               (G_jdbc,G_DashThing)    	
Switch                      DashThing5         "Dash5 (Bewässerung)"                            (G_jdbc,G_DashThing)    	
Switch                      DashThing6         "Dash6 (unused)"                                 (G_jdbc,G_DashThing)

My rule to check the state:

rule "OFFLINE check DashX"
when
	Time cron "22 */30 * * * ?"
then
// loop through all members of G_DashThing
	G_DashThing.members.forEach[dash | 
	// get ID of the Dash button
		val dshName = dash.name.toString.replaceAll("DashThing","")
	// translate ID into real Thing-ID
		val thing = transform("MAP", "dashs.map", dshName)
	// get status of Thing
		val status = getThingStatusInfo(thing).getStatus()
					
		if(status.toString() == 'ONLINE') {
			postUpdate(dash, "ON") // the following does not work, because it's not the item itself, but just its name as string: itemNameSw.postUpdate(ON)
		}
		else { 
			postUpdate(dash, "OFF") // the following does not work, because it's not the item itself, but just its name as string: itemNameSw.postUpdate(ON)
		}
	]
end

where my dashs.map file looks like:

1=amazondashbutton:dashbutton:MAC-ADDRESS
3=amazondashbutton:dashbutton:MAC-ADDRESS
4=amazondashbutton:dashbutton:MAC-ADDRESS
5=amazondashbutton:dashbutton:MAC-ADDRESS
6=amazondashbutton:dashbutton:MAC-ADDRESS