Delayed action on received command

I’m wanting to effectively power down the ‘living room’ 30 minutes after the TV goes off… but I’m struggling.

The items are all working, but It’s the rule I’m struggling with.

I’ve tried the following concoctions;

rule "BlahBlahCar"
when
	Item SamsungTV received command
then
	if(receivedCommand==ON) {
		if(tSamsungPower==null) {
			// first ON command, create a timer to turn the light off
			tSamsungPower = createTimer(now.plusSeconds(20)) [|
				sendCommand(Hue_LivingRoom_MainLight, OFF)
			]
		} else {
			// subsequent ON command, so reschedule the existing timer
			tSamsungPower.reschedule(now.plusSeconds(20))
		}
	} else if(receivedCommand==OFF) {
		// remove any previously scheduled timer
		if(tSamsungPower!=null) {
			tSamsungPower.cancel
			tSamsungPower = null
		}	
	}
end

^^ This one works when I the Samsung TV is detected as being ON, unfortunately, this is the exact opposite of what I want. I thought simply switching the receivedCommand==ON to with OFF and vice versa, would do the job. It doesnt :frowning:

So I tried both off, with no luck.

rule "BlahBlahCar"
when
	Item SamsungTV received command
then
	if(receivedCommand==OFF) {
		if(tSamsungPower==null) {
			// first OFF command, create a timer to turn the light off
			tSamsungPower = createTimer(now.plusSeconds(20)) [|
				sendCommand(Hue_LivingRoom_MainLight, OFF)
			]
		} else {
			// subsequent ON command, so reschedule the existing timer
			tSamsungPower.reschedule(now.plusSeconds(20))
		}
	} else if(receivedCommand==OFF) {
		// remove any previously scheduled timer
		if(tSamsungPower!=null) {
			tSamsungPower.cancel
			tSamsungPower = null
		}	
	}
end

and then I tried

rule "Turn Downstairs Lights off 5 minutes after Onkyo goes off"
when
	Item onkyoPower received command 
then
	if(receivedCommand==ON) {
		if(tOnkyoPower==null) {
			tOnkyoPower = createTimer(now.plusSeconds(300)) [|
				sendCommand(Hue_LivingRoom_MainLight, OFF)
				sendCommand(Hue_LivingRoom_Lamp, OFF)
				sendCommand(Hue_LivingRoom_TallLamp, OFF)				
			]
		} else {
			tOnkyoPower.reschedule(now.plusSeconds(300))
		}
	} else if(receivedCommand==OFF) {
		if(tOnkyoPower==null) {
			tOnkyoPower = createTimer(now.plusSeconds(300)) [|
				sendCommand(Hue_LivingRoom_MainLight, OFF)
				sendCommand(Hue_LivingRoom_Lamp, OFF)
				sendCommand(Hue_LivingRoom_TallLamp, OFF)
			]
		} else {
			tOnkyoPower.reschedule(now.plusSeconds(300))
		}	
	}
end

Again with no luck. Which is why I’m now here, asking for help/advice.

The variables exist, the timer in the first example works absolutely fine with lights on a motion sensor, so I’m not entirely sure what I’m missing.

If anything, the timer is too complex. I simply need to turn off the lights/other items after X amount of minutes, I don’t need to perform any other checks.

Any thoughts?

Maybe I’m completely off here, but shouldn’t the command you want to listen to be ‘KEY_POWEROFF’ instead of ‘OFF’?

With regards to the Samsung rule, my listening item ‘SamsungTV’ is actually a network health bind, pinging the TV.

The Onkyo example, uses the Onkyo binding.

I used two different methods as tests, because I couldn’t get one or the other working.

This seems to have worked tonight, unsure why, as it’s no different to what I’ve tried earlier. I’ve also included some mail ‘traps’ to see if/where its triggering.

rule "Turn Downstairs Lights off 5 minutes after TV goes off"
when
	Item SamsungTV received command 
then
	if(receivedCommand==ON) {
		if(tSamsungPower==null) {
			tSamsungPower = createTimer(now.plusSeconds(300)) [|
				sendCommand(Hue_LivingRoom_MainLight, OFF)
				sendCommand(Hue_LivingRoom_Lamp, OFF)
				sendCommand(Hue_LivingRoom_TallLamp, OFF)
				sendMail("poedhrupbgwz@bxc.io", "TV off, Lights off! (1)", "TV off, Lights off! (1)")						
			]
		} else {
			tSamsungPower.reschedule(now.plusSeconds(300))
		}
	} else if(receivedCommand==OFF) {
		if(tSamsungPower==null) {
			tSamsungPower = createTimer(now.plusSeconds(300)) [|
				sendCommand(Hue_LivingRoom_MainLight, OFF)
				sendCommand(Hue_LivingRoom_Lamp, OFF)
				sendCommand(Hue_LivingRoom_TallLamp, OFF)
				sendMail("poedhrupbgwz@bxc.io", "TV off, Lights off! (2)", "TV off, Lights off! (2)")										
			]
		} else {
			tSamsungPower.reschedule(now.plusSeconds(300))
		}	
	}
end