Motion Lights + manual switch

no no its allready setup like that , what i want to do is when the motion detector is triggered and sends the command ON to the proxy it should also check the lux value of the motion sensor for a value so it does not turn on the light when the lux is higher then 18.
if there is enough light in the room there is no need to turn on the light.
i tought making something like this.

rule "Motion Triggered"
when
    Item Z_Bureau_Motion changed from CLOSED to OPEN 
    
then
	if 
	((Z_Bureau_Licht.state == OFF)&&((Z_Bureau_Lux.state as DecimalType) < 18))
	// motion sensor is sending this command
    whoCalled = "motion"
    // Turn on the light
   	Z_Bureau_Licht_PROXY.sendCommand(ON)

    // Set/Reset timer for 2 minutes to turn off the light
    if(motionTimer != null && !motionTimer.hasTerminated){
        motionTimer.reschedule(now.plusMinutes(2))
    }
    else {
        motionTimer = createTimer (now.plusMinutes(2)) [|
            Z_Bureau_Licht_PROXY.sendCommand(OFF)
            motionTimer = null
           ]
    }
end

ok so i modified it like this and it seems to be working.
i’ll have to test now if flipping the switch works but sseing in the logs and your comment from before is should work.
state and item of the switch are indeed logged as different items.
let’s pray :slight_smile:


rule "Motion Triggered"
when
    Item Z_Bureau_Motion changed from CLOSED to OPEN 
    
then
	if ((Z_Bureau_Lux.state as DecimalType) < 18) {
	logInfo("BureauLightOn","Motion detected! Measuring Lux state below 19 lux or not,current Luminance is " + Z_Bureau_Lux.state)
	}
	// motion sensor is sending this command
    whoCalled = "motion"
    // Turn on the light
    
   	Z_Bureau_Licht_PROXY.sendCommand(ON)
	// Set/Reset timer for 2 minutes to turn off the light
    if(motionTimer != null && !motionTimer.hasTerminated){
        motionTimer.reschedule(now.plusMinutes(2))
    }
    else {
        motionTimer = createTimer (now.plusMinutes(2)) [|
            Z_Bureau_Licht_PROXY.sendCommand(OFF)
            motionTimer = null
           ]
    }
end

i had to revert the change cause the lux statement caused the light to not turn on anymore :frowning:
any suggestion ?

Hi @rlkoshak,
it’s always very, very helpful to read your rules and even better, get some additional explanation to understand the logic behind it.
Thanks a lot for taking so much time to support us!

also just noted that when flipping the switch its listed like this in the log

Z_Bureau_Licht_all state updated to ON

because this is a dual realy i think another item is called

If your dual relay is a Fibaro FGS-222 you should read this post:

i checked the topic but mine is doing fine, the state is updated correctly.
when i tested the rule above it contains the value for Switch 1 from the dual relay to define how to turn on the light.
based on a override set to false or true the motion detector takes charge or the manual switch.
in my case when using the manual switch is is not Switch 1 that activates but Switch All
wich i think is good as it can be seen a third item that can be defined as beeing the manual switch.
i reverted back to normal motion detection as the rule i tried did flip on the light but after several on/off it stops and no longer updates anything but the proxy so something fails.

Theres still something not right with the logic here, or I don’t have a copy of your entire working rule. I am trying to do the exat same thing. I have a sensor that I want to detet motion and turn on a light in my kitchen, however if I want light on permanently, I want to turn on the switch (happy to turn it off and then on to get it working) and the sensor is disabled.

I have copied the code, however I need to walk past the sensor to get to the switch, so the rule applies and turns on the light based on “motion”. I then turn off the switch at the wall which detects “manual” I then turn it on again and the rule sees “manual”, however when I move again the motion sensor detects this, the rule fires and we are running on “motion” again and the timer kicks in

I think the logic is wrong where the motion change sets the whoCalled but not sure how to fix (yes I have lux set to 99 for testing)

rule "Motion Triggered"
when
    Item Zipato1_Kitchen_Motion changed from CLOSED to OPEN 
    
then
	if ((Zipato1_Kitchen_Lux.state as DecimalType) < 99) {
	logInfo("Zipato1_Kitchen_LightOn","Motion detected! Measuring Lux state below 19 lux or not,current Luminance is " + Zipato1_Kitchen_Lux.state)
	}
	// motion sensor is sending this command
    whoCalled = "motion"
		logInfo("Zipato1_Kitchen_LightOn","Running on Motion ")
    // Turn on the light

1 Like

My full rule file:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer
import java.util.Date 
import java.util.format

var Timer motionTimer = null
var boolean override = false
var String whoCalled = "manual"

rule "Proxy Commanded"
when
Item Zipato1_Kitchen_LightProxy received command
then
// Forward ON commands always
if(Zipato1_Kitchen_LightProxy.state == ON && Fibaro_Kitchen1_Light.state != ON) Fibaro_Kitchen1_Light.sendCommand(ON)

// Only send off if not overridden
else if(!override && Fibaro_Kitchen1_Light  != OFF) Fibaro_Kitchen1_Light.sendCommand(OFF)
end


rule "Motion Triggered"
when
    Item Zipato1_Kitchen_Motion changed from CLOSED to OPEN 
    
then
	if ((Zipato1_Kitchen_Lux.state as DecimalType) < 99) {
	logInfo("Zipato1_Kitchen_LightOn","Motion detected! Measuring Lux state below 19 lux or not,current Luminance is " + Zipato1_Kitchen_Lux.state)
	}
	// motion sensor is sending this command
    whoCalled = "motion"
		logInfo("Zipato1_Kitchen_LightOn","Running on Motion ")
    // Turn on the light
    
   	Zipato1_Kitchen_LightProxy.sendCommand(ON)
	// Set/Reset timer for 2 minutes to turn off the light
    if(motionTimer != null && !motionTimer.hasTerminated){
        motionTimer.reschedule(now.plusMinutes(2))
    }
    else {
        motionTimer = createTimer (now.plusMinutes(2)) [|
            Zipato1_Kitchen_LightProxy.sendCommand(OFF)
            motionTimer = null
           ]
    }
end

rule "Light received update"
when
Item Fibaro_Kitchen1_Light received update
then
// Called whenever the Light's state is updated, its purpose is to tell the difference between a Motion triggered 
// event and a manual event and set the override accordingly.

// Light was turned ON and whoCalled is "manual", light is overridden
if(Fibaro_Kitchen1_Light.state == ON && whoCalled == "manual") override = true

// Light was turned OFF, reset override
else override = false

// reset whoCalled back to manual
whoCalled = "manual"
logInfo("Zipato1_Kitchen_LightOn","Running on switch ")

// update the Proxy in case the light was changed at the switch, use postUpdate so rules don't trigger
if(Fibaro_Kitchen1_Light.state != Zipato1_Kitchen_LightProxy.state) Zipato1_Kitchen_LightProxy.postUpdate(Fibaro_Kitchen1_Light.state)
end


Your thinking is correct, i still have no solution for this yet.
I am i the exact same position when i enter the room the light is switched on, flipping the manual switch does nothing here. Thats why i reverted.

anyone found a solution yet ?
i’m sure allot of people will have met this topic and have a similar situation at home.
i found maybe a solution as i applied a dual relay from fibaro.
Here i noticed that when the motion sensor fires the rule only S1 is activated, but as this is a dual one it has a S2 and S ALL.
When pressing the button i see that SALL is activated so maybe this opens a window of oppertunity.
i will test this as soon as possible

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer
import java.util.Date
import java.util.format

var Timer Bureau_timer = null

rule "Basement Motion- Lights On"
when
Item Z_Bureau_Motion changed from CLOSED to OPEN
then
if(Z_Bureau_Light_PROXY.state == ON) && (Z_Bureau_Lux.state < (18)) {
if(Z_Bureau_Light_all.state == OFF) {
sendCommand(Z_Bureau_Light, ON)
}
}
end

rule "Basement Motion- Lights Off"
when   
        Item Z_Bureau_Motion changed from OPEN to CLOSED
then 
        if(Bureau_timer!=null) {
            Bureau_timer.cancel
            Bureau_timer = null
        }
        if(Z_Bureau_Light_PROXY.state == ON) {
        if(Z_Bureau_Light_all.state == OFF) {
        Bureau_timer = createTimer(now.plusMinutes(3)) [|
        sendCommand(Z_Bureau_Light, OFF)
        ]
        }
	} 
end

rule "Basement Motion Override"
when 
        Item Z_Bureau_Light_all changed from OFF to ON
then

        sendCommand(Z_Bureau_Light_PROXY, OFF)
		postupdate(Z_Bureau_Light, ON)
end

rule "Cancel Basement Motion Override"
when 
        Item Z_Bureau_Light_all changed from ON to OFF 
then

        sendCommand(Z_Bureau_Light_PROXY, ON)
		postupdate ( Z_Bureau_Light, OFF)
end

Ok guys,

I have tested my rule out for a few days and its working beautifully.
The key here was that my fibaro dual relay has another item that can be spoken to that is only reacting when switching the light at the actual switch.
so the motion sensor turns on the light by switching S1
by using the proxy method i then configured that when pressing the switch will turn off the proxy which prevents motion detection and activates SALL.
so now i am able to set my light on forever untill i turn it off again manually without beeing interupted by the motion sensor. :slight_smile:

1 Like

I tried your code but it does not seem to work, am I doing something wrong ?

(I have 2 motion sensors in the same room to cover every corner of the room.

I am using Xiaomi Mu Motion Sensors

my .rules file:

var Timer motionTimer = null
var boolean override = false
var String whoCalled = "manual"

rule "Proxy Commanded"
when
    Item WohnzimmerDeckenlicht_1_Proxy received command
then
    // Forward ON commands always
    if(WohnzimmerDeckenlicht_1_Proxy.state == ON && WohnzimmerLampeDecke.state != ON) WohnzimmerLampeDecke.sendCommand(ON)

    // Only send off if not overridden
    else if(!override && WohnzimmerLampeDecke.state != OFF) WohnzimmerLampeDecke.sendCommand(OFF)
end

rule "Motion Triggered"
when
    Item XiaomiMiMotionSensor1_MotionStatus received command or Item XiaomiMiMotionSensor2_MotionStatus received command
then
    // motion sensor is sending this command
    whoCalled = "motion"
   
    // Turn on the light
    WohnzimmerDeckenlicht_1_Proxy.sendCommand(ON)

    // Set/Reset timer for four minutes to turn off the light
    if(motionTimer != null || !motionTimer.hasTerminated) {
        motionTimer.reschedule(now.plusMinutes(5))
    }
    else {
        motionTimer = createTimer(now.plusMinutes(5), [|
            WohnzimmerDeckenlicht_1_Proxy.sendCommand(OFF)
            motionTimer = null
        ])
    }
end

rule "Light received update"
when
    Item WohnzimmerLampeDecke received update
then
    // Called whenever the Light's state is updated, its purpose is to tell the difference between a Motion triggered 
    // event and a manual event and set the override accordingly.

    // Light was turned ON and whoCalled is "manual", light is overridden
    if(WohnzimmerLampeDecke.state == ON && whoCalled == "manual") override = true
 
    // Light was turned OFF, reset override
    else override = false

    // reset whoCalled back to manual
    whoCalled = "manual"

    // update the Proxy in case the light was changed at the switch, use postUpdate so rules don't trigger
    if(WohnzimmerLampeDecke.state != WohnzimmerDeckenlicht_1_Proxy.state) WohnzimmerDeckenlicht_1_Proxy.postUpdate(WohnzimmerLampeDecke.state)

end

My item file:

Switch XiaomiMiMotionSensor1_MotionStatus  { channel="mihome:sensor_motion:158d0001d8e731:motion" }
Switch XiaomiMiMotionSensor2_MotionStatus  { channel="mihome:sensor_motion:158d0001d6668e:motion" }

Switch WohnzimmerLampeDecke "Deckenlicht" (gLight) [ "Lighting" ] { channel="homematic:HM-LC-Sw1PBU-FM:ccu2bridge:OEQ0624006:1#STATE" }
Switch WohnzimmerLampeDecke_1_Proxy  { channel="homematic:HM-LC-Sw1PBU-FM:ccu2bridge:OEQ0624006:1#STATE" }

The log shows this:

2018-01-25 20:55:25.989 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'motiondetector.rules'

2018-01-25 20:55:27.763 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'motiondetector.rules', using it anyway:

The operator '!=' should be replaced by '!==' when null is one of the arguments.

2018-01-25 20:55:27.772 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'motiondetector.rules'

Even when the motion sensors are triggered and change to ON, nothing happens regarding the rule, I think somehow it does not get triggered
I can not locate my mistake, i need help.

regards

Can you confirm that the motion sensor is indeed changing to ON in events.log?

Do oyu know whether or not the Xiaomi receives a command or an update? If it doesn’t receive a command the rule won’t trigger.

Reading your logs you should first fix that i read you are missing a “=” and

In question to above this does not work with a normal 1 switch, you need a double to use my code.
You need to be able to define the manual state of the switch wich is done by the switch all.
The motion sensor just triggers switch one.
Like rlkoshak said 2 devices.
So when there is motion it will turn on switch 1 which gives light and checks on the status of the switch all.
If i manualy push the switch the switch all item is on + switch1 which states me to define i manually switched the light.
I read you could also solve this with a proxy item but i never succeeded on that part.

Kindest regards

This is how it looks like in the events.log:

2018-01-25 22:09:13.189 [vent.ItemStateChangedEvent] - XiaomiMiMotionSensor2_MotionStatus changed from OFF to ON

I did not completly understand how you do it. If I may ask could you maybe change your code example with my items etc. so I could try your way?

All you see is changed? You should see an update or a command before that line in the logs. That is the important line.

I’m afraid looking at your items you use a single switch and not a double like i said in explenation above.
Seems to me your proxy and switch are the same device and link ?
The intention is that a proxy is a different assignment / device.
Please read the story carefully and try to understand how you want to make it work without conflicts.