Unexpected behavior - what have I done wrong?

I’m about to embark on my journey from OH2.5 to OH3 and have decided I need to tidy up a few of my rules. Pretty much all of my rules have been created by searching the forum to find how to make it work as expected. The rule below is one of those rules that I never really progressed beyond my original idea.

The idea behind the rule is that when someone leaves the house a timer kicks in for 180 seconds and switches off the Kitchen Sonos, but only if there is no Motion detected.

In reality the Sonos is switched off every time a door is closed as someone leaves the house. I think I’ve misunderstood how timers work and just need a little nudge in the right direction.

rule "We've all left the house"
    Item gDoor_Sensors_GroundFloor changed to CLOSED
if(Kitchen_Sonos_State.state == "PLAYING" && Time_Of_Day.state == "DAY") {
    myTimer = createTimer(now.plusSeconds(180)) [|
        if(myTimer !== null || gMotion_Sensors_GroundFloor.state == OFF) {
            if(gDoor_Sensors_GroundFloor.state == CLOSED && gMotion_Sensors_GroundFloor.state == OFF) {
                logInfo(filename, "System Message: The Kitchen Sonos has been switched Off.")
                sendPushoverMessage(pushoverBuilder("System Message: The Kitchen Sonos has been switched Off.").withPriority(1))
    myTimer = null

It’s probably not a problem with the Timers but perhaps a misunderstanding on how your motion sensors work.

Typically a Motion sensor will set a Switch or Contact Item to ON/OPEN temporarily. Often only for a matter of a couple seconds. So your if statement will only return false if motion was detected within a couple of seconds of the Timer running. If there was motion 30 seconds before the 180 seconds are up, the Motion sensor Items will have reset back to OFF and your if statement will return to True.

What you need to do is trigger a different rule that cancels myTimer when motion is detected and myTimer exists.

Thanks @rlkoshak

The motion sensors I have send an OFF/Closed after 180 seconds, which is why I set my timer to the same. The expectation being that there would never be a situation when a door is closed and motion is activated at the exact same time.

Creating a new rule to cancel the timer seems straightforward enough, I didn’t really think if that approach. But it all makes sense now.