Disable motion lighting rule at the actual light switch

I have the lights in my basement turn on and off automatically by a motion detector with the rules below. Sometimes my son will have friends over and they end up sleeping in the basement. Is there a way to temporarily disable the rule for a period of time at the actual light switch. Thanks for any help.

rule "BasementMotionLightOn"
    when   
            Item BasementMotion changed from CLOSED to OPEN 
    then   
            if(BasementLights2.state == 0) {
            sendCommand(BasementLights2, 60)
            sendCommand(BasementLights5, 60)
            sendCommand(BasementBackRoom, 60)
            }
    end

    rule "BasementMotionLightOff"
    when   
            Item BasementMotion changed from OPEN to CLOSED
    then 
            if(basementTimer!=null) {
                basementTimer.cancel
                basementTimer = null
            }
            if(BasementLights2.state != 0) {
            basementTimer = createTimer(now.plusMinutes(30)) [|
            sendCommand(BasementLights2, OFF)
            sendCommand(BasementLights5, OFF)
            sendCommand(BasementBackRoom, OFF)
            ]
            } 
    end

Assuming that the physical light switch does indeed report its status to openHAB, this is a perfect situation to apply @bob_dickenson’s Proxy Design pattern documented here

The short version is create a Proxy Item to represent the light. This proxy Item is not bound to anything but it does have one or more rules that trigger when the Proxy is switched. In the rule you can check for certain states and/or set states before triggering the light (or deciding not to trigger the light). So when the light is turned off at the switch you can set a State Item which indicates the motion sensors are overridden; set a timer too to turn the state Item off after a time. Now, when the motion sensor triggers it triggers the rule which checks the State Item before actually turning on the light.

Thanks for pointing me in the right direction. I’m not sure I did it the same way as Bob, but I did use a virtual switch.
It appears to be working. My virtual switch “BasementMotionSwitch” is triggered off and on by turning off and on the Z-Wave bulb, “BasementBackRoom”, in my unfinished part of my basement. Then one of my sunrise rules turns the “BasementMotionSwitch” back on if my son doesn’t. If you see anything wrong with my logic, or cleaning it up some to reduce lines let me know. I know I have to come up with better shorter names for my items.

rule "Basement Motion- Lights On"
    when   
            Item BasementMotion changed from CLOSED to OPEN 
    then   
            if(BasementMotionSwitch.state == ON) {
            if(BasementLights2.state == 0) {
            sendCommand(BasementLights2, 60)
            sendCommand(BasementLights5, 60)
            sendCommand(BasementBackRoom, 60)
            }}
    end

    rule "Basement Motion- Lights Off"
    when   
            Item BasementMotion changed from OPEN to CLOSED
    then 
            if(basementTimer!=null) {
                basementTimer.cancel
                basementTimer = null
            }
            if(BasementMotionSwitch.state == ON) {
            if(BasementLights2.state != 0) {
            basementTimer = createTimer(now.plusMinutes(30)) [|
            sendCommand(BasementLights2, OFF)
            sendCommand(BasementLights5, OFF)
            sendCommand(BasementBackRoom, OFF)
            ]
            }} 
    end

    rule "Basement Motion Override"
    when 
            Item BasementBackRoom changed from 60 to 100
    then
            sendCommand(BasementLights2, OFF)
            sendCommand(BasementLights5, OFF)
            sendCommand(BasementBackRoom, OFF)
            sendCommand(BasementMotionSwitch, OFF)
    end

    rule "Cancel Basement Motion Override"
    when 
            Item BasementBackRoom changed from 0 to 100 
    then
            sendCommand(BasementLights2, 60)
            sendCommand(BasementLights5, 60)
            sendCommand(BasementBackRoom, 60)
            sendCommand(BasementMotionSwitch, ON)
    end

Not necessarily. My Item names are pretty long. The biggest thing you want to do is come up with a naming convention and stick to it. For example, here is my naming convention:

// Naming Conventions:
//  - Groups all start with a lower case g
//  - Items are all capitalized camel case
//  - Takes the form of X_X_Name
//  - The first letter denotes the Item type:
//      None    Internal Utility
//      'N'     Sensor
//      'S'     Switch
//      'T'     Trigger (switch without a state)
//      'W'     Weather
// - The second letter denotes the object type
//      None    Internal Utility
//      'C'     Conroller
//      'D'     Door
//      'I'     Input
//      'L'     Light
//      'V'     Value
//      'W'     Window

I’m the first to admit this is not a very good naming scheme. It was my first attempt and I’ve not gone back to think about a better approach. But the key is to come up with something that will help you as your HA grows.

I recommend using proper indentation. For example:

if(BasementMotionSwitch.state == ON) {
    if(BasementLights2.state == 0) {
        sendCommand ...
    }
}

Proper indentation will help you avoid scope errors because you can just look and see what you intended your scope to be rather than needing to count curly brackets.

The only thing I can see that would save you a handful of lines of code would be to put BasementLights2, BasementLights5, BasementBackRoom, and BasementMotionSwitch in the same group you can then issue the OFF commands and ON commands using something like

gBasementLights.members.forEach[s | s.sendCommand(OFF)]

For the ON you can still do a one liner but you need to be a little tricker:

gBasementLights.members.forEach[s | if(s instanceof DimmerItem) s.sendCommand(60) else s.sendCommand(ON)]

You can have Items be members of multiple groups as well so if you wanted to create one group that just included the Dimmers for your Lights On rule and everything for your override rules you can.

I like this approach because as your HA system grows or your configuration changes all you have to do is add and remove items from membership in your various groups and you never have to touch your rules. It also makes for terser and easier to read rules code.

One final bit of advice. Where ever possible I recommend calling the sendCommand and postUpdate methods on the Items rather than using the actions like you do above. The Item’s methods are necessarily smarter about converting the data you pass to it into something the Item can understand than the generic action methods which have to work for all Item types. You will save some headaches in the future if you follow this advice.

1 Like

trying to do the same thing but i’m unable to
i have a fibaro relay and a fibaro motion sensor.
what i want is that the motion sensor turns on the light if its darker then 18 lux and turns the light back off after 2 min no motion.
thats the part that works perfectly.
now i want to be able to switch my light manually and ignore the motion sensor till i switch ik back off manually, so actually i want to still be able to turn the light on /off with the switch and otherwise use the motion detector .

anyone any idea cause i’m thinking myself crazy with this proxy thing

kindest regards

wolf

so i adapted your code to mine but as you see when the lightswitch is updated to on it will end at rule 1 as it executes the same.

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer
import java.util.Date
import java.util.format

var Timer Bureau_timer = null

rule "Bureau Motion- Lights On"
when
Item Z_Bureau_Motion changed from CLOSED to OPEN
then
if ((Z_Bureau_Licht_PROXY.state == ON)&&((Z_Bureau_Lux.state as DecimalType) < 18)) {
if(Z_Bureau_Licht.state == OFF) {
sendCommand(Z_Bureau_Licht, ON)

        }}
end

rule "Basement Motion- Lights Off"
when   
        Item Z_Bureau_Motion changed from OPEN to CLOSED
then 
        if(Bureau_timer!=null) {
            Bureau_timer.cancel
           Bureau_timer = null
        }
        if(Z_Bureau_Licht_PROXY.state == ON) {
        if(Z_Bureau_Licht.state != OFF) {
        Bureau_timer = createTimer(now.plusMinutes(2)) [|
        sendCommand(Z_Bureau_Licht, OFF)
        ]
        }} 
end

rule "Basement Motion Override"
when 
   Item Z_Bureau_Licht changed from OFF to ON
then
	   sendCommand(Z_Bureau_Licht_PROXY, OFF)
	   
end

rule "Cancel Basement Motion Override"
when 
        Item Z_Bureau_Licht changed from ON to OFF 
then
		sendCommand(Z_Bureau_Licht_PROXY, ON)
end