Automatic Light Shut-Off Rules

Hi all,

Have had my openHAB setup running pretty well so far with basic app control and the like, and am trying to make my system more smart. Starting with a rule to automatically shut off all my lights when neither me or my girlfriend are in the apartment. The way I’d like it to work would be something like this:

If the front door is opened, then closed (When I leave the apartment). Then openHAB would wait 5 minutes, (in case I’m just going to check the mail and/or also to give me time to be out of wifi range) then check to see if either me or my girlfriend is home (Via Network binding to see if our phones are connected to WiFi), then if neither of us are at home, then send a command to shut off all the lights.

Sounded simple but I haven’t found any examples of this that made sense to me…

Apologies as I am still getting my head around rules and the like!


Well, you have to define items for your phones and the door:

Group:Switch:OR(ON,OFF) gMyLights     //all lights go to this group
Group:Switch:OR(ON,OFF) gMyPhones     //all phones go to this group
Switch Phone1online (gMyPhones) {...} //binding depends on your hardware
Switch Phone2online (gMyPhones) {...} //binding depends on your hardware
Contact Door {...}                    //binding depends on your hardware

and the rule:

var Timer tDoor = null

rule "auto lights off"
    Item Door changed from OPEN to CLOSED                      //door was open and is closed now
    if(tDoor!=null)                                            //timer running? then stop it...
    tDoor = createTimer(now.plusMinutes(5),[                   //start timer
        if(gMyPhones.members.filter(p|p.state==ON).size == 0)  //no phone online


If using dimmers, you will have to use an additional group, because the state will be 0 but not OFF for this sort of items.

I recommend separating this into two separate parts: presence and sensing.

For the presence detection part see Generic Presence Detection which shows a way to aggregate more than one presence detection sensor into one sate, with a five-minute timer to avoid flapping. I suggest this as I suspect you will care about presence elsewhere in your rules and most people find that just using Network is not sufficient for presence detection given how phones go into a deep sleep these days.

After that the rule is pretty much like Udo wrote. I’ll present an alternative implementation using Expire Binding instead of Timers.

Switch DoorTimer { expire="5m,command=OFF" }
rule "Door closed"
    Item Door changes from OPEN to CLOSED

rule "Door timer went off"
    Item DoorTimer received command OFF
    if(gPresence.state != ON) gMyLights.members.filter[l|l.state != OFF].forEach[l|l.sendCommand(OFF)]