Shut off all light if leave house and switch lights on when back depending on their previous state

Hello,

I’m using openhab2 togehter with knx to control all the items in my house (rollershutters, lights, etc.) and it works very good.

I’ dont have any programming skills and it is very hard for me tho understand how to work with rules.

I just want to archive the following Goal the easiest way by using a rule:

If I leave the house and lock the door then all lights should be switched off (e.g. Kittchen light, Bedroom light, etc.).

Wenn I come back and unlock the door all the light that where switched on (before i locked the door once i left the house) should return to their previous state.

That means that if this lights where switched on when i left the house and locked the door they must be switched on again once I retun and unlock the door.

I tried searching for a solution for several days but didn’t find anything that seems to work with openhab2.

I hope someone can help me to Archive this Goal.

Thanks in advance!

Finally I found a solution!

Here’s the rule:

var previousStates = null

rule "door closed"
when
Item door_lock changed from OPEN to CLOSED
then
// store the current states
previousStates = storeStates(gLight)
sendCommand (gLight, OFF)
end

rule "door unlocked"
when
Item door_lock changed from CLOSED to OPEN
then
// restore the states
if(previousStates != null) restoreStates(previousStates)
end

Eclipse SmartHome Designer ist showing the following error message:
Type mismatch: cannot convert from Object to Map<Item, State>
But it works anyway!

It’s almost straight forward:

  1. step will be to define an item MyLock which holds the state of your lock. this should be persisted.
  2. step is to define a group gAllLights (or something similar), put all light items into this group and do a persistence to this group either.
  3. build a “simple” rule:
rule "auto off and auto on"
when
    Item MyLock changed
then
    if (previousState != NULL) {
        if (MyLock.state == ON) {
            logInfo("auto_lights","Door locked -> switch off all lights!")
            gAllLights.sendCommand(OFF)
        }
        else {
            logInfo("auto_lights","Door unlocked -> switch all lights to old state!")
            val TimeDate MyLockLocked = MyLock.previousState(true).getTimestamp
            logInfo("auto_lights","Door was locked {}",MyLockLocked)
            gAllLights.members.forEach[i|
                i.sendCommand(i.historicState(MyLockLocked.minusSeconds(1)).state.toString)
            ]
        }
    }
end

I did not prove this code is correct, but you should first understand the theory anyway.

So, the theory is:

  • when the door gets locked, the rule sends a command OFF to all lights which belong to the group.
  • when the door gets unlocked, the rule gets the timestamp, when the door got locked.
    After that every item of the group is set to the state of this item, one second before door got locked

As my coding skills are very limited, I’m pretty sure the rule will not work without changes :wink: but this should be a starting point.

1 Like