Motion detection rule problem

i have a problem with a rule, the light goes off, and then random on again and not off anymore.

rule "Licht Garderobe bei Bewegung Einschalten"
when
    Item Motion_GF_Corridor_Garderobe received update ON
then
       if((flagItemsunset.state == ON && Bewegungsmelder.state == ON) || 
       (Motion_GF_Corridor_Garderobe_illuminance.state <= 60 && Bewegungsmelder.state == ON)) 
       {
		logInfo("FILE", "Setting Light Garderobe to ON")
        Light_GF_Corridor_Garderobe.sendCommand(ON)
		logInfo("FILE", "Garderobe eingeschalten")
        } 
      else {
	    logInfo("File","Bewegung erkannt, aber noch Tag oder zu hell: Nichts zu tun")
	}
end

rule "Licht Garderobe ausschalte"
when
    Item Motion_GF_Corridor_Garderobe_no_occupancy received update 60
then
    if(Bewegungsmelder.state == ON)
       {
		logInfo("FILE", "Setting to OFF Garderobe")
        Light_GF_Corridor_Garderobe.sendCommand(OFF)
        logInfo("File","Licht Garderobe ausgeschalten")
    } 
end

item Bewegungsmelder is a dummy switch, for turning off the motion rule.

I get this in the logs:

2022-09-11 08:05:53.573 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Light_GF_Corridor_Garderobe' received command ON
2022-09-11 08:05:53.573 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Light_GF_Corridor_Garderobe' received command OFF
2022-09-11 08:05:53.573 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'Light_GF_Corridor_Garderobe' predicted to become ON
2022-09-11 08:05:53.573 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'Light_GF_Corridor_Garderobe' predicted to become OFF
2022-09-11 08:05:53.574 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Light_GF_Corridor_Garderobe' changed from ON to OFF
2022-09-11 08:05:53.682 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Light_GF_Corridor_Garderobe' changed from OFF to ON
2022-09-11 08:05:53.731 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Light_GF_Corridor_Garderobe' changed from ON to OFF
==> /var/log/openhab/openhab.log <==
2022-09-11 08:05:53.572 [INFO ] [org.openhab.core.model.script.FILE  ] - Setting Light Garderobe to ON
2022-09-11 08:05:53.572 [INFO ] [org.openhab.core.model.script.FILE  ] - Setting to OFF Garderobe
2022-09-11 08:05:53.572 [INFO ] [org.openhab.core.model.script.File  ] - Licht Garderobe ausgeschalten
2022-09-11 08:05:53.572 [INFO ] [org.openhab.core.model.script.FILE  ] - Garderobe eingeschalten


can someone help me with this topic, I think, there is a problem with the if in the rule, because I get the if and the else feedback in the log at the same time.

if((flagItemsunset.state == ON && Bewegungsmelder.state == ON) || 
       (Motion_GF_Corridor_Garderobe_illuminance.state <= 60 && Bewegungsmelder.state == ON)) 
       {

if sunset and motion are both on it will always send on or if motion garderobe lux is les or equal to 60 and motion is on.

maybe what you want is

if((flagItemsunset.state == ON && Bewegungsmelder.state == ON) || 
       (Motion_GF_Corridor_Garderobe_illuminance.state < 60 && Bewegungsmelder.state == ON)) 
       {

so that the other rule that triggers at value 60 doesn’t conflict with this one

do you thing its also good to add this:

		if((flagItemsunset.state == ON && Bewegungsmelder.state == ON && Light_GF_Corridor_Garderobe.state == OFF) || 
		(Motion_GF_Corridor_Garderobe_illuminance.state < 60 && Bewegungsmelder.state == ON && Light_GF_Corridor_Garderobe.state == OFF)) 
       {

&& Light_GF_Corridor_Garderobe.state == OFF

I still geht also the if and else message in the log:

2022-09-11 08:50:30.407 [INFO ] [org.openhab.core.model.script.FILE  ] - Setting Light Garderobe to ON

2022-09-11 08:50:30.408 [INFO ] [org.openhab.core.model.script.FILE  ] - Garderobe eingeschalten

2022-09-11 08:50:30.451 [INFO ] [org.openhab.core.model.script.File  ] - Bewegung erkannt, aber noch Tag oder zu hell: Nichts zu tun

the Motion_GF_Corridor_Garderobe_illuminance.state is under 60, but I get the else message and the flagItemsunset is off, so it should not turn on the light

Make sure that all the values that are used in your rule are also printed out to the log file done by the rule itself. In this way you will see all the current values that are used in the evaluations of your rules.
Looks like there are two runs through your rule ? First one turns the light on; second one the light is on and it is not dark enough any longer because of the light that was turned on and the light will be turned off again.
Put a debug/log statement before the if statement so that you will see if the rule is being entered more than one time.

well explain what you want to happen.
so you have a garderobe light you want that light to turn on when lux is below 60 and when is sunset and when motion is on. assuming your motion item declares it’s state as on or off and assuming the sunset item declares it’s state as on or off and the lux level is declared as a number without anything else

Is that correct ?

that’s correct, I have the lux if it is dark in the room, the motion item is a off switch for me, and the sunset is also a switch item with on and off, another rule use the status from a weather binding and turns the switch on and off.

I would like to use this rule on all lights, but now its nor working great.

and I have added && Light_GF_Corridor_Garderobe.state == OFF, so it should only start one time the rule

2022-09-11 10:03:59.183 [INFO ] [org.openhab.core.model.script.FILE  ] - Garderobe rule is startig

2022-09-11 10:03:59.183 [INFO ] [org.openhab.core.model.script.FILE  ] - Setting to OFF Garderobe

2022-09-11 10:03:59.184 [INFO ] [org.openhab.core.model.script.File  ] - Licht Garderobe ausgeschalten

2022-09-11 10:03:59.184 [INFO ] [org.openhab.core.model.script.File  ] - Bewegung erkannt, aber noch Tag oder zu hell: Nichts zu tun
2022-09-11 10:04:06.600 [INFO ] [org.openhab.core.model.script.FILE  ] - Garderobe rule is startig

2022-09-11 10:04:06.601 [INFO ] [org.openhab.core.model.script.FILE  ] - Setting Light Garderobe to ON

2022-09-11 10:04:06.601 [INFO ] [org.openhab.core.model.script.FILE  ] - Garderobe eingeschalten

2022-09-11 10:04:06.645 [INFO ] [org.openhab.core.model.script.FILE  ] - Garderobe rule is startig

2022-09-11 10:04:06.646 [INFO ] [org.openhab.core.model.script.File  ] - Bewegung erkannt, aber noch Tag oder zu hell: Nichts zu tun

the rule is sometimes starting 2 times

That Items are a strange mix.
What’s the function of Bewegungsmelder? Is it some sort of enable Switch?
What’s Motion_GF_Corridor_Garderobe_no_occupancy?

A more common rule would be to switch light on when motion is detected, at the same time start a timer which will switch the light off.

Bewegungsmelder is a on off switch for me, a dummy switch.
Motion_GF_Corridor_Garderobe_no_occupancy is a value from zigbee2mqtt, if there is no occupancy since, this is for me the timer, because when the motion sensor detects an motion in a specified time, it will not sent this no occupancy, its like a monitoring whether anyone else is in the room, you can setup the seconds in zigged2mqtt, if there is 60 seconds no motion detection in the room, I get the no occupancy value

i had the timer bevor with a value, but often the light goes off when I am longer in the room, and than on again, with the new rule, I would like that the light stays on until I moved

ok let’s start from the begining
when do you want to trigger that rule only when Motion_GF_Corridor_Garderobe is ON ?
if yes
ok let’s go further then all this has to be like this to do something

if(flagItemsunset.state == ON && Bewegungsmelder.state == ON) && Motion_GF_Corridor_Garderobe_illuminance.state < 60) 
       

then we want to send on to the light

else if that doesn’t get true then let’s

else if(flagItemsunset.state == OFF && Bewegungsmelder.state == OFF) || Motion_GF_Corridor_Garderobe_illuminance.state > 60) 
       

then we want to turn off the light

So what’s the real difference between Motion_GF_Corridor_Garderobe and Motion_GF_Corridor_Garderobe_no_occupancy? What’s the state of Motion_GF_Corridor_Garderobe_no_occupancy when motion is detected?

Maybe you made a mistake when implementing…
Simple rule:

rule "Licht Garderobe bei Bewegung Einschalten"
when
    Item Motion_GF_Corridor_Garderobe received update ON                                    // Bewegung erkannt
then
    if(Bewegungsmelder.state != ON) {                                                       // Automatik aus?
        logInfo("motion","Bewegung im Flur erkannt, aber Automatik deaktiviert.")
        return;                                                                             // dann Abbruch
    }
    if(flagItemsunset.state != ON && Motion_GF_Corridor_Garderobe_illuminance.state > 60) { // Zu hell?
        logInfo("motion","Bewegung im Flur erkannt, aber zu hell.")
        return;                                                                             // dannn Abbruch
    }
    logInfo("motion", "Schalte Garderobenlicht ein")
    Light_GF_Corridor_Garderobe.sendCommand(ON)                                             // Einschaltimpuls senden
end

And set the Item Light_GF_Corridor_Garderobe to use an expiration Timer. If you want to use a permanent switch in addition, use a proxy Item for the expiration.
The point is: restart the expiration time each Time motion is detected.

Motion_GF_Corridor_Garderobe = Motion Detection
Motion_GF_Corridor_Garderobe_no_occupancy = send a value (60) after 60sek only when no motion was detected in that time of 60sek

no_occupancy_since

Sends a message the last time occupancy (occupancy: true) was detected. When setting this for example to [10, 60] a {"no_occupancy_since": 10} will be send after 10 seconds and a {"no_occupancy_since": 60} after 60 seconds.

So the real problem is, detected motion is not the same as occupancy.
You can’t use occupancy to switch off the light - at least, you would have to ensure that not only motion is detected but also occupancy.
Still valid… Use a timer but ensure that every motion detection is used tu restart the timer.

ok, this was my old rule that is working:

var Timer timergardero = null
rule "Licht Garderobe bei Bewegung Einschalten"
when
    Item Motion_GF_Corridor_Garderobe received update ON
then
       if((timergardero === null && flagItemsunset.state == ON && Bewegungsmelder.state == ON) || 
       //(timergardero === null && Light_Sensor_Outdoor.state <= 60 && Bewegungsmelder.state == ON) || 
       (timergardero === null && Motion_GF_Corridor_Garderobe_illuminance.state <= 60 && Bewegungsmelder.state == ON))  {
        logInfo("FILE", "Setting to ON and creating timer")
        Light_GF_Corridor_Garderobe.sendCommand(ON)
        timergardero = createTimer(now.plusMinutes(2), [|
            logInfo("FILE", "Timer expired and setting to OFF")
            Motion_GF_Corridor_Garderobe.postUpdate(OFF)
            Light_GF_Corridor_Garderobe.sendCommand(OFF)
            timergardero = null
        ])
    } 
      else if (timergardero !== null) {
        logInfo("FILE", "Timer rescheduled")
        timergardero.reschedule(now.plusMinutes(5))  
    }
      else {
	    logInfo("File","Bewegung erkannt, aber noch Tag oder zu hell: Nichts zu tun")
	}
end

Well, in theory, the rule should work without switch off. Boolean logic could be slightly better, though:

var Timer timergardero = null

rule "Licht Garderobe bei Bewegung Einschalten"
when
    Item Motion_GF_Corridor_Garderobe received update ON
then
    if(timergardero !== null) {
        logInfo("motion", "Timer für Garderobe verlängert")
        timergardero.reschedule(now.plusMinutes(5))
    } else if(Bewegungsmelder.state == ON && (flagItemsunset.state == ON || Motion_GF_Corridor_Garderobe_illuminance.state <= 60)) { 	// || Light_Sensor_Outdoor.state <= 60)) {
        logInfo("motion", "Garderobe einschalten und Timer erzeugen")
        Light_GF_Corridor_Garderobe.sendCommand(ON)
        timergardero = createTimer(now.plusMinutes(2), [|
            logInfo("motion", "Timer Abgelaufen, Abschalten")
         // Motion_GF_Corridor_Garderobe.postUpdate(OFF)
            Light_GF_Corridor_Garderobe.sendCommand(OFF)
            timergardero = null
        ])
    } else {
        logInfo("motion","Bewegung erkannt, aber noch Tag oder zu hell: Nichts zu tun")
    }
end

Instead of testing and retesting over and over again, keep it simple. First option (while at first start of the rule always false) is to check wether timer var is not null (because this is the difference to the other path with multiple conditions).
Now, for the second path, you will not need to check wether the var is null, you know it is, otherwise the first option would have been true.
The condition Bewegungsmelder.state == ON must be met for all other conditions, so you can simply add this with a boolean AND. In addition, there are two (or three) options from which one must be true, so it’s an boolean OR (condition1 OR condition2 OR condition3 is true). The brackets are needed to let openHAB know what part is the AND for (first do the OR for the last three conditions, then take the result and do the AND with the state == ON).
The result should be the same, but it’s more elegant and even more readable.

Did you set the very first line

var Timer timergardero = null

on top of the rules file? Global Variables must be defined on top of the file, ahead of any rule.

2 Likes