Can someone help with this lighting rule?

I have my lighting set to go off at a point in the evening. I then want to revert to being controlled by a sensor overnight. I want to ensure that if the light is already ON, it is not interfered with.

I’ve modified an existing working rule and added in the check that it is currently Dark and that the light is OFF

I’m having issues and it isn’t working - can anyone help?

rule "Back Door Light control when its after bedtime!"
when
Item EventState02 received update "OPEN" 
then
if (Back_Standard_Window.state == OFF) 
{
	logInfo("Back Door Light Control","If it were dark, it would have been good to have this switched on ")
	if (Is_Dark.state == ON)
	{    
    	if (backroom_timer != null) 
    	{            
        backroom_timer.reschedule(now.plusMinutes(backroom_timeout))
        logInfo("Back Door Light Control","Light timer rescheduled for " + backroom_timeout + " minutes")
    	}
    	else
    	{            
		sendCommand(Back_Standard_Window, ON)
        logInfo("Back Door Light Control","Light timer create with " + backroom_timeout + " minutes")
        backroom_timer = createTimer(now.plusMinutes(backroom_timeout)) 
        [|        
            if (EventState02 ==  "DetectedMovement")   
            {
                backroom_timer.reschedule(now.plusMinutes(backroom_timeout))
                logInfo("Back Door Light Control","Light timer triggered, but rescheduled again for " + backroom_timeout + " minutes")                    
            } 
            else 
            {            
 				sendCommand(Back_Standard_Window, OFF)
                backroom_timer = null
            }
        ]
    	}
	}
	else
	{
	logInfo("Back Door Light Control","Its not dark")
	}
}
else 
{
	logInfo("Back Door Light Control","Light Already on, so should do nothing")
}
end

You have the rule activation when using the Item “EventState02”, but later on you make the check for:

EventState02 ==  "DetectedMovement"

This should be EventState02.state and “DetectedMovement” shouldn’t be a valid state in this case.

Thanks, I suspect that might cause an issue…I discovered another error

if (Back_Standard_Window.state == OFF)

should be

if (Back_Standard_Window.state == 0)

If Back_Standard_Window is a Contact it should actually be

if(Back_Standard_Window.state == CLOSED)

Hi all

I’m also after some assistance if you wouldn’t mind please…

I have a similar issue - to do with a coding a timer to switch off a light after 60 seconds of it being triggered on. The code has been modified from various other openhab community sources, and has actually worked twice today, but then no longer. I rebooted OpenHAB and Mosquitto in case it was getting a bit tied up but unfortunately still no joy. The light triggers on but is not sent a command to switch off (according to the logs).

When I get this working, I then need to incorporate an AND statement so that the light only comes on when triggered, and when dark. Day/Night will be read from another item.

Grateful for all assistance.

Here is my code:

var Timer garagelighttimer = null

	rule "Automatically Switch ON Garage Door Light"
    when   
            Item Driveway_CCTV_Trigger received command ON
    then   
            sendCommand(Light_Outdoor_Garagedoor, ON)
    end
    
	rule "Time Delay To Switch OFF Garage Door Light"
	when
			Item Driveway_CCTV_Trigger received command
	then
			if(receivedCommand==ON) {
				if(garagelighttimer==null) {
					garagelighttimer = createTimer(now.plusSeconds(60)) [|
						sendCommand(Light_Outdoor_Garagedoor, OFF)
			]
		} else {
			garagelighttimer.reschedule(now.plusSeconds(60))
		}
	} else if(receivedCommand==OFF) {
			if(garagelighttimer==null) {
				garagelighttimer = createTimer(now.plusSeconds(60)) [|
					sendCommand(Light_Outdoor_Garagedoor, OFF)
				]
		} else {
		 		garagelighttimer.reschedule(now.plusSeconds(60))
		}
	} 
	end     

You’ve tested multiple times for gargelighttimer==null, but this is probably false even when the timer is not doing anything.

You don’t actually need to reschedule, just garagelighttimer.cancel and garagelighttimer=null, then set it again:

Here’s a useful template for setting an action for a timer, just replace MOTION, TIMER, LIGHT and BRIGHTNESS (When you get it) to suit your needs.

var org.openhab.model.script.actions.Timer TIMER

rule "Motion Turns On Light"
when
	Item MOTION changed from OFF to ON
then
	if (BRIGHTNESS.state < 25) {
		LIGHT.sendCommand(ON)
	}
end

rule "Motion Time Out"
when
	Item Motion changed
then
	if(TIMER!=null) {
		TIMER.cancel
		TIMER = null
	}
	if(MOTION.state==OFF){
		TIMER = createTimer(now.plusMinutes(1)) [|
			LIGHT.sendCommand(OFF)
		]
	} 
end

(If anyone else is reading this post, some motion sensors are contact items, so don’t forget to change OFF to CLOSED and ON to OPEN.)

Hi Ben

Thank you for your reply. I tried your suggested example but unfortunately it didnt work for me.

Instead I used the following which seemed to work well. I’m not sure if there is something odd with my installation which prevents the example from working as intended.

var org.openhab.model.script.actions.Timer garagelighttimer

	rule "Automatically Switch ON Garage Door Light"
    when   
            Item Driveway_CCTV_Trigger received command ON
    then   
            sendCommand(Light_Outdoor_Garagedoor, ON)
    end

	rule "Motion Time Out"
	when
		Item Driveway_CCTV_Trigger received command
	then
		if(garagelighttimer!=null) {
			garagelighttimer.cancel
			garagelighttimer = null
		}
		if(Driveway_CCTV_Trigger.state==ON){
			garagelighttimer = createTimer(now.plusMinutes(1)) [|
				sendCommand(Light_Outdoor_Garagedoor, OFF)
			]
		} 
	end