Motion Lights + manual switch

Dear members.

i have the following situation.
I want to be able to enter the room and see the lights go on by motion detection and off again after 4 min.
Also i want to be able to enter the room and spent some time there so i must be able to manually switch the lights on so they stay on.
The problem i have is that motiondetection is going well but i’m unable to find a logic to say if i manually switched ignore the motion detector till i manually turn of the light.

items used is a fibaro motion detector and a fibaro dual relay switch, can anyone help ?

Take a look here:

Thnx but that onlybworks when having zwave bulbs as in that example the light and switch are seperated. In my case the motion detector and wall switch do the same.

Kindest regards

Upps, sorry, didn’t scroll down the page to see that you already had tried that …

What do you mean by that? Do you have association groups set?
Maybe you should post your items and (also non working) rules to help you better …

You have one Item that represents the Motion detector. You have another Item that represents the light switch.

You need to be able to distinguish between the Motion detector setting the light and manually setting the light switch. If you do not have separate Items in OH to represent each you cannot do what you want. OH has no way to distinguish between the two.

Assuming that you do have separate Items, when you manually flip the light switch, does its new state get reported to OH? If not you can not do what you want.

I do have 2 different items .
Z_bureau_motion and Z_bureau.
I have allready had a rule based on the lux and motion value will turn on the light switch.
I tried working with a proxy but i failed as i saw that when the motion sensor updates the proxy i am missing another item for the switch, if i use only one. It would be much easier for example if i had a zwave lamp or extra sensor.
Anyone an idea on how to bring logic in a rule for that ?

If you have two separate items in OH then as far as OH is concerned they are different devices. Having two separate devices wouldn’t solve your problem.

From the Proxy example:

#Items

Contact Motion
Switch Light
Switch LightProxy

#Rules

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

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

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

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

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

rule "Light received update"
when
    Item 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(Light.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(Light.state != LightProxy.state) LightProxy.postUpdate(Light.state)

end

Theory of operation:

If the Light receives an ON update and whoCalled is “manual” the override flag will be set. If whoCalled is not “manual” override remains unset.

If Motion is triggered, it will turn ON the Light through the proxy, however, it will set whoCalled to “motion” so when the Light updated rule triggers the light does not get overridden.

When the light is overridden, it cannot be turned off except by physically toggling the switch, effectively disabling the motion detector.

Once the light is turned off it will rest the override.

NOTES:

  • I just typed in the above, there are likely typos and errors
  • This rule will not work if when you manually switch the light switch the new state is not sent to OH as an update to the Light Item.

Edit: Forgot to reset whoCalled at the bottom of the Light received update rule

3 Likes

Supper i am going to try this and see if it works. I never knew / saw this kind of solution. Defining what item called is brilliant :wink:

Thank you so much !

What do you mean it will not work with the wall switch in your notes ? Cause this is what i need to be able to ?

This is basically a very simplified version of the lighting rules posted to that same proxy thread @sihui pointed you to.

When you switch the wall switch manually, does the Light Item in openHAB change to reflect the new state independent of what the Motion Item does?

If so this rule will work.

If not there is NO solution that will work. The only way to override the Motion detector through rules is if you get two different signals, one from the motion detector and one from the light switch.

if i flip the wall switch i see that in my ui the switch moves to the on status , in the log is see it recieves command ON.
i assume that’s what i need ?
can you also clarify what you forgot in the end as i allready see a reset of whocalled ?

kindest regards and allready many thanks for your help

this is my current rule now

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 Z_Bureau_Licht_PROXY received command
then
// Forward ON commands always
if(Z_Bureau_Licht_PROXY.state == ON && Z_Bureau_Licht.state != ON) Z_Bureau_Licht.sendCommand(ON)

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

end

rule "Motion Triggered"
when
Item Z_Bureau_Motion received command
then
// motion sensor is sending this command
whoCalled = “motion”
// Turn on the light
Z_Bureau_Licht_PROXY.sendCommand(ON)

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

end

rule "Light received update"
when
Item Z_Bureau_Licht 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(Z_Bureau_Licht.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(Z_Bureau_Licht.state != Z_Bureau_Licht_PROXY.state) Z_Bureau_Licht_PROXY.postUpdate(Z_Bureau_Licht.state)

end

That is just a note to indicate what I changed when I edited the already posted message. Usually the posting is tagged with an icon indicating that it has been edited after it was posted and it is good form to let future readers know what changed.

Please use code formatting. There are lots of ways you can do that but the easiest is to wrap your code in backticks:

```
all your code goes here
```

As it is posted it is all but impossible to follow easily.

Till now its not working :frowning:
In my event log i see the proxy changing when entering the room, so on motion the proxy goes to off ?
I will have to have a look why cause the code seemed good.

I think i found abug in the code at the motion detector .
a motion detector can never recieve commands only send or change status.
so i altered like this.

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 Z_Bureau_Licht_PROXY received command
then
// Forward ON commands always
if(Z_Bureau_Licht_PROXY.state == ON && Z_Bureau_Licht.state != ON) Z_Bureau_Licht.sendCommand(ON)

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

rule "Motion Triggered"
when
Item Z_Bureau_Motion changed from closed to open or
Item Z_Bureau_Motion changed from open to closed
then
// motion sensor is sending this command
whoCalled = "motion"
// Turn on the light
Z_Bureau_Licht_PROXY.sendCommand(ON)

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

rule "Light received update"
when
Item Z_Bureau_Licht 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(Z_Bureau_Licht.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(Z_Bureau_Licht.state != Z_Bureau_Licht_PROXY.state) Z_Bureau_Licht_PROXY.postUpdate(Z_Bureau_Licht.state)
end

checking the logs i see this now

Error during the execution of rule ‘Motion Triggered’: cannot invoke method public abstract boolean org.openhab.model.script.actions.Timer.hasTerminated() on null

That should be an and (i.e. &&) in the if statement:

if(motionTimer != null && !motionTimer.hasTerminated) {

can you also tell me where to imply the lux state ?

like ((Z_Bureau_Lux.state as DecimalType) < 18)) ?
the motion detector is allready turning on the light now so its doing something,
i’ll check the logs with your improvement for the timer.

kindest regards

I don’t understand the question. If you are asking how to set up an Item to get the lux state, I’ve not a clue. It depends on your specific hardware device.