However, I wonder if my rule below - which is obviously wrong - could be realized somehow:
rule "Monitor Average Traveltime"
Member of G_Trvl changed
// 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")
logInfo("office.rules", "Average Traveltime of " + itemName + ": ...")
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.
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.
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.