[SOLVED] Rule with a delay

With the rule below I’m trying to achieve something that should be quite simple, but I’ve gone blind trying to understand where I’ve gone wrong.

The aim of the rule is to wait 2 seconds from the Hallway motion sensor being activated to see if either the Kitchen motion sensor has been activated or the cloakroom door has been opened. If neither are activated with in the 2 second window, then switch on the Landing light at the right level.

What I’m getting though is the Landing light is switching on straight away as soon as the Hallway motion sensor is active. Can someone point me in the right direction as to where I’ve gone wrong.



var Timer tHallwayTimer = null

rule "Hallway Motion Sensor Activation"
    Item Hallway_SP3102_Motion changed
var dimmingState = -1
if(Porch_ST815_Lux_Level.state < 200) {
    dimmingState = 100
switch Time_Of_Day.state {
    case "EVENING": {
        if(Master_Bedroom_Occupied.state == ON) dimmingState = 65
    case "NIGHT": {
        if(Master_Bedroom_Occupied.state == ON) dimmingState = 25
    case "BED": {
        if(Master_Bedroom_Occupied.state == ON) dimmingState = 15
if(Hallway_SP3102_Motion.state == ON) {
    postUpdate(Hallway_Motion_Sensor_Last_Activation, new DateTimeType())
    logInfo("org.openhab","Motion Sensors: Hallway {}.",Hallway_SP3102_Motion.state)
    if(Living_Room_TV_Power == ON && LivingRoom_SP3102_Motion.state == ON || Kitchen_SP3102_Motion.state == ON) {
        tHallwayTimer = createTimer(now.plusSeconds(2)) [|
            if(CloakRoom_ZW089_Sensor.state == OPEN || Kitchen_SP3102_Motion.state == ON) dimmingState = -1
        if(dimmingState != -1) {
            logInfo("org.openhab","Hallway: Motion detected. Landing light level is " + dimmingState + "%")

Since you are using rules DSL, you can try an expire timer.

I think your own indenting is misleading you here. The createTimer schedules a block of code for later … the block of code is that between the [ ] square brackets.
In this case, that’s the if() .
The code that follows the closing ] is just another part of the main rule body is executed immediately. That’s the sendCommand.

1 Like

I think you need more than one rule here.
A rule much as you have it, that starts a timer off, the timer to shortly do whatever it is - if nothing else happens.
A separate rule can trigger from the “something else”, look to see if the timer is running, and if it is then cancel it.

1 Like

Thanks for the replies, I’ve taken a look at the design pattern topic and will try to work that method into my logic.