Make use of Group based rules

Raspberry with OH 2.5 M3 (openhabian).

I use @rlkoshak DP a lot - especially this one:

However, I wonder if my rule below - which is obviously wrong - could be realized somehow:

rule "Monitor Average Traveltime"
when
Member of G_Trvl changed
then
// get itemname and update avr time of this item
var itemName = triggeringItem.name.toString
var String itemNameStr = itemName + "_Avr"
var Number avr = itemName.averageSince(now.withTimeAtStartOfDay)
logInfo("office.rules", "Average Traveltime calculation")
itemNameStr.postUpdate(avr)
logInfo("office.rules", "Average Traveltime of " + itemName + ": ...")
end 

I guess this could be the key, but I don’t get it:

itemName is just a string. You can’t get the .averageSince() of a string.
What you need is the proper Item object by that name.
There are ways to get hold of an Item if you only have its name as a string … but in this case you derived the name string directly from the Item object to begin with. You already have it, so -

var Number avr = triggeringItem.averageSince(now.withTimeAtStartOfDay)

Again, itemNameStr is just a string and you cannot .postUpdate() to a string, you need the Item object.
As said, there are ways to get hold of Item by name.

But fortunately the postUpdate action (as opposed to the dot-method) happens to able to do that for us - it wants two strings as parameters.
So in this rule, we can “cheat” and let postUpdate action find the Item for us.

postUpdate(itemNameStr, avr.toStrng)
1 Like

Glad that I checked the thread again before sending my message. Otherwise there would be two same answers now. :slight_smile:

An alternative to

would be to include

 import org.eclipse.smarthome.model.script.ScriptServiceUtil

and get the real item using

itemNameStr = ScriptServiceUtil.getItemRegistry.getItem(itemName + "_Avr")
...
itemNameStr.postUpdate(avr)
1 Like

See Design Pattern: Associated Items for a full discussion. But, as rossko57 indicates, the code you have above is trying to use the name of triggeringItem so you already have access to that Item. When later when you postUpdate using itemNameStr, you can use the postUpdate Action.

postUpdate(itemNameStr, avr)

This is one of the few cases where use of the Action is OK.

In those cases where you need the state of an Item where you only have that Item’s name, Olti’s approach is recommended.

Both of these are covered in the Associated Items DP.

@rossko57 @Olti

Awesome - thank you for your quick help.
I am going to merge your suggestions as @rlkoshak suggested, because I like the syntax itemNameStr.postUpdate(avr) better.