Delay in rule

but isnt that triggered when the state changes to open.

how will that fire when the state remains open after the initial timer expires?

the initial timer trigger and reschedule itself, try it out please and look at the loginfos…

will do. thanks again for all your help.

if the timer once is started it runs outside the rule. if “expired” the timer look how the state is, if it is open it reschedule itself and again …

that makes a lot more sense. Sorry am not too familiar with programming rules yet.

so I have been using your rule for the last two days and it has been working really well.

I made some modications to it, mainly to turn the lights on even when the timer is running but someone manually turned the lights off before the timer ran out. Also I wanted to send the command to turn the switch on or off only if they were in the other state but for some reason the code is not working.

light do not turn on when motion is detected. here is the modified code.

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer

var Timer kitchen_timer = null
var Integer kitchen_timeout = 4

rule “Kitchen light motion”
when
Item Kitchen_Movement changed to OPEN
then
if (Kitchen_Lux.state < 30){
if (kitchen_timer != null) && (WallSwitch_Kitchen.state == ON )
{
kitchen_timer.reschedule(now.plusMinutes(kitchen_timeout))
}
else if (kitchen_timer == null) && (WallSwitch_Kitchen.state == OFF )
{
sendCommand(WallSwitch_Kitchen, ON)
kitchen_timer = createTimer(now.plusMinutes(kitchen_timeout))
[|
if (Kitchen_Movement.state == OPEN)
{
kitchen_timer.reschedule(now.plusMinutes(kitchen_timeout))

        }
        else if (WallSwitch_Kitchen.state == ON)
        {
            sendCommand(WallSwitch_Kitchen,OFF)
            kitchen_timer = null
        }
    ]
}
else if (kitchen_timer != null) && (WallSwitch_Kitchen.state == OFF  )
{
kitchen_timer.reschedule(now.plusMinutes(kitchen_timeout))
sendCommand(WallSwitch_Kitchen,ON)
}

}

Interesting use of the state test inside the [ |…] clause. Might work OK. I prefer to separate timer-triggered actions by using PROXY elements for switches and intercepting the command to the PROXY at timer execution time. Puts all the run-time parts “together” for easier testing/maintenance. YMMV

see Automation Design Patterns

thanks @bob_dickenson the code I pasted above was not working. going to try your suggestions now.

Did you find a solution to turn the light on and of manually ? If yes please post your example, i try to do the same thing .

Grtz

Wolf

This is working well for me right now.

var Timer kitchen_timer = null
var Integer kitchen_timeout = 4
rule "Kitchen light motion"
when
Item Motion_GFKitchen changed to OPEN
then
if (Lumin_GFKitchen.state < 30 ){
    if (kitchen_timer != null)
    {
        kitchen_timer.reschedule(now.plusMinutes(kitchen_timeout))
        logInfo("Kitchen","Kitchen timer rescheduled for " + kitchen_timeout + " minutes")
    }
    else
    {
        sendCommand(WallSwitch_Kitchen, ON)
        logInfo("Kitchen","Kitchen timer create with " + kitchen_timeout + " minutes")
        kitchen_timer = createTimer(now.plusMinutes(kitchen_timeout))
        [|
            if (Motion_GFKitchen.state ==  OPEN)
            {
                kitchen_timer.reschedule(now.plusMinutes(kitchen_timeout))
                logInfo("Kitchen","Kitchen timer triggered, but rescheduled again for " + kitchen_timeout + " minut$
            }
            else
            {
                sendCommand(WallSwitch_Kitchen,OFF)
                kitchen_timer = null
            }
        ]
    }
}
end



hope it helps.

This is interesting. I’d like to know if support of conditionals (e.g. if (Motion_GFKitchen.state…)) INSIDE the [|…] closure is explicitly designed or an accidental side-effect. @kai , could you chime in here ??

edit: just realized what you were asking @bob_dickenson
Not sure if they designed this on purpose but helps reduce the amount of code significantly.

no its done on purpose. not sure but I developed it using someones example a year or so ago when I was a lot more active with the project.

Works out really well for me. Cannot wait to start on more projects this upcoming summer.

Dears , thank you for allready answering but i do not see how this rule is making me able to turn on the switch on and off and cancel the motion detection ?
if i read the rule above then i see a motion detection rule with a timer that checks the status of the lightswitch.

what i want to do is the following.

when i enter the room i want the motion detector to turn of the light and turn it off if no motion is detected for 4 min.
But when i want to work at my desk the light needs to stay on so want to be able to push the wallswitch so the light stays on till i turn it off manually again.
when turned off manually i want the motiondetector to take over again.

the rule above i allready had installed just todo motion detection and indeed it works well but it does not allow me to turn on and off the light manually.

kindest regards

items are a fibaro motion detector and a dual relay from fibaro.

Well, a closure is a closure, so you can put any kind of complex logic in there and this is design on porpose, yes.
Not sure, if I fully grasped your question?

All you need to do is add another condition. Create a new switch in openhab such a “Manual override”.

If manual override is “ON” then the above loop is inactive. if it is “OFF” then above loop is active.

Does that help?

thats the whole problem , the overide should be activated or deactivated by the same switch which is turned on by the motion sensor.
believe me , i allready tried but did not succeed.

Is the switch nearby the sensor. If not you can make a condition where the
timer is only activated if light turns on when motion is active. If not thr
timer stays dormant.

They are in the same room.
The problem lies that when the motion sensor will always detect first when entering the room.
So when the motion sensor turns on the light it will set the state of the relay to on, which means that flipping the switch again has no use.
Ideal would be when i enter the room and the motion detector turned on the light i can flip the switch so they stay on and i can flip the switch again to resume motion detection.

Grtz

Wolf

That is doable as well.

If switch state change and motion is active disregard the timer else
continue with timer.

i openend another new topic motion lights + manual switch
maybe we can continue our discussion there bease don the code there.

thnx