Openhab 2 rules not catching changes to strings

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

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:

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.

Have you tried:

Item HarmonyActivity changed from PowerOff to TVon
1 Like

thanks for confirming.

Dan

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…

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.

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.

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

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…

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

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?

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)]" }

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.

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

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…

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

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.

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