# \[SOLVED\] Rule doesn't trigger on change randomly?

**URL:** <https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181>\
**Category:** Scripts & Rules\
**Created:** [June 8, 2018, 10:45pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181 "2018-06-08T22:45:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [June 8, 2018, 10:45pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/1 "2018-06-08T22:45:10Z")

</div>

- Platform information:
  - Hardware: x86/2G ram /16GB ssd
  - OS: Lubuntu Xenial
  - Java Runtime Environment: openjdk 1.8
  - openHAB version: 2.3.0

Some times I find my rules don’t trigger when they should be.

I have a group for presence detection SA\_gPresence with multiple items in it.

I also have 3 different rules for this item

```php
rule "No presence detected"
when 
    Item SA_gPresence changed from ON to OFF
then
    logInfo("SA", "global presence changed to OFF, ie no someone home")
end

rule "Presence detected"
when 
    Item SA_gPresence changed from OFF to ON
then
    logInfo("SA", "global presence changed to ON, ie someone home ")
end

rule "debug status printout"
when
    Item SA_downstairs_occupancy changed 
    or Item SA_girls_occupancy changed 
    or Item SA_boys_occupancy changed 
    or Item SA_parents_occupancy changed
    or Item SA_gPresence changed
then
        logInfo("SA", String::format("pres(%-3s)="
                + "Sen[D(%-3s)/P(%-3s)/G(%-3s)/B(%-3s)]"
                SA_gPresence.state, SA_downstairs_occupancy.state,SA_parents_occupancy.state,SA_girls_occupancy.state,SA_boys_occupancy.state))
end

```

Some times this works as is, but sometimes it only fires the debug print one and not the others.

Am I doing something wrong?

I did check the event log and that does report the change, but the output log only has the debug print out and not the Presence detection.

```auto
2018-06-08 10:57:06.966 [INFO] [rg.eclipse.smarthome.model.script.SA] - pres(OFF)=Sen[D(OFF)/P(OFF)/G(OFF)/B(OFF)]
2018-06-08 10:57:06.972 [INFO] [rg.eclipse.smarthome.model.script.SA] - global presence changed to OFF, ie no someone home
2018-06-08 18:13:15.398 [INFO] [rg.eclipse.smarthome.model.script.SA] - pres(ON )=Sen[D(ON )/P(OFF)/G(OFF)/B(OFF)]
2018-06-08 18:13:15.402 [INFO] [rg.eclipse.smarthome.model.script.SA] - pres(ON )=Sen[D(ON )/P(OFF)/G(OFF)/B(OFF)]
2018-06-08 18:16:15.828 [INFO] [rg.eclipse.smarthome.model.script.SA] - pres(ON )=Sen[D(ON )/P(OFF)/G(ON )/B(OFF)]
2018-06-08 18:16:15.920 [INFO] [rg.eclipse.smarthome.model.script.SA] - pres(ON )=Sen[D(ON )/P(ON )/G(ON )/B(OFF)]

```

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [June 8, 2018, 11:27pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/2 "2018-06-08T23:27:33Z")

</div>

```php
rule "Presence changed"
when 
    Item SA_gPresence changed
then
    if (previousState == NULL) return;
    var String presenceString = "global presence changed to " + gPresence.state.toString
    if (gPresence.state == ON) presenceString = presenceString + ", ie someone home"
    else presenceString = presenceString + ", ie no one home"
    logInfo("SA", presenceString)
end

rule "debug status printout"
when
    Item SA_downstairs_occupancy changed 
    or Item SA_girls_occupancy changed 
    or Item SA_boys_occupancy changed 
    or Item SA_parents_occupancy changed
    or Item SA_gPresence changed
then
    logInfo("SA", String::format("pres(%$1s)=Sen[D(%$2s)/P(%$3s)/G(%$4s)/B(%$5s)]"
        SA_gPresence.state, SA_downstairs_occupancy.state,SA_parents_occupancy.state,SA_girls_occupancy.state,SA_boys_occupancy.state))
end

```

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [June 9, 2018, 1:24pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/3 "2018-06-09T13:24:04Z")

</div>

Thanks I’ll try this out and see if it happens again.

But is there any reason you know why my implementation would have failed?

I’d like to know what I did wrong to ensure all my other rules work.

I’m assuming because I explicitly had the two rules trigger on a from - to that I wouldn’t need to account for null.

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [June 9, 2018, 2:37pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/4 "2018-06-09T14:37:36Z")

</div>

Your rules triggered, your logInfo was incorrect.

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [June 11, 2018, 3:19pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/5 "2018-06-11T15:19:42Z")

</div>

I’m still struggling to understand what was failing originally.

When I read the log it shows at **10:57** that presence was changed to **OFF** and _“debug status printout”_ rule was triggered, then just after that the _“No presence detected”_ triggered.

Then a few hours later at **18:13** only the _“debug status printout”_ triggered and printed debug status.

I thought according to my rules _“Presence detected”_ should have also been triggered since according to the debug printout **pres(OFF)** changed to **pres(ON)**

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [July 16, 2018, 11:33pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/6 "2018-07-16T23:33:17Z")

</div>

So its still happening

Usually all the rules trigger and all of this works fine, but intermittently one does not.

In the log, after the following output I should see a line with `"enter global presence changed"` but I do not.

```
2018-07-16 09:54:57.312 [INFO] [rg.eclipse.smarthome.model.script.SA] - pres(ON )=Sen[D(ON )/P(OFF)/G(OFF)/B(OFF)]
2018-07-16 09:57:57.831 [INFO] [rg.eclipse.smarthome.model.script.SA] - pres(OFF)=Sen[D(OFF)/P(OFF)/G(OFF)/B(OFF)]
2018-07-16 09:57:57.845 [INFO] [rg.eclipse.smarthome.model.script.SA] - pres(OFF)=Sen[D(OFF)/P(OFF)/G(OFF)/B(OFF)]

```

However in the events log I do see the following, so definitely my rule should have triggered:

```
2018-07-16 09:57:57.865 [GroupItemStateChangedEvent] - SA_gPresence changed from ON to OFF through SA_downstairs_occupancy

```

Can anyone answer why didn’t my rule trigger?

**My Rules**

```php
rule "debug status printout"
when
    Item SA_downstairs_occupancy changed 
    or Item SA_girls_occupancy changed 
    or Item SA_boys_occupancy changed 
    or Item SA_parents_occupancy changed
    or Item SA_gPresence changed
then
    logInfo("SA", String::format("pres(%$1s)=Sen[D(%$2s)/P(%$3s)/G(%$4s)/B(%$5s)]"
        SA_gPresence.state, SA_downstairs_occupancy.state,SA_parents_occupancy.state,SA_girls_occupancy.state,SA_boys_occupancy.state))
end

rule "Presence detected"
when 
    Item SA_gPresence changed
then
    logInfo("SA", "enter global presence changed")
    if (previousState == null)
    {
        logInfo("SA", "global presence was null, skip presence detection")
        return;
    }
    ...
end

```

---

<div class="post-metadata">

**Author:** ![sam2b](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@sam2b](https://community.openhab.org/u/sam2b)\
**Post date:** [July 17, 2018, 5:59am UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/7 "2018-07-17T05:59:06Z")

</div>

My suggestion is to also try _not_ to use `changed from OFF to ON`, and just use this:  
`Item SA_gPresence changed to ON`

Specifying the `from` along with the `to` is a double constraint, but for binary switch (just two states) all you really care about is when the state is changed **to** something.

> [@RedOranges](#):
>
> I’m still struggling to understand what was failing originally.

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [July 17, 2018, 6:05am UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/8 "2018-07-17T06:05:38Z")

</div>

Does `rule "Presence detected"` triggers sometimes and sometimes not or does it never triggers?

If the first rule triggers when SA\_gPresence changes then the second one should too.  
The first rule has 5 triggers and the second one only one. What makes you think that `SA_gPresence` triggered the first rule in your log?

Change the first rule to this temporarily:

```php
rule "debug status printout"
when
    Item SA_downstairs_occupancy changed 
    or Item SA_girls_occupancy changed 
    or Item SA_boys_occupancy changed 
    or Item SA_parents_occupancy changed
    or Item SA_gPresence changed
then
    logInfo("SA", triggeringItem.name.toString)
    logInfo("SA", String::format("pres(%$1s)=Sen[D(%$2s)/P(%$3s)/G(%$4s)/B(%$5s)]"
        SA_gPresence.state, SA_downstairs_occupancy.state,SA_parents_occupancy.state,SA_girls_occupancy.state,SA_boys_occupancy.state))
end

```

To see which item triggered the rule

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [July 17, 2018, 5:32pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/9 "2018-07-17T17:32:08Z")

</div>

> [@sam2b](#):
>
> My suggestion is to also try _not_ to use `changed from OFF to ON` , and just use this:  
> `Item SA_gPresence changed to ON`

Since the original post i have switch to eliminating all constraints on the rule, the same issue occurs.

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [July 17, 2018, 5:36pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/10 "2018-07-17T17:36:15Z")

</div>

@vzorglub

All the rules as written work most of the time, however sometime it does not.

In the output there are two lines that are exactly the same (except the miliseconds), this is expected since the rule will be triggered twice when the very last item in the group changes.  
This is because it will trigger once when the item in the group changes to OFF, then a second trigger when the group changes to OFF since all its children are OFF.

However, the group changing should also trigger the second rule and print “enter global presence changed” and it does not.

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [July 17, 2018, 6:38pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/11 "2018-07-17T18:38:38Z")

</div>

> [@RedOranges](#):
>
> This is because it will trigger once when the item in the group changes to OFF, then a second trigger when the group changes to OFF since all its children are OFF.

You didn’t post the items so I didn’t know which item belonged to which group.  
If you trigger the group only through the rest api what happens?

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [July 17, 2018, 6:50pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/12 "2018-07-17T18:50:34Z")

</div>

Sorry about that

All the \*\_occupancy items are under the group SA\_gPresence

I’m not home at the moment, I’ll see if i can test through the API

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [July 18, 2018, 4:12am UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/13 "2018-07-18T04:12:12Z")

</div>

I don’t know whats going on  
I tried the rest API and changed the state of SA\_gPresence and only the “debug status printout” was printing

Then I commented out SA\_gPresence from that rule, saved it and updated the state again and this time it printed from the rule “Presence detected”.

So undid my change, saved it and updated the state again and this time it printed out from both rules!

I’m guessing that the the rules as i have written them are fine, its just after some time OH gets into a state where it wont trigger both rules anymore until OH is reset or refreshed.

I’m thinking I need to open a bug for this

---

<div class="post-metadata">

**Author:** ![Rivi](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@Rivi](https://community.openhab.org/u/Rivi)\
**Post date:** [July 18, 2018, 4:18am UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/14 "2018-07-18T04:18:56Z")

</div>

Ehmm, just a wild guess: Are your items and the group defined in two different files? I did have the case that when I did this, the group claimed to get updates in the log as it should, but any rule trigger that should have fired happily ignored it. Restarting oh solved the issue, but some (not all) edits in the item definition files could bring it back.

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [July 18, 2018, 4:30am UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/15 "2018-07-18T04:30:12Z")

</div>

Hmm, looks like all in one file.  
Although I have multiple files with item definitions, all the the items that are in the group SA\_gPresence are defined in the same file as the group itself.

But I’ll keep that in mind though thanks

---

<div class="post-metadata">

**Author:** ![sam2b](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@sam2b](https://community.openhab.org/u/sam2b)\
**Post date:** [July 19, 2018, 6:31am UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/16 "2018-07-19T06:31:24Z")

</div>

Another thought is to use Zulu instead of openjdk 1.8. If you have not tried it yet, stop OpenHAB, uninstall openjdk, and install Zulu.

[Installation Prerequisites say](https://www.openhab.org/docs/installation/#prerequisites) “Zulu is currently the recommended Java platform for openHAB.”.

> [@RedOranges](#):
>
> Java Runtime Environment: openjdk 1.8

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [July 20, 2018, 6:14pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/17 "2018-07-20T18:14:06Z")

</div>

Unfortunately Zulu isn’t available for x86 32bit.

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [August 2, 2018, 6:08pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/18 "2018-08-02T18:08:33Z")

</div>

I tried to simplify this and remove one of the rules and I still have the same problem.

I removed the debug printing rule from printing when the group changes so there is only one rule in question now.

Again this rules seems to work for a day or two then stops working at all.  
Note, my assumption is regardless of anything else in the rule I should have the first log line print on _any_ change to `SA_gPresence`, and it is not.

In my events log I get

`2018-08-02 12:53:17.418 [GroupItemStateChangedEvent] - SA_gPresence changed from ON to OFF through SA_parents_occupancy`

But I don’t get “enter global presence changed” in my log (once this issue rears its head)

Now I only the following rule where `SA_gPresence` is a group with several switch items in it

```
rule "Presence detected"
when 
    Item SA_gPresence changed
then
    logInfo("SA", "enter global presence changed")
    if (previousState == null)
    {
        logInfo("SA", "global presence was null, skip presence detection")
        return;
    }
    
    logInfo("SA", "global presence changed to " + SA_gPresence.state.toString)
    if (SA_delaySmartAwayTimer !== null)
    {
        logInfo("SA", "\tcancelling SA_delaySmartAwayTimer")
    
        SA_delaySmartAwayTimer.cancel()
        SA_delaySmartAwayTimer = null
    }
    if (SA_gPresence.state == ON)
    {
        if (firstEvent_climate.state == SA_SmartAwayClimateRef)
        {
            logInfo("SA", "\tWe were in our smart away climate")
            if (SI_DEBUG.state == OFF)
            {
                sendMail("myfake@email.com", "SmartAway", "Someone home, resuming schedule")
                logInfo("SA", "\tcalling ecobeeResumeProgram(" + ecobeeID +", true)")
                ecobeeResumeProgram(ecobeeID, true)
            }
        }
    }
    else if (SA_gPresence.state == OFF)
    {
        SA_delaySmartAwayTimer = createTimer(now.plusMinutes(SA_dismissMinuets), [|

                logInfo("SA", "Timer up, no occupancy for X min")
                if (SI_DEBUG.state == OFF)
                {
                    sendMail("myfake@email.com", "SmartAway", "No one home for 60 min, Holding on smart away climate")
                    logInfo("SA", "\tcalling ecobeeSetHold("+ecobeeID+", null, null, " + SA_SmartAwayClimateRef + ", null, null, 'holdHours', " + SA_holdHours +")") 
                    ecobeeSetHold(ecobeeID, null, null, SA_SmartAwayClimateRef, null, null, 'holdHours', SA_holdHours) 
                }
                SA_delaySmartAwayTimer = null
            ])
    }
end

```

Can anyone see why this rule would just stop working over time? is my timer causing the function to get stuck or something?

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [August 2, 2018, 8:22pm UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/19 "2018-08-02T20:22:52Z")

</div>

Are you running the rule with SI\_DEBUG off? If so, it is possibly the sendMail or ecobee actions that are hanging the rule. I would put a log at the end and check the log the next time it hangs to see if you ever got the last completion log. This would prove that it is this rule that is hanging. What is SA\_dismissMinuets set for, and is that a typo in your post or rule? In your ecobee log entry and action, are you using items for ecobeeID, SA\_SmartAwayClimateRef, etc.? If so, it looks like you are missing the .state.

Also, previousState may be NULL (an UnDefType), but it will never be null (undefined). And please post your item definitions.

---

<div class="post-metadata">

**Author:** ![RedOranges](https://community.openhab.org/user_avatar/community.openhab.org/redoranges/32/71780_2.png) [@RedOranges](https://community.openhab.org/u/RedOranges)\
**Post date:** [August 3, 2018, 3:02am UTC](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181/20 "2018-08-03T03:02:48Z")

</div>

Thanks for the replay @5iver

1. Yes SI\_DEBUG is off
2. I added some exit logging to determine where it fails. Hopefully I can figure it out now.
3. Those are val or var defined in the rules file, sorry i never included those in the post
4. When I tried to use NULL instead of null i got `Cannot reference the field 'NULL' before it is defined`

[Next page](https://community.openhab.org/t/solved-rule-doesnt-trigger-on-change-randomly/46181.md?page=2)
