Kitchenlight - Changing Timer

Hello Community

I’m at the tuning of my kitchenlight…
My idea is:
If you are only a short time in the kitchen, the light went of quickly. But the longer you stay in the kitchen, the longer the light will be on.

My rule looks like:

rule "Kuechenlicht"
when
Item Bewegung_Kueche received update
then

if((Helligkeit_Kueche.state < 15) && (Licht_Kueche.state == OFF)) {
	logInfo("SmaWo Lichststeuerung", "Licht in der Küche wurde eingeschalten")
	Licht_Kueche.sendCommand(ON)
	KiLi_Auto = true
	KiLi_Counter = KiLi_Counter + 1
	timer_kueche = createTimer(now.plusSeconds(30)) [|
		logInfo("SmaWo Lichststeuerung", "Licht in der Küche wurde nach 30 Sekunden ausgeschalten. Counter bei:" +KiLi_Counter)
		Licht_Kueche.sendCommand(OFF)
		KiLi_Counter = 0
		KiLi_Auto = false
	]
}
else if ((Licht_Kueche.state == ON) && (KiLi_Auto == true)) {
	timer_kueche.cancel
	KiLi_Counter = KiLi_Counter + 1
	
	if (KiLi_Counter < 5) {
		timer_kueche = createTimer(now.plusSeconds(60)) [|
			logInfo("SmaWo Lichststeuerung", "Licht in der Küche wurde nach 1 Minute ausgeschalten. Counter bei:" +KiLi_Counter)
			Licht_Kueche.sendCommand(OFF)
			KiLi_Counter = 0
			KiLi_Auto = false
		] 
	}
	if ((KiLi_Counter > 5) && (KiLi_Counter < 10)){
		timer_kueche = createTimer(now.plusMinutes(2)) [|
			logInfo("SmaWo Lichststeuerung", "Licht in der Küche wurde nach 2 Minuten ausgeschalten. Counter bei:" +KiLi_Counter)
			Licht_Kueche.sendCommand(OFF)
			KiLi_Counter = 0
			KiLi_Auto = false
		]			
	}
	if (KiLi_Counter > 10) {
		timer_kueche = createTimer(now.plusMinutes(5)) [|
			logInfo("SmaWo Lichststeuerung", "Licht in der Küche wurde nach 5 Minuten ausgeschalten. Counter bei:" +KiLi_Counter)
			Licht_Kueche.sendCommand(OFF)
			KiLi_Counter = 0
			KiLi_Auto = false
		]			
	} 
}

end

I’ve defined two variables:
var boolean KiLi_Auto = false
var Number KiLi_Counter = 0

But in the console i see

2016-02-27 19:21:19.036 [INFO ] [runtime.busevents ] - Bewegung_Kueche state updated to OPEN
2016-02-27 19:21:19.296 [INFO ] [m.script.SmaWo Lichststeuerung] - Licht in der Küche wurde eingeschalten
2016-02-27 19:21:19.401 [INFO ] [runtime.busevents ] - Licht_Kueche received command ON
2016-02-27 19:21:19.893 [INFO ] [runtime.busevents ] - Helligkeit_Kueche state updated to 3
2016-02-27 19:21:20.220 [INFO ] [runtime.busevents ] - Temp_Kueche state updated to 21
2016-02-27 19:21:20.685 [INFO ] [runtime.busevents ] - Licht_Kueche state updated to ON
2016-02-27 19:21:25.767 [INFO ] [runtime.busevents ] - Bewegung_Kueche state updated to OPEN
2016-02-27 19:21:26.032 [INFO ] [runtime.busevents ] - Helligkeit_Kueche state updated to 8
2016-02-27 19:21:26.294 [INFO ] [runtime.busevents ] - Temp_Kueche state updated to 21
2016-02-27 19:21:33.076 [INFO ] [runtime.busevents ] - Bewegung_Kueche state updated to OPEN
2016-02-27 19:21:33.356 [INFO ] [runtime.busevents ] - Helligkeit_Kueche state updated to 8
2016-02-27 19:21:33.753 [INFO ] [runtime.busevents ] - Temp_Kueche state updated to 21
2016-02-27 19:21:40.646 [INFO ] [runtime.busevents ] - Bewegung_Kueche state updated to OPEN
2016-02-27 19:21:40.881 [INFO ] [runtime.busevents ] - Helligkeit_Kueche state updated to 8
2016-02-27 19:21:41.290 [INFO ] [runtime.busevents ] - Temp_Kueche state updated to 21
2016-02-27 19:21:42.578 [INFO ] [runtime.busevents ] - Rasplex_Wohnzimmer_Power state updated to OFF
2016-02-27 19:21:47.269 [INFO ] [runtime.busevents ] - Bewegung_Kueche state updated to OPEN
2016-02-27 19:21:47.381 [INFO ] [runtime.busevents ] - Helligkeit_Kueche state updated to 8
2016-02-27 19:21:47.571 [INFO ] [runtime.busevents ] - Temp_Kueche state updated to 21
2016-02-27 19:21:54.443 [INFO ] [runtime.busevents ] - Bewegung_Kueche state updated to OPEN
2016-02-27 19:21:55.580 [INFO ] [runtime.busevents ] - Helligkeit_Kueche state updated to 9
2016-02-27 19:21:55.829 [INFO ] [runtime.busevents ] - Temp_Kueche state updated to 21
2016-02-27 19:22:36.315 [INFO ] [m.script.SmaWo Lichststeuerung] - Licht in der Küche wurde nach 1 Minute ausgeschalten. Counter bei:6
2016-02-27 19:22:38.288 [INFO ] [runtime.busevents ] - Licht_Kueche received command OFF
2016-02-27 19:22:38.612 [INFO ] [runtime.busevents ] - Licht_Kueche state updated to OFF

2016-02-27 19:24:01.178 [INFO ] [m.script.SmaWo Lichststeuerung] - Licht in der Küche wurde nach 2 Minuten ausgeschalten. Counter bei:0

Where is my logical error?

Thank you

I’m not sure, but I think you have to define KiLi_Counter as Integer, not Number.

I’m not entirely certain what specifically the problem is (language is a barrier to quickly scanning what is happening in the log for me) but I notice that you create timer_kueche under a whole lot of conditions without ever checking to see if timer_kueche already has an active timer assigned to it. This will cause more than one Timer to be running at the same time. Before calling createTimer you should test to see if there already is one set and cancel it before creating a new one. Add the following to the top of your rule:

if(timer_kueche != null) {
    timer_kueche.cancel
}

Hello everybody

@Udo_Hartmann: I’ve changed the variable to integer, with no result.

@rlkoshak: So, you get a little bit german lessons :wink:
Bewegung_Küche means Movement Kitchen.

I set a variable, which defines (or should define) how long the timer for the kitchen light is set.
If I’m only a shot time in the kitchen (1 to 5 Movement-Detections) the light should go off after 60 seconds (after last Movement-Detection)
If I’m (or my wife :wink: ) stay middle long time in the kitchen (6 to 10 Movement-Detections) the light should go off after 2 Minutes.(after last Movement-Detection)
If my wife stays longer in the kitschen (more than 10 Movement-Detections) the light should switch off after 5 Minutes.(after last Movement-Detection).

My idea behind this staging is if you are e.g. cuting some vegetables the sensor could not see a movement for this time. I want to prevent that the light switches off, if you are deep in the work.

In the log you can see, that the first condition is triggered (switching light off after 1 Minute), but my Counter counts 6, so the second condition is triggered.

I didn’t check the state of the timer, because I thought it would be always in a defined state…

It isn’t an issue of the Timer being in a defined state. It is an issue of creating multiple Timers.

If you create a timer to go off in two minutes and assign it to timer_kueche and then one minute later you create a timer to go off in two minutes and assign it to timer_kueche, the first timer still exists and will still execute when the time is up. Since you do not check to see if you already have an active timer and cancel it my first thought was that you were creating multiple timers that are all running at the same time when what you really want is only the most recently created Timer to be running.

OK, I think I understand

I will add the check at the very beginning of my rule.
Does this solve my Problem with the wrong counter-value too?

I don’t know. It might.