Two items on one thing

openHAB version: 2.1

I have a back porch light that I already turn on when my camera detects motion, stays on for a time, and then turns off. That all works fine. Problem is when someone turns on the light manually, the camera detects motion and then after a time the light turns off when you don’t want it to.

So I made a 2nd item on the same thing, wanting to trigger one for auto/motion and one for manual wall switch. This actually operates turning on the light either from camera motion or from the wall switch. But as soon as I make a 2nd item I no longer get reports on the items status when the physical wall switch is flipped. This means I can’t turn off the auto motion turning of the lights off.

Is this normal? When you have two items on one thing, should you expect to see openhab get updates when the thing is switched?

You need to test for the wall switch in your movement rule.
Can you post your items and related rules, please?

I’m not sure what you mean by test for the wall switch. Previous to adding a 2nd item I could see in paper UI the status change when the physical switch was used. Now I don’t see any change.

rule "Back Porch Lights Switch2"
    when Item Porch_Light changed
    then
    	if(Porch_Light.state == ON){
    		sendCommand(Porch_Light_Auto, OFF)
    		logInfo("info", "Manual2 Lights Switched On.")}
    	if(Porch_Light.state == OFF){
    		sendCommand(Porch_Light_Auto, ON)
    		logInfo("info", "Manual2 Lights Switched Off.")}
    end

rule "Back Door Motion"
 when Item BackDoorCamMotion changed
  then
  if(Daylight.state == OFF && Porch_Light_Auto.state == ON && BackDoorCamMotion.state 
== "Motion")
  {logInfo( "File", "Motion was detected at the back door at night.")
  sendCommand(Porch_Light_Motion, ON)
  Thread::sleep(9000)
  if(Porch_Light.state == ON){
  sendCommand(Porch_Light_Motion, OFF)}
}end

Can you show me the item definition for the wall switch, please?

In the mean time I tidied up your rules a bit
I used the postUpdate method for the item Porch_Light_Auto because I assume that is just a flag item with no binding and a postUpdate is sufficient
I used the sendCommand method for the item Porch_Light_Motion
In both cases I have used the method instead of the action as the item is known.
The explanation is here:

I have also used a timer instead of your 9 seconds sleep
Thread::sleep should only be used if unavoidable and only for short periods (1s or less)
The explanation is here:

var Timer timer = null

rule "Back Porch Lights Switch2"
when
    Item Porch_Light changed
then
    if (Porch_Light.state == ON) {
        Porch_Light_Auto.postUpdateOFF)
        logInfo("info", "Manual2 Lights Switched On.")
    }
    if (Porch_Light.state == OFF) {
        Porch_Light_Auto.postUpdate(ON)
        logInfo("info", "Manual2 Lights Switched Off.")
    }
end

rule "Back Door Motion"
when
    Item BackDoorCamMotion changed
then
    if (Daylight.state == OFF && Porch_Light_Auto.state == ON && BackDoorCamMotion.state 
== "Motion") {
        logInfo( "File", "Motion was detected at the back door at night.")
        Porch_Light_Motion.sendCommand(ON)
        if (timer === null) {
            timer = createTimer(now.plusSeconds(9), [ | 
                if(Porch_Light.state == ON) Porch_Light_Motion.sendCommand(OFF)
                timer = null // resets the timer
            ])
        }
    }        
end

I sure appreciate the improved rules, I’ll study these changes to learn how to write them better.

As for the items, I took the basic way and added them via paper UI. Category and type Switch.

OH1

By applying Design Pattern: How to Structure a Rule the first rule can collapse down to

rule "Back Porch Lights Switch2"
when
    Item Porch_Light changed
then
    val newState = if(Porch_Light.state == ON) OFF else ON
    Porch_Light_Auto.postUpdate(newState)
    logInfo("info", "Manual2 Lights Switched " + Porch_Light.state)
end

The second Rule could benefit from this approach too but the improvements are less dramatic.

rule "Back Door Motion"
when
    Item BackDoorCamMotion changed to "Motion" // I think this works now
then
    if(Daylight.state != OFF || Porch_Light_Auto.state != ON) return;

    logInfo("File", "Motion was detected at the back door at night.")
    Porch_Light_Motion.sendCommand(ON)
    if (timer === null) {
        timer = createTimer(now.plusSeconds(9), [ | 
            if(Porch_Light.state == ON) Porch_Light_Motion.sendCommand(OFF)
            timer = null // resets the timer
        ])
    }
end
1 Like

This will still not work as you want described in post #1.
How do you detect the state of the wall switch?

You don’t need 2 items for the light one will do.
What you need is another item for the wall switch.

I thought another item for the wall switch was adding a 2nd item on the Thing. When there was only one, I would detect the wall switch when Porch_Light would change state.

No, your two items are the one and the same, the light…
You need to be able to detect the state of the wall switch independently.

Sounds good, but what type of object am I creating to do that?

You need more than that. You need hardware. A sensor of some type. Zwave, sonoff…

I think I can see the logic here. Provided that OpenHAB can be updated with the actual state of the light:
Item XX can be commanded by rules to turn on the light
Item ZZ can be updated by binding from the light to reflect its actual state.
if ZZ changes without XX being recently commanded, it was a manual event by wallswitch.

I think this is viable, with care. The key things are separation of outgoing commands and incoming state updates, and a means to ‘remember’ a command until the state update takes place (so you can distinguish rule or UI actions from wallswitch actions)

Zwave supports separate in and out, < > bindings right?

I think I can see the logic here. Provided that OpenHAB can be updated with the actual state of the light:
Item XX can be commanded by rules to turn on the light
Item ZZ can be updated by binding from the light to reflect its actual state.
if ZZ changes without XX being recently commanded, it was a manual event by wallswitch.

This is correct. So the crux of the question is how can I detect that ZZ changed? I need to make XX not interfere while it is on.

So you want to not switch the light on when motion is detected if the light is already on? Or not run the motion rule when the light is already on?

I want to not turn the light off by rule if someone physically turned it on. So not running the motion rule seems to be how to accomplish that. If I can’t get two items to operate the same light independently then perhaps I have to check if it’s already on and just trust that it got that way from the wall switch.

Okay, one step at a time. Do you have a means for OpenHAB to detect that the light is on? i.e. what kind of device is involved here, what feedback can you get into OpenHAB? Its still a secret so far.

This is actually not always easy.

First, like rossko57 indicates, your light needs to report its current state when it is triggered outside of OH (e.g. the wall switch). Assuming that is the case, then you can use Design Pattern: Manual Trigger Detection.