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
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 ??
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?
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.