Rule help needed please

I have attempted to put together a rule that turns the lights left on by my wife and son.

I’m currently running OH 2.0.0 and can not seem to get this to trigger. I’ve changed the time from minutes to seconds with no luck.

Do I need to add any “includes” at the top of my rule file?

Any suggestions appreciated.

The example below is based on this item:

// Master Bedroom
Switch MasterCloset "Master Closet" [ "Switchable" ] {channel="zwave:device:ad34b696:node12:switch_binary"}

With this rule…

var Timer mcloset

rule "Master Closet Lights Left On"
when 
	Item MasterCloset changed to ON or
	Item MasterCloset changed to OFF
then
	if(mcloset != null) {
		mcloset.cancel
        mcloset = null
    }
    
    if(MasterCloset.state == ON){
		mcloset =  createTimer(now.plusMinutes(10)) [|
			MasterCloset.sendCommand(OFF)
	      ]
      }
      
end

No includes needed since OH2!

first of all i would declare your timer variable like this:
var Timer mcloset = null

then i woulld change the Rule to:

rule "Master Closet Lights Left On"
when 
	Item MasterCloset received update
then
	if (mcloset != null) {
		mcloset.cancel()
                mcloset = null
		logInfo("debug","timer cancelled!")
        }
        if (MasterCloset.state == ON) {
                logInfo("debug","timer created! Executes @ {}",now.plusMinutes(10))
		mcloset = createTimer(now.plusMinutes(10)) [|
			MasterCloset.sendCommand(OFF)
			mcloset = null
			logInfo("debug","timer executed!")
	        ]
        }
end

That should normally work… see the debug entrys in your log file…

Hello and thanks for you help…

I tried your rule and it did not work initially, in fact I could find no items in the log file either.

The rule was on a page with three other iterations of the same rule for other lights.

Just to be sure I created a new rules document with only the one rule and sure enough it worked! I even went back and tried my original rule on it’s own individual page and sure enough it worked as well!

I have placed multiple rules on the same page many times previously…is there something special I need to do with timers? Each rule has an end statement?

Am I missing something simple?

Thanks,

Squid

Here’s the rules file with multiple entries:

var Timer zclosettimer

rule "Zane's Closet Lights Left On"
when 
	Item ZaneCloset changed to ON or
	Item ZaneCloset changed to OFF
then
	if(zclosettimer != null) {
		zclosettimer.cancel
        zclosettimer = null
    }
    
    if(ZaneCloset.state == ON){
		zclosettimer =  createTimer(now.plusMinutes(5)) [|
			ZaneCloset.sendCommand(OFF)
	      ]
      }
      
end


var Timer zsconcetimer

rule "Zane's Sconces Left On"
when 
	Item ZaneSconce changed to ON or
	Item ZaneSconce changed to OFF
then
	if(zsconcetimer != null) {
		zsconcetimer.cancel
        zsconcetimer = null
    }
    
    if(ZaneSconce.state == ON){
		zsconcetimer =  createTimer(now.plusMinutes(60)) [|
			ZaneSconce.sendCommand(OFF)
	      ]
      }
      
end

var Timer mcloset

rule "Master Closet Lights Left On"
when 
	Item MasterCloset changed to ON or
	Item MasterCloset changed to OFF
then
	if(mcloset != null) {
		mcloset.cancel
        mcloset = null
    }
    
    if(MasterCloset.state == ON){
		mcloset =  createTimer(now.plusMinutes(10)) [|
			MasterCloset.sendCommand(OFF)
	      ]
      }
      
end

var Timer zclosettimer = null
var Timer zsconcetimer = null
var Timer mcloset = null

You need to put all global variables to the top of the rule file!

edit: or you use local variables and put them inside of the rules

Putting all the var at the top is one of those things that shouldn’t matter, but it seems it does.

When you get it working, for this simple application you might consider instead installing the expire binding. It’s not always appropriate, but for lighting timeouts is ideal.

Switch mylight ...  { expire="10m,command=OFF", channel="zwave:device:ad34 ...  }