Get item of group which is raising the event

Hi all,

how do I get the item of a group that raised the receivedCommand rule. I know I can use persistence and historicState etc. to find which item of the group recently changed. But I do not want to use persistence for these items and I also want to get the event even if the state has not changed. There must be an easier way to achieve this goal.

Simple example:

// items
Switch S1 "Foo1"	(MyGroup) 
Switch S2 "Foo2"	(MyGroup) 
Switch S3 "Foo3"	(MyGroup) 

// rule for entire group.  is this possible?
rule "who was it by group"
when
	**Group** MyGroup received command
then
	logInfo("Foo" , "received command:" + receivedCommand)
       // how to get the item which raised the event here?
end

// rule for each item
rule "who was it by item"
when
	Item S1 received command or
	Item S2 received command or
	Item S3 received command 
then
	logInfo("Foo" , "received command:" + receivedCommand)
       // how to get the item which raised the event here?
end

There must be a simple approach which maybe I just can’t find.

Thank you
Karl

Given your restrictions there is no way to achieve this goal.

The only way is to use persistence and grab the most recently updated Item from the Group.

NOTE: If you configure your persistence using everyUpdate then lastUpdate will return the time that the Item was updated even if the update was the same state.

Also, a rule triggered by MyGroup received command will only trigger if the Group itself receives a command. You can send commands to its members all day long and the rule will never trigger. The only way to get a rule to trigger any time one of its members receive and event (command or update) is to use MyGroup received update.

There is no simple approach. It simply cannot be done in OH as it exists today without using persistence.

If you use the jsr223 scripting extension you can access the item that triggered the rule (https://github.com/openhab/openhab/wiki/Jsr223-Script-Engine) since it is passed as an argument.
I use it with jython and I am very happy with it.
If you use the “normal” rule engine, then everything what rlkoshak said. :wink:

Thanks a lot for your quick and helpful replies :slight_smile:

@rlkoshak: I thought so. The lastUpdate function is a very good hint.

@Spaceman_Spiff : this sounds very promising as well. I will dig into it ASAP.

So as of now I solved it using a lambda function:

val Functions$Function2 myfunc = [SwitchItem item, OnOffType receivedCommand |
// whatever comes here
]

rule "item S1"
when
	Item S1 received command 
then
        myfunc.apply( S1 , receivedCommand)
end

rule "item S2"
when
	Item S2 received command 
then
        myfunc.apply( S2 , receivedCommand)
end

rule "item S3"
when
	Item S3 received command 
then
        myfunc.apply( S3 , receivedCommand)
end
...

Well this may result in a a lot of small rules but I do save persistence this way. Which I really don’t need in my case.

I’m just curious, why so against persistence?

Hi Rich,

I’m not against persistence at all. I am using it. I am currently implementing some quite complex rollershutter logic. The WAF (woman acceptance factor) is quite high. Something like: short pressing a rollershutter button goes to 75%, long pressing to 100% down. While the shutter is moving and someone presses the button again it should stop. Of course some convenience scenes for certain groups of shutters. But if a door to the garden is opened then only lower the shade by 25% otherwise someone may get locked out, etc. etc.

As for the persistence I do not want to store each key/switch pressed in the database. I do not really see a reason for it and I am a little concerned about disk space in the long run.

BTW is there a persistence feature how to rotate a SQL table. Not really rotating but deleting everything that is older than one day for some tables or so. I know I could do this with a crontab entry and some scripts but maybe there is a built in feature?

Sounds like a good job for rrd4j and/or MapDB. Realize you are not limited to just one persistence engine. I always recommend one use MapDB for almost all Items with restoreOnStartup. Then put all Numeric Items (Number, Switch, Contact, DateTime, etc.) that you want historic data and/or charting in rrd4j. Finally, use something like MySQL only for those Items that you want to keep long term accurate data or expose the data to some external software or service.

MapDB only saves the most recent update so it never grows and it supports all OH Item types. rrd4j “compresses” data as it ages by averaging near by values and replacing them all with the average (for example, you may have a value every minute for the past week, one every 15 minutes for the past month, one every hour for six months ago, and so on). This is a good choice for those Items where you may want recent history but don’t care about this compression because rrd4j databases never grow so no need for maintenance.

See this posting for how I configured my persistence in this way:

It is on the roadmap but not yet implemented.

1 Like

Great, of course you are right. I never thought about using various persistence engines. This sounds like a real good approach. Thanks a lot again for your valuable feedback :+1: