# Rule Not Triggering On Group Change

**URL:** <https://community.openhab.org/t/rule-not-triggering-on-group-change/36112>\
**Category:** Scripts & Rules\
**Created:** [November 12, 2017, 7:30am UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112 "2017-11-12T07:30:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Webster](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@Mark\_Webster](https://community.openhab.org/u/Mark_Webster)\
**Post date:** [November 12, 2017, 7:30am UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/1 "2017-11-12T07:30:54Z")

</div>

Hi

I have a issue where a rule doesn’t seem to be triggering. The rule is configured to trigger on a group receiving an update. The group is the top of a nested tree of groups.

The items are as follows..

```csv
Group gNetwork (gLocation)
Group gNet_BRIDGE01 (gNetwork, gControl)
Group gNet_NAS01 (gNetwork, gControl)
Group gNet_NAS02 (gNetwork, gControl)
Group gNet_ROUTER01 (gNetwork, gControl)
Group gNet_GOOGLEDNS (gNetwork, gControl)
Switch BRIDGE01_Online "BRIDGE01" (gNet_BRIDGE01) {channel="network:device:BRIDGE01:online"}
Switch ROUTER01_Online "ROUTER01" (gNet_ROUTER01) {channel="network:device:ROUTER01:online"}
Switch GOOGLEDNS_Online "GOOGLEDNS" (gNet_GOOGLEDNS) {channel="network:device:GOOGLE-DNS:online"}
Switch NAS01_Online "NAS01" (gNet_NAS01) {channel="network:device:NAS01:online"}
Switch NAS02_Online "NAS02" (gNet_NAS02) {channel="network:device:NAS02:online"}

```

And the rule is…

```auto
rule "Network Alert"
        when
                Item gNetwork received update
        then
                val lastItem = gNetwork.allMembers.sortBy[lastUpdate].last
                logInfo("Rules","Network Alert -> " + lastItem.name + " changed to " + lastItem.state)
                pushNotification("Openhab Notice", "Network Alert -> " + lastItem.name + " changed to " + lastItem.state)
                sNetworkCheck.postUpdate(OFF)
        end

```

Now I’m sure it’s something simple that I’ve missed but I just can’t see it.. Any ideas would be most useful

---

<div class="post-metadata">

**Author:** ![Udo\_Hartmann](https://community.openhab.org/user_avatar/community.openhab.org/udo_hartmann/32/70871_2.png) [@Udo\_Hartmann](https://community.openhab.org/u/Udo_Hartmann)\
**Post date:** [November 12, 2017, 11:46am UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/2 "2017-11-12T11:46:30Z")

</div>

You have to set the Group Item Type.

```auto
Group:Switch gNetwork (gLocation)
Group:Switch gNet_BRIDGE01 (gNetwork, gControl)
Group:Switch gNet_NAS01 (gNetwork, gControl)
Group:Switch gNet_NAS02 (gNetwork, gControl)
Group:Switch gNet_ROUTER01 (gNetwork, gControl)
Group:Switch gNet_GOOGLEDNS (gNetwork, gControl)

```

Please be aware that the rule will trigger multiple times at every update.

---

<div class="post-metadata">

**Author:** ![Mark\_Webster](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@Mark\_Webster](https://community.openhab.org/u/Mark_Webster)\
**Post date:** [November 12, 2017, 12:52pm UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/3 "2017-11-12T12:52:40Z")

</div>

Thanks for the info… I’ve updated the group definitions and the rule does indeed trigger now.. However, the rule doesn’t seem to work as written… I’ve updated the rule to include a lock to stop it triggering multiple times.

```csv
rule "Network Alert"
        when
                Item gNetwork received update
        then
                logInfo("Testing","Lock: " + lock.isLocked)
                if (!lock.isLocked) {
                        lock.lock()
                        try {
                                logInfo("Testing","gNetwork received update")
                                val lastItem = gNetwork.allMembers.sortBy[lastUpdate].last
                                logInfo("Rules","Network Alert -> " + lastItem.name + " changed to " + lastItem.state)
                                pushNotification("Openhab Notice", "Network Alert -> " + lastItem.name + " changed to " + lastItem.state)
                        } finally {
                                lock.unlock()
                        }
                } else {
                        logInfo("Testing","Locked, update ignored")
                }
        end

```

The log message “gNetwork received update” from line 9 get’s logged into the openhab.log which shows that the rule is triggered and the locking working, but oddly the log message from line 11 never gets logged. I’m presuming something’s not quite right on line 10 but just can’t see it!!

---

<div class="post-metadata">

**Author:** ![mikaelgu](https://community.openhab.org/user_avatar/community.openhab.org/mikaelgu/32/37407_2.png) [@mikaelgu](https://community.openhab.org/u/mikaelgu)\
**Post date:** [November 12, 2017, 6:32pm UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/4 "2017-11-12T18:32:50Z")

</div>

A total stab in the dark here, but on line 10 substitute val with var. val’s are static and can’t be changed where as var’s can change during runtime.

---

<div class="post-metadata">

**Author:** ![Mark\_Webster](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@Mark\_Webster](https://community.openhab.org/u/Mark_Webster)\
**Post date:** [November 12, 2017, 6:37pm UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/5 "2017-11-12T18:37:57Z")

</div>

Thanks for the advice… I actually worked out the issue here… It seems that if one of the group members has a last value of Null then the lastUpdate.last seems to fail.. Once all group members have a last value it works, sort of.

The issue I’m having now is stopping the rule firing multiple times and therefore sending multiple messages even though the item has NOT updated. I’m thinking I need to change from

```csv
Item gNetwork received update

```

to

```csv
Item gNetwork changed

```

Ideally I’d like to use

```csv
Item gNetwork changed from ON to OFF

```

But it seems when an item in the group changes from ON to OFF the group changes from ON to UNDEF

---

<div class="post-metadata">

**Author:** ![Udo\_Hartmann](https://community.openhab.org/user_avatar/community.openhab.org/udo_hartmann/32/70871_2.png) [@Udo\_Hartmann](https://community.openhab.org/u/Udo_Hartmann)\
**Post date:** [November 13, 2017, 9:24am UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/6 "2017-11-13T09:24:25Z")

</div>

No, you can’t trigger to `changed` or at least this would possibly not work as expected. In fact you don’t use any sum function (AND/OR…), so there is no rule how to build the state of the group at all - hence the UNDEF, I guess.

However, within the `try {}` section, you could use `lastItem.state` and `lastItem.previousState` (you have to use a persistence to do this).

```auto
if (lastItem.state == OFF && lastItem.previousState().state == ON)

```

should ensure only to get those events where an item changed from ON to OFF.

---

<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:** [November 13, 2017, 5:01pm UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/7 "2017-11-13T17:01:27Z")

</div>

> [@Mark\_Webster](#):
>
> It seems that if one of the group members has a last value of Null then the lastUpdate.last seems to fail

You can filter out those Items using

```
val lastItem gNetwork.allMembers.filter[net | net.lastUpdate != null].sortBy[lastUpdate].last

```

> [@Mark\_Webster](#):
>
> The issue I’m having now is stopping the rule firing multiple times and therefore sending multiple messages even though the item has NOT updated.

This is how Groups work unfortunately. Each Group receives multiple updates any time any one of its members is updated. If your Group has N members, an update to one member will cause N-1 updates and your Rule will trigger N-1 times.

As Udo explains, using `changed` is unlikely to work for you because that will only trigger the Rule when the Group’s state changes. So if all the members are OFF, the Group’s state will be OFF. One Item changes to ON then the Group will change to ON. But if a second Item changes to ON then the Group will not change to ON because it already is ON and therefore the Rule won’t trigger.

However, if you use Group:Number:SUM for your Groups the state of the Group will be the count of the members that are ON. Then you can use changed as your trigger. I’m not 100% certain this will work with your hierarchical Groups but I think it should work.

---

<div class="post-metadata">

**Author:** ![jvanzuijlen](https://community.openhab.org/user_avatar/community.openhab.org/jvanzuijlen/32/70715_2.png) [@jvanzuijlen](https://community.openhab.org/u/jvanzuijlen)\
**Post date:** [May 14, 2018, 9:47am UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/8 "2018-05-14T09:47:59Z")

</div>

> [@rlkoshak](#):
>
> However, if you use Group:Number:SUM for your Groups the state of the Group will be the count of the members that are ON.

Just stumbled upon this post and it looks like this could be a solution to my problem as well. However: what would happen if two switches in the group toggled state ‘simultaneously’, assuming that they had unequal values at the start.

Would the above always yield an update (with the value of the sum going from `N` to `N-1` to `N`) or can’t this be guaranteed?

---

<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:** [May 14, 2018, 2:44pm UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/9 "2018-05-14T14:44:43Z")

</div>

> [@jvanzuijlen](#):
>
> what would happen if two switches in the group toggled state ‘simultaneously’

This is a nearly impossibe thing to occur in a computer. One or the other event will occur milliseconds before or after the other. I think the worst case scenario is that the first SUM calcualted might be wrong (i.e. include the new state of the second Item as well) but the result of the second SUM calculated will be wrong so the Group might be off by one for a dozen milliseconds or so.

---

<div class="post-metadata">

**Author:** ![jvanzuijlen](https://community.openhab.org/user_avatar/community.openhab.org/jvanzuijlen/32/70715_2.png) [@jvanzuijlen](https://community.openhab.org/u/jvanzuijlen)\
**Post date:** [May 14, 2018, 7:35pm UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/10 "2018-05-14T19:35:08Z")

</div>

That is assuming that all state changes are processed individually and not batched or sampled. Clearly this is not the case for openHAB ; perfect. I can live with the couple of milliseconds of ‘false’ state 🙂

---

<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:** [May 14, 2018, 7:46pm UTC](https://community.openhab.org/t/rule-not-triggering-on-group-change/36112/11 "2018-05-14T19:46:16Z")

</div>

I can’t speak to individual bindings. But once the events get published to the Event Bus they are processed individually based on my observations. I’ve not looked at the code to verify though.
