Two items on one thing

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