HELP: Motion Sensor Rule

As the topic would suggest I’m after a little help with building a new rule for when a motion sensor is activated. Let explain the scenario I’d like to accommodate.

The motion sensor is located in the first floor landing, for example when going to the bathroom over night. I would like to control the landing light during the Night. If motion is detected the light needs to switch on at say 20% brightness and stay on long enough. Maybe 10 seconds after the second motion sensor activation the light is switched off. Working on the approach that when going to the bathroom you’ll cause a single activation and then leaving the bathroom you’ll cause the second activation.

Firstly would this work and secondly who would I go about creating a rule for this? I’ve searched the forum to see if I can find enough examples to assist me but haven’t managed to find anything.

Thanks in advanced.


See the following for determining when it is night.

Now that we know it is night we have a couple of challenges with a rule like this.

  • How frequently does the motion detector go off? Do you truly only receive on event when you go past the detector and only one when you come back? Or do you receive multiple events in this case? I’ll assume it is only one but if it is multiple you will need to add some debouncing.

  • The second challenge is we need to keep track of whether a given motion sensor event is the first or the second one in the sequence and do something different based on which it is.

So the over all approach would be something like the following:


Contact MotionSensor ...
Switch Light ...

// Create a Swtich to help us determine whether this is the first or second triggering of the MotionSensor
// This could also be implemented as a global var in the Rules file, I like to use Items because I can use
// restoreOnStartup which lets the rule support restarts more reliably and with fewer lines of code.
Switch MotionSensorFlag


rule "MotionSensor triggered"
    Item MotionSensor received update
    if(TimeOfDay.state.toString == "Night") {
        // First MotionSensor trigger in the sequence
        // I use != ON so it works correctly in the case that MotionSensorFlag is Undefined
        if(MotionSensorFlag.state != ON) {

        // Second MotionSensor trigger in the sequence
        else {
            Thread::sleep(10000) // You could use a Timer but I think Thread::sleep results in simpler code and it works well here

The above only works if you truly only receive one event from the MotionSensor on each pass. If not it will get a whole lot more complicated. Search the forum for “debounce” and you will see lots of examples of ways to deal with it though.

EDIT: Forgot to add the test for Night

Thanks for your help. I’ll take a look later on when I’m at home.