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