# \[SOLVED\] Help with a light timer rule

**URL:** <https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940>\
**Category:** Beginners\
**Created:** [December 16, 2018, 12:57am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940 "2018-12-16T00:57:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![joedirt](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@joedirt](https://community.openhab.org/u/joedirt)\
**Post date:** [December 16, 2018, 12:57am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/1 "2018-12-16T00:57:06Z")

</div>

hi all, im looking for a little help with a light timer rule. im getting an error in the logs and im not sure what’s causing it.

```auto
2018-12-16 13:50:02.077 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Motion Timer in the Kitchen': cannot invoke method public abstract boolean org.eclipse.smarthome.model.script.actions.Timer.reschedule(org.joda.time.base.AbstractInstant) on null

```

my rule file is as follows

```php
var Timer KitchenLightsTimer = null
val int timeoutMinutesMorning = 60 // choose an appropriate value
val int timeoutMinutesDay = 60 // choose an appropriate value
val int timeoutMinutesAfternoon = 60 // choose an appropriate value
val int timeoutMinutesEvening = 60 // choose an appropriate value
val int timeoutMinutesNight = 60 // choose an appropriate value
val int timeoutMinutesBed = 5 // choose an appropriate value

rule "Kitchen_Motion_Timer ON"
when
	Item Lounge_Motion received update ON
then 
 	Kitchen_Motion_Timer.sendCommand(ON)
end

rule "Kitchen_Motion_Timer OFF"
when
	Item Kitchen_Motion_Timer received update OFF
then 
	sendCommand(Kitchen_Light, "0")
	sendCommand(Kitchen_PendantLightLeft, "82,55,0")
	sendCommand(Kitchen_PendantLightCenter, "82,55,0")
	sendCommand(Kitchen_PendantLightRight, "82,55,0")
	postUpdate(All_Lights, OFF)
end

rule "Motion Timer in the Kitchen"
when
    Item Kitchen_Motion_Timer received update ON
then
    if((KitchenLightsTimer == null) && (vTimeOfDay.state == "MORNING")) {
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesMorning ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
        KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesMorning ))
	sendNotification(“ *********** , "Kitchen Light Timer Has Been Reset!!")
    }
    if(KitchenLightsTimer == null) {
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesDay ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
        KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesDay ))
	sendNotification(“ ********* ”, "Kitchen Light Timer Has Been Reset!!")
    }
    if(KitchenLightsTimer == null) {
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesAfternoon ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
        KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesAfternoon ))
	sendNotification(“ *********** ”, "Kitchen Light Timer Has Been Reset!!")
    }
    if(KitchenLightsTimer == null) {
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesEvening ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
        KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesEvening ))
	sendNotification(“ ************ ”, "Kitchen Light Timer Has Been Reset!!")
    }
    if(KitchenLightsTimer == null) {
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesNight ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
        KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesNight ))
	sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Reset!!")
    }
    if(KitchenLightsTimer == null) {
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesBed ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
        KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesBed ))
	sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Reset!!")
    }
end

rule "Motion Sensor Kitchen Lights"

when
	Item Kitchen_Motion_Timer changed to ON
then
	if	((All_Lights.state == OFF) && (Someones_Home.state == ON) && (Kitchen_Motion_Timer.state == "MORNING"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((All_Lights.state == OFF) && (Someones_Home.state == ON) && (vTimeOfDay.state == "DAY"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((All_Lights.state == OFF) && (Someones_Home.state == ON) && (vTimeOfDay.state == "AFTERNOON"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((All_Lights.state == OFF) && (Someones_Home.state == ON) && (vTimeOfDay.state == "EVENING"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((All_Lights.state == OFF) && (Someones_Home.state == ON) && (vTimeOfDay.state == "NIGHT"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((All_Lights.state == OFF) && (Someones_Home.state == ON) && (vTimeOfDay.state == "BED"))
	{
	sendCommand(Kitchen_PendantLightCenter, "82,55,5")
	postUpdate(All_Lights, ON)
	}
end

```

any ideas or help would be appreciated, this is my most complicated rule to date!

---

<div class="post-metadata">

**Author:** ![yfaway](https://community.openhab.org/letter_avatar_proxy/v4/letter/y/58956e/32.png) [@yfaway](https://community.openhab.org/u/yfaway)\
**Post date:** [December 16, 2018, 2:16am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/2 "2018-12-16T02:16:48Z")

</div>

You have a logical error on this condition:

```auto
    if((KitchenLightsTimer == null) && (vTimeOfDay.state == "MORNING")) {

```

Your KitchenLightsTimer could be null, but because the time of day is not morning, thus the flow goes into the else clause where you reschedules on a null KitchenLightsTimer var.

---

<div class="post-metadata">

**Author:** ![jswim788](https://community.openhab.org/user_avatar/community.openhab.org/jswim788/32/36341_2.png) [@jswim788](https://community.openhab.org/u/jswim788)\
**Post date:** [December 16, 2018, 5:42am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/3 "2018-12-16T05:42:36Z")

</div>

Don’t you need to use === to compare against [null](https://community.openhab.org/t/after-openhabe-2-3-update-homematic-binding-dont-update-item-status/46070/4)?

---

<div class="post-metadata">

**Author:** ![lipp\_markus](https://community.openhab.org/user_avatar/community.openhab.org/lipp_markus/32/56218_2.png) [@lipp\_markus](https://community.openhab.org/u/lipp_markus)\
**Post date:** [December 16, 2018, 6:28am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/4 "2018-12-16T06:28:45Z")

</div>

> [@jswim788](#):
>
> Don’t you need to use === to compare against [null](https://community.openhab.org/t/after-openhabe-2-3-update-homematic-binding-dont-update-item-status/46070/4)?

Yes you do!

All instances of `KitchenLightsTimer == null` need to read `KitchenLightsTimer === null`

---

<div class="post-metadata">

**Author:** ![joedirt](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@joedirt](https://community.openhab.org/u/joedirt)\
**Post date:** [December 16, 2018, 6:59am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/5 "2018-12-16T06:59:07Z")

</div>

thanks guys for the help so far. the lights are now turning on but im not getting a notification to say that the timer has been reset.

this is what I have now.

```auto
var Timer KitchenLightsTimer = null
val int timeoutMinutesMorning = 60 // choose an appropriate value
val int timeoutMinutesDay = 60 // choose an appropriate value
val int timeoutMinutesAfternoon = 60 // choose an appropriate value
val int timeoutMinutesEvening = 60 // choose an appropriate value
val int timeoutMinutesNight = 60 // choose an appropriate value
val int timeoutMinutesBed = 5 // choose an appropriate value

rule "Kitchen_Motion_Timer ON"
when
	Item Lounge_Motion received update ON
then 
 	Kitchen_Motion_Timer.sendCommand(ON)
end

rule "Kitchen_Motion_Timer OFF"
when
	Item Kitchen_Motion_Timer received update OFF
then 
	sendCommand(Kitchen_Light, "0")
	sendCommand(Kitchen_PendantLightLeft, "82,55,0")
	sendCommand(Kitchen_PendantLightCenter, "82,55,0")
	sendCommand(Kitchen_PendantLightRight, "82,55,0")
	postUpdate(All_Lights, OFF)
end

rule "Motion Timer in the Kitchen"
when
    Item Kitchen_Motion_Timer received update ON
then
    if((KitchenLightsTimer === null) && (vTimeOfDay.state == "MORNING")) {
	sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Created!!")
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesMorning ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
    		if(vTimeOfDay.state == "MORNING") {
        		KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesMorning ))
			sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Reset!!")
    }
}

    if((KitchenLightsTimer === null) && (vTimeOfDay.state == "DAY")) {
	sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Created!!")
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesDay ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
    		if(vTimeOfDay.state == "DAY") {
        		KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesDay ))
			sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Reset!!")
    }
}

    if((KitchenLightsTimer === null) && (vTimeOfDay.state == "AFTERNOON")) {
	sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Created!!")
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesAfternoon ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
    		if(vTimeOfDay.state == "AFTERNOOON") {
        		KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesAfternoon ))
			sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Reset!!")
    }
}

    if((KitchenLightsTimer === null) && (vTimeOfDay.state == "EVENING")) {
	sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Created!!")
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesEvening ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
    		if(vTimeOfDay.state == "EVENING") {
        		KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesEvening ))
			sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Reset!!")
    }
}

    if((KitchenLightsTimer === null) && (vTimeOfDay.state == "NIGHT")) {
	sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Created!!")
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesNight ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
    		if(vTimeOfDay.state == "NIGHT") {
        		KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesNight ))
			sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Reset!!")
    }
}

    if((KitchenLightsTimer === null) && (vTimeOfDay.state == "BED")) {
	sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Created!!")
        KitchenLightsTimer = createTimer(now.plusMinutes(timeoutMinutesBed ), [|
            Kitchen_Motion_Timer.sendCommand(OFF)
            KitchenLightsTimer = null
        ])
    }
    else {
    		if(vTimeOfDay.state == "BED") {
        		KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesBED ))
			sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Reset!!")
    }
}

end

rule "Motion Sensor Kitchen Lights"

when
	Item Kitchen_Motion_Timer received command ON
then
	if	((Someones_Home.state == ON) && (vTimeOfDay.state == "MORNING"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((Someones_Home.state == ON) && (vTimeOfDay.state == "DAY"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((Someones_Home.state == ON) && (vTimeOfDay.state == "AFTERNOON"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((Someones_Home.state == ON) && (vTimeOfDay.state == "EVENING"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((Someones_Home.state == ON) && (vTimeOfDay.state == "NIGHT"))
	{
	sendCommand(Kitchen_Light, "100")
	sendCommand(Kitchen_PendantLightLeft, "82,55,100")
	sendCommand(Kitchen_PendantLightCenter, "82,55,100")
	sendCommand(Kitchen_PendantLightRight, "82,55,100")
	postUpdate(All_Lights, ON)
	}

	if	((Someones_Home.state == ON) && (vTimeOfDay.state == "BED"))
	{
	sendCommand(Kitchen_PendantLightCenter, "82,55,5")
	postUpdate(All_Lights, ON)
	}
end

```

---

<div class="post-metadata">

**Author:** ![lipp\_markus](https://community.openhab.org/user_avatar/community.openhab.org/lipp_markus/32/56218_2.png) [@lipp\_markus](https://community.openhab.org/u/lipp_markus)\
**Post date:** [December 16, 2018, 7:17am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/6 "2018-12-16T07:17:53Z")

</div>

> [@joedirt](#):
>
> but im not getting a notification to say that the timer has been reset.

Not sure if you are wondering whether the corresponding parts of your rule execute or if you are having an issue with the way notifications are configured. If you are interested in the former, just add `loginfo("my rule", "put text of notifications here")` statements and you can see in your log-file what is happening.  
For the latter, I would suggest to open a new thread.

---

<div class="post-metadata">

**Author:** ![joedirt](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@joedirt](https://community.openhab.org/u/joedirt)\
**Post date:** [December 16, 2018, 7:32am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/7 "2018-12-16T07:32:53Z")

</div>

I don’t think the else part of the rule is triggering. I’ve changed the notification to.

```auto
			loginfo("my rule", "Kitchen Light Timer Has Been Reset")

```

and im not seeing anything in the logs searching for “Kitchen Light Timer Has Been Reset” any ideas?

---

<div class="post-metadata">

**Author:** ![joedirt](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@joedirt](https://community.openhab.org/u/joedirt)\
**Post date:** [December 16, 2018, 8:00am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/8 "2018-12-16T08:00:59Z")

</div>

ok its my stuff up. spot the typo!!

```auto
 else {
    		if(vTimeOfDay.state == "AFTERNOOON") {
        		KitchenLightsTimer.reschedule(now.plusMinutes(timeoutMinutesAfternoon ))
			sendNotification(“ ********** ”, "Kitchen Light Timer Has Been Reset!!")
    }

```

changing AFTERNOOON to AFTERNOON and all is well.

thanks guys for your help.

---

<div class="post-metadata">

**Author:** ![lipp\_markus](https://community.openhab.org/user_avatar/community.openhab.org/lipp_markus/32/56218_2.png) [@lipp\_markus](https://community.openhab.org/u/lipp_markus)\
**Post date:** [December 16, 2018, 8:17am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/10 "2018-12-16T08:17:06Z")

</div>

Great…if you are interested in simplifying your rules (avoiding all the repetitive if clauses for example), this may help

> [@Design Pattern: How to Structure a Rule](https://community.openhab.org/t/design-pattern-how-to-structure-a-rule/45360):
>
> If you are unfamiliar with Design Patterns, please see [Design Pattern: What is a Design Pattern and How Do I Use Them](https://community.openhab.org/t/design-pattern-what-is-a-design-pattern-and-how-do-i-use-them/40319). Problem Statement Rules developers will frequently encounter situations where they have to perform only slightly different tasks depending on the states of two or more Items or variables. The most obvious approach is to have a series of nested if/else statements or switch statements and repeat the same code over and over with the slight variations in each section. This approac…

and this one

> [@Design Pattern: Encoding and Accessing Values in Rules](https://community.openhab.org/t/design-pattern-encoding-and-accessing-values-in-rules/36210):
>
> Edit: Added link to item\_init library. Please see [Design Pattern: What is a Design Pattern and How Do I Use Them](https://community.openhab.org/t/design-pattern-what-is-a-design-pattern-and-how-do-i-use-them/40319) for how to read and use a DP. Problem Statement Often a user has the desire to be able to store and retrieve a series of constants or other values in a Rule. For example, a user may have an RFID reader and want to store the list of authorized and disallowed IDs in a flexible manner. As another example, one may be sending commands to a gateway and need to include the ID of the target…

---

<div class="post-metadata">

**Author:** ![joedirt](https://community.openhab.org/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@joedirt](https://community.openhab.org/u/joedirt)\
**Post date:** [December 16, 2018, 9:26am UTC](https://community.openhab.org/t/solved-help-with-a-light-timer-rule/59940/11 "2018-12-16T09:26:02Z")

</div>

thanks, I started off with  
[Design Pattern: Motion Sensor Timer](https://community.openhab.org/t/design-pattern-motion-sensor-timer/14954)  
but I wanted to make it work for the different times of day and the above is the best way that I could think of. if you or anyone else have any ideas or the time to show how too do it more efficiently im really keen too learn.
