Two step lightcontrol with motion sensor

Hi there!

I bought a Hue motion sensor, everything working fine. I havce a rule to switch off the lights 5 minutes after motion is detected.
I would like to insert an additional step to first, just dimm the lights for ie: to 10% and after an additional couple of minutes turn them off.

My current rule is this:

    if (Motion_Timer !== null)		//sensor time is running
                   		{
                   		HueWhiteLamp1_Brightness.sendCommand("100")
                   		HueWhiteLamp2_Brightness.sendCommand("100")
                           logInfo("rules", "Reschedule timer with " + sensorDay_timeout + " minuts")
                           Motion_Timer.reschedule(now.plusMinutes(sensorDay_timeout))
                   		}
                   else
                   		{
                   		HueWhiteLamp1_Brightness.sendCommand("100")
                   		HueWhiteLamp2_Brightness.sendCommand("100")
                           logInfo("rules", "Create timer with " + sensorDay_timeout + " minuts")
                           Motion_Timer = createTimer(now.plusMinutes(sensorDay_timeout))
                           		[|
                                   if (HueMotionSensor1_Motion.state ==  ON)
                                   		{
                                           logInfo("rules", "Rescheduleing, there is still motion with " + sensorDay_timeout + " minuts")
                                           Motion_Timer.reschedule(now.plusMinutes(sensorDay_timeout))
                                   		}
                                   else
                                   		{
                                   		HueWhiteLamp2_Brightness.sendCommand("OFF")
                                   		HueWhiteLamp1_Brightness.sendCommand("OFF")
                                           logInfo("rules", "the light is turning off")
                                           Motion_Timer = null
                                   		}
                           		]
                   		}

Thanks!

What have you tried? Personally I tend to use the expire binding & timers

           if (Motion_Timer !== null)		//sensor time is running
           		{
           		   HueWhiteLamp1_Brightness.sendCommand("99")
                   HueWhiteLamp2_Brightness.sendCommand("99")
                   createTimer(now.plusMinutes(5)) [|
                        if(HueWhiteLamp1_Brightness.state == 99 && HueWhiteLamp2_Brightness.state == 99){
                            HueWhiteLamp2_Brightness.sendCommand("5")
                            HueWhiteLamp1_Brightness.sendCommand("5")
                        }
                   ]
                   logInfo("rules", "Az elejen Reschedule timer with " + sensorDay_timeout + " minutes")
                   Motion_Timer.reschedule(now.plusMinutes(sensorDay_timeout))
           		}
           else
           		{
                    logInfo("rules", "Create timer with " + sensorDay_timeout + " minutes")
                    Motion_Timer = createTimer(now.plusMinutes(sensorDay_timeout))
                   		[|
                           if (HueMotionSensor1_Motion.state ==  ON){
           		                HueWhiteLamp1_Brightness.sendCommand("98")
                                HueWhiteLamp2_Brightness.sendCommand("98")
                                createTimer(now.plusMinutes(5)) [|
                                    if(HueWhiteLamp1_Brightness.state == 98 && HueWhiteLamp2_Brightness.state == 98){
                                        HueWhiteLamp2_Brightness.sendCommand("5")
                                        HueWhiteLamp1_Brightness.sendCommand("5")
                                    }
                                ]
                                   logInfo("rules", "kozepen Rescheduleing, there is still motion with " + sensorDay_timeout + " minutes")
                                   Motion_Timer.reschedule(now.plusMinutes(sensorDay_timeout))
                           		}
                           else
                           		{
                                   logInfo("rules", "the light is turning off")
                                   Motion_Timer = null
                                   HueWhiteLamp2_Brightness.sendCommand("OFF")
                                   HueWhiteLamp1_Brightness.sendCommand("OFF")
                           		}
                   		]
           		}

I tried this but it’s not respecting if some one comes back. I’m fairly novice. I have no idea how to cancel the second brightness command if someone comes back.

1 Like

Perhaps this example I use can give you some hints using the expire binding. It is a v1 binding but hopefully there will be similar functionality in OH3. This is used controlling an outdoor light for our Papillon dogs.

Item switches off after 5 minutes. Starting the timer while running resets it.

Switch PapTimer { expire="5m,command=OFF" }

Rules

rule "Papillon Motion On"
when
    Item zooz_papillon_motion changed from OFF to ON
then
    if (IsItDark.state == ON) {
        papillon_light.sendCommand(ON)
        // start Timer
        PapTimer.sendCommand(ON)
    }
end

rule "Papillon Motion Off"
when
    Item PapTimer changed from ON to OFF
then
    papillon_light.sendCommand(OFF)
end

Thanks,but this is still only an on/off rule. I really wnt to dimm it before turn it off.

You could use 2 timer Items then, correct?

The purpose of this user forum is to help you develop your solution, not to provide a complete working solution.

This is a volunteer forum, not a paid HelpDesk.

How to ask a good question / Help Us Help You - Tutorials & Examples - openHAB Community

Okay, but where to insert the second timer? After the last “else”? Sorry I’m very novice.

To do two different things at two different times with expire based timing, you’d need two dummy Items. You would give both a kick at each motion trigger.

If you’re going with the rule-based timer (I would do it that way) this is a fairly key point.

The trick is to use a global variable to hold a handle to your timer. This survives in between runs of rules or different rules, so you can “do stuff” to an existing timer after you first create it.
You’ll find lots of timer examples using this technique. The main elements are creating a var outside of any rule (that is what makes it “global”) and then instead of an anonymous createTimer(), you assign it to that variable.

The general idea is shown under “Simple Example: Timers Version” here

I think I would have it cancel an existing timer at each motion trigger (because you don’t know which of the two steps it might currently be in) and create anew. (and of course set full brightness)

You can still use your previous reschedule idea for the two-stage timing - if it is full bright at execution time, set part bright and reschedule. If it is part bright, set off and finish.

1 Like