# Make use of Group based rules

**URL:** <https://community.openhab.org/t/make-use-of-group-based-rules/83608>\
**Category:** Beginners\
**Tags:** groups\
**Created:** [October 15, 2019, 7:53am UTC](https://community.openhab.org/t/make-use-of-group-based-rules/83608 "2019-10-15T07:53:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [October 15, 2019, 7:53am UTC](https://community.openhab.org/t/make-use-of-group-based-rules/83608/1 "2019-10-15T07:53:55Z")

</div>

Raspberry with OH 2.5 M3 (openhabian).

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

> [@Design Pattern: Working with Groups in Rules](https://community.openhab.org/t/design-pattern-working-with-groups-in-rules/20512):
>
> Edit: Updates for OH 4, inclusion of JS Scripting and Blockly examples, removed the doors example as it’s superfluous. Please see [Design Pattern: What is a Design Pattern and How Do I Use Them](https://community.openhab.org/t/design-pattern-what-is-a-design-pattern-and-how-do-i-use-them/40319) for how to read and use DPs. Problem Statement One powerful way to consolidate code in rules is to use array/list manipulation operations on the members of a Group. For example, filter down to just those members that are ON, get a list of the labels of those Items that are NULL, etc. Concept T…

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:

> [@Reusable Functions: A simple lambda example with copious notes](https://community.openhab.org/t/reusable-functions-a-simple-lambda-example-with-copious-notes/15888):
>
> Major Edit: Rewrote with the cleaner OH 2.3 syntax. The example lambda on the OH 1 wiki is overly complex and as written will not work in OH 2. This quick example will show a super simple lambda example with copious notes. Here is the code without comments. It takes in an Item, logs it’s state and returns a slightly modified version of the String logged. val log= [GenericItem s | logInfo("lambda", s.state.toString) s.state.toString + " logged"] rule "Call Lambda" when // some t…

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [October 15, 2019, 12:21pm UTC](https://community.openhab.org/t/make-use-of-group-based-rules/83608/2 "2019-10-15T12:21:36Z")

</div>

> [@NCO](#):
>
> var itemName = triggeringItem.name.toString  
> …  
> var Number avr = itemName.averageSince(now.withTimeAtStartOfDay)

`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 -

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

```

> [@NCO](#):
>
> itemNameStr.postUpdate(avr)

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.

```php
postUpdate(itemNameStr, avr.toStrng)

```

---

<div class="post-metadata">

**Author:** ![Olti](https://community.openhab.org/letter_avatar_proxy/v4/letter/o/9de053/32.png) [@Olti](https://community.openhab.org/u/Olti)\
**Post date:** [October 15, 2019, 12:25pm UTC](https://community.openhab.org/t/make-use-of-group-based-rules/83608/3 "2019-10-15T12:25:46Z")

</div>

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

An alternative to

> [@rossko57](#):
>
> postUpdate(itemNameStr, avr.toStrng)

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)

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 15, 2019, 2:43pm UTC](https://community.openhab.org/t/make-use-of-group-based-rules/83608/4 "2019-10-15T14:43:23Z")

</div>

See [Design Pattern: Associated Items](https://community.openhab.org/t/design-pattern-associated-items/15790) 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.

---

<div class="post-metadata">

**Author:** ![NCO](https://community.openhab.org/user_avatar/community.openhab.org/nco/32/55938_2.png) [@NCO](https://community.openhab.org/u/NCO)\
**Post date:** [October 16, 2019, 5:18am UTC](https://community.openhab.org/t/make-use-of-group-based-rules/83608/5 "2019-10-16T05:18:47Z")

</div>

@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.
