# Openhab 2 rules not catching changes to strings

**URL:** <https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501>\
**Category:** Runtime\
**Tags:** rules, openhab2\
**Created:** [December 20, 2016, 9:59am UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501 "2016-12-20T09:59:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan12345](https://community.openhab.org/letter_avatar_proxy/v4/letter/d/c0e974/32.png) [@dan12345](https://community.openhab.org/u/dan12345)\
**Post date:** [December 20, 2016, 9:59am UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/1 "2016-12-20T09:59:15Z")

</div>

Hi,

I’m experiencing some odd behaviour with OH2 and rules driven by strings.

In OH1.8, this worked:

```
rule "Lights on when TV starts"
when
    Item HarmonyActivity changed from "PowerOff" to "TVon"
then
...

```

In OH2.0 this rule never fires, even when events.log shows HarmonyActivity changing from PowerOff to TVon.

I can fix it with this:

```
rule "Lights on when TV starts"
when
    Item HarmonyActivity received update
then
    if (HarmonyActivity.state=="TVon") {    
       ...

```

which works fine in this instance, but not for all use cases (e.g. sometimes one wants to catch a change from X to Y). I can replicate the same result with any notional String, whether or not linked to a binding.

Am I making a subtle mistake or is this a bug?

Dan

---

<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:** [December 21, 2016, 6:24pm UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/2 "2016-12-21T18:24:09Z")

</div>

I don’t know if it is a bug but I do know you can’t use Strings in changed from in OH 2 at this time.

Your new rule should be the following to match the same behavior as your old rule:

```auto
when
   Item HarmonyActivity changed
then
  if(HarmonyActivity.state=="TVon") {

```

It does work with ON/OFF and OPEN/CLOSED but I’m pretty sure it also works with Number Items as well.

---

<div class="post-metadata">

**Author:** ![danielwalters86](https://community.openhab.org/user_avatar/community.openhab.org/danielwalters86/32/37745_2.png) [@danielwalters86](https://community.openhab.org/u/danielwalters86)\
**Post date:** [December 21, 2016, 6:32pm UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/3 "2016-12-21T18:32:23Z")

</div>

Have you tried:

```php
Item HarmonyActivity changed from PowerOff to TVon

```

---

<div class="post-metadata">

**Author:** ![dan12345](https://community.openhab.org/letter_avatar_proxy/v4/letter/d/c0e974/32.png) [@dan12345](https://community.openhab.org/u/dan12345)\
**Post date:** [December 22, 2016, 10:30am UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/4 "2016-12-22T10:30:41Z")

</div>

thanks for confirming.

Dan

---

<div class="post-metadata">

**Author:** ![paglia](https://community.openhab.org/user_avatar/community.openhab.org/paglia/32/42097_2.png) [@paglia](https://community.openhab.org/u/paglia)\
**Post date:** [February 5, 2017, 3:25pm UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/5 "2017-02-05T15:25:41Z")

</div>

Has this issue been solved? I’m trying to fire an event whrn string changes from OFF to ON, but no way to do it…

---

<div class="post-metadata">

**Author:** ![HolgerL](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/779978/32.png) [@HolgerL](https://community.openhab.org/u/HolgerL)\
**Post date:** [February 5, 2017, 3:43pm UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/6 "2017-02-05T15:43:35Z")

</div>

I have an strange behavior with rules:  
I have a rule for a light timer. It does not fire after startup of the system.  
But if I go into the rule and change a comment text and save it again, it works!  
Looks like the initial loading is not working propperly.

---

<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:** [February 6, 2017, 3:52am UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/7 "2017-02-06T03:52:14Z")

</div>

Set up persistence on you String or save the last state of your String in a var.

Trigger your rule just on changed

Then use if statements to see if the changed from and to are what you are looking for

Be sure to do the book keeping to keep track of the previous state of you don’t use persistence.

---

<div class="post-metadata">

**Author:** ![paglia](https://community.openhab.org/user_avatar/community.openhab.org/paglia/32/42097_2.png) [@paglia](https://community.openhab.org/u/paglia)\
**Post date:** [February 6, 2017, 9:05am UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/8 "2017-02-06T09:05:05Z")

</div>

Thanks @rlkoshak - can you make an example? At least for variable save.  
I never worked with persistence.

---

<div class="post-metadata">

**Author:** ![Oli](https://community.openhab.org/user_avatar/community.openhab.org/oli/32/8233_2.png) [@Oli](https://community.openhab.org/u/Oli)\
**Post date:** [February 6, 2017, 9:15am UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/9 "2017-02-06T09:15:05Z")

</div>

I had a similar problem a while ago with a .items file. Last few entries where ignored.  
Long story short, there must have been some non-visible char somewhere; after deleting and reentering the last lines it worked. Maybe you have some problem with the general charset of the file (can especially happen if you edit on Windows and save to a linux type system).  
HTH…

---

<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:** [February 6, 2017, 4:52pm UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/10 "2017-02-06T16:52:43Z")

</div>

I highly recommend using Persistence for something like this. The non-persistence approach would be something like this:

```auto
var lastState

rule "MyItem state changed"
when
    Item MyItem changed
then
    if(lastState == "OFF" && MyItem.state == "ON"){
        // do work
    }
    lastState = MyItem.state.toString
end

```

NOTE: If your states are ON and OFF, why are you not using a Switch?

---

<div class="post-metadata">

**Author:** ![paglia](https://community.openhab.org/user_avatar/community.openhab.org/paglia/32/42097_2.png) [@paglia](https://community.openhab.org/u/paglia)\
**Post date:** [February 6, 2017, 6:03pm UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/11 "2017-02-06T18:03:31Z")

</div>

Putting altogheter… no result.  
This is the rule, swithc on two hue bulb if motion detection coes ON (events.log shows this event:

> var String state  
> rule “test”  
> when  
> //Time cron “\*/2 \* \* \* \* ?”  
> item HueMotionEvent changed  
> then  
> if (HueMotionEvent.state == “ON”) {

> ```
> var DecimalType hue = new DecimalType(240) 
> var PercentType sat = new PercentType(100)
> var PercentType bright = new PercentType(50) 
> var HSBType light = new HSBType(hue,sat,bright)
> 
> sendCommand(corr_salotto, light)
> sendCommand(corr_camere, light)
> Thread::sleep(120000)
> sendCommand(corr_camere, OFF)
> sendCommand(corr_salotto, OFF)
> 
> ```
> 
> }  
> end

This is the item definition:

> String HueMotionEvent “Hue Motion Detected [%s]” (Motion, Sensors) { http=“\<[[http://192.168.101.173/api/XXXXXXu0smt-2qXXXXDb0OMJXXXXccmmnXXXXXkb/sensors/3:500:JS(getHueMotionEvent.js)](http://192.168.101.173/api/XXXXXXu0smt-2qXXXXDb0OMJXXXXccmmnXXXXXkb/sensors/3:500:JS(getHueMotionEvent.js))]” }

This is the transform:

> (function(i) {  
> var json = JSON.parse(i);  
> return ((json[‘state’][‘presence’])) == true ? “ON” : “OFF”;  
> })(input)

this is the event:

> 2017-02-06 18:58:48.596 [ItemStateChangedEvent] - HueMotionEvent changed from OFF to ON

NO IDEA why is not working.  
Of course i can let Hue take care of that but the motion sensor is really a cool piece of hardware, i’d like to employ it in other ways.

---

<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:** [February 6, 2017, 6:51pm UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/12 "2017-02-06T18:51:19Z")

</div>

the word “item” in the rule trigger should be capitalized:

```auto
when
    Item HueMotionEvent changed
then

```

Add a log statement as the first line of the rule.

Review the following for a better way to do the above using timers. The way it is coded now your light will flicker off and on after two minutes depending how many additional motion detection events were detected..

> [@Design Pattern: Motion Sensor Timer](https://community.openhab.org/t/design-pattern-motion-sensor-timer/14954):
>
> Edit: Updates for OH 4 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 details on how to use DPs. Problem Statement Many motion detectors only report ON when they detect motion but do not send a corresponding OFF after no motion has been detected after a period of time. In addition, often one wants to control a device like a light for a certain period after the last motion is seen, not after the motion sensor goes to OFF. Therefore one needs to keep a Contact OPEN…

When you encounter errors like this your first thing to do should be to load your config into Designer to check for syntax errors. Get Eclipse SmartHome Designer 0.8 (not the 0.9 snapshot which has a major bug).

Thus, your rule should look something closer to:

Note: you are not even using the transition from OFF to ON in this case so no need to the var

```auto
var Timer timer = null

rule "test"
when
    Item HueMotionEvent received update
then
    if(HueMotionEvent.state ==ON) { // HueMotionEvent should be a Switch, not a String
        if(timer == null){
            var DecimalType hue = new DecimalType(240) 
            var PercentType sat = new PercentType(100)
            var PercentType bright = new PercentType(50) 
            var HSBType light = new HSBType(hue,sat,bright)

            corr_salotto.sendCommand(light)
            corr_camere.sendCommand(light)

            timer = createTimer(now.plusMinutes(2), [|
                corr_salotto.sendCommand(OFF)
                corr_camere.sendCommand(OFF)
                timer = null
            ])
        }
        else {
            timer.reschedule(now.plusMinutes(2)
        }
    }
end

```

In the above, the light will be turned on when an ON is detected on the motion sensor and a Timer that expires in two minutes is created. The body of the Timer turns the lights off. If another motion is detected before the Timer expires it is rescheduled for an additional two minutes into the future.

In short, the light stays ON until two minutes after the last motion event. No flickering in between events.

---

<div class="post-metadata">

**Author:** ![paglia](https://community.openhab.org/user_avatar/community.openhab.org/paglia/32/42097_2.png) [@paglia](https://community.openhab.org/u/paglia)\
**Post date:** [February 6, 2017, 7:42pm UTC](https://community.openhab.org/t/openhab-2-rules-not-catching-changes-to-strings/18501/13 "2017-02-06T19:42:19Z")

</div>

Perfect. It works.  
Thank you, i didn’t know anything about Eclipse SmartHome Designer - really useful.
