How to stop runIn()?

Maybe it’s just as simple as calling runIn() again but I can’t seem to confirm how to do this. What I’d like to do is delay the setNoMotionTemp() event by x minutes but if motion is detected again unset the runIn() set by noMotionHandler().

/**
 *  Motion Activated Thermostats
 *
 *  Author: Chris LeBlanc
 */
definition(
    name: "Motion Activated Thermostats",
    namespace: "cl",
    author: "Chris LeBlanc",
    description: "Changes your thermostat settings automatically in response to motion sensing.",
    category: "Green Living",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo@2x.png"
)

preferences {

    page( name:"page1", title:"Choose a Thermostat and Sensor", nextPage:"page2", uninstall:true, install:false ) {
        section("Choose thermostat... ") {
            input "thermostat", "capability.thermostat"
        }
         section("When there's movement...") {
            input "motion", "capability.motionSensor", title: "Where?", multiple:true
        }
         section("Delay no motion event...") {
            input "after", "number", title: "Minutes", description: "60 minutes",  required: false
        }
    
    }

    page( name:"page2", title:"Motion Tempuratures", nextPage:"page3", uninstall:true, install:false ) {
        section("Set air conditioning setting..."){
            input "coolingSetpointMotion", "number", title: "Degrees?"
        }
        section("Set heat setting...") {
            input "heatingSetpointMotion", "number", title: "Degrees?"
        }
    }

    page( name:"page3", title:"No Motion Tempuratures", nextPage:"page3", uninstall:true, install:true ) {
        section("Set air conditioning setting..."){
            input "coolingSetpointNoMotion", "number", title: "Degrees?"
        }
        section("Set heat setting...") {
            input "heatingSetpointNoMotion", "number", title: "Degrees?"
        }
    }



}

def installed()
{

    subscribe(motion, "motion.active", motionHandler)
    subscribe(motion, "motion.inactive", noMotionHandler)

}

def updated()
{
    unsubscribe()

    subscribe(motion, "motion.active", motionHandler)
    subscribe(motion, "motion.inactive", noMotionHandler)

}




def motionHandler(evt)
{
    log.debug "value: $evt.value, event: $evt, settings: $settings, handlerName: ${evt.handlerName}"
    thermostat.setCoolingSetpoint(coolingSetpointMotion)
    thermostat.setHeatingSetpoint(heatingSetpointMotion)
    thermostat.poll()
}

def noMotionHandler(evt)
{
    def delay = (after != null && after != "") ? after * 60 : 3600
    runIn(delay, setNoMotionTemp)
}

def setNoMotionTemp()
{
    log.debug "value: $evt.value, event: $evt, settings: $settings, handlerName: ${evt.handlerName}"
    thermostat.setCoolingSetpoint(coolingSetpointNoMotion)
    thermostat.setHeatingSetpoint(heatingSetpointNoMotion)
    thermostat.poll()
}

// catchall log.debug "value: $evt.value, event: $evt, settings: $settings, handlerName: ${evt.handlerName}"

unschedule()

Isn’t that going to unsubscribe all the events?

Random sidenote, any idea why my console log doesn’t work :frowning: . This is making it impossible to debug. I’m just using virtual thermos and motions and the log.debug command you see above.

No… Just anything you have scheduled in your app (which runIn does).

I’d contact support if your logs are not working.

I switched some stuff up so it all runs in one eventHandler function and it seems to immediately run the inactive motion setTemp which is frustrating. Can you see anything weird? I tried both the unsubscribe and what I have below (runIn(0, …)).

I also admittedly don’t have a good way to test the result of evt.value, does that seem like it’d work?

 /**
 *  Motion Activated Thermostat
 *
 *  Author: Chris LeBlanc
 */
definition(
    name: "Motion Activated Thermostat",
    namespace: "cl",
    author: "Chris LeBlanc",
    description: "Changes your thermostat settings automatically in response to motion sensing.",
    category: "Green Living",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo@2x.png"
)

preferences {

    page( name:"page1", title:"Choose a Thermostat and Sensor", nextPage:"page2", uninstall:true, install:false ) {
        section("Choose thermostat... ") {
            input "thermostat", "capability.thermostat"
        }
         section("When there's movement...") {
            input "motion", "capability.motionSensor", title: "Where?"
        }
         section("Delay no motion event...") {
            input "after", "number", title: "Minutes", description: "60 minutes",  required: false
        }
    
    }

    page( name:"page2", title:"Motion Tempuratures", nextPage:"page3", uninstall:true, install:false ) {
        section("Set air conditioning setting..."){
            input "coolingSetpointMotion", "number", title: "Degrees?"
        }
        section("Set heat setting...") {
            input "heatingSetpointMotion", "number", title: "Degrees?"
        }
    }

    page( name:"page3", title:"No Motion Tempuratures", nextPage:"page3", uninstall:true, install:true ) {
        section("Set air conditioning setting..."){
            input "coolingSetpointNoMotion", "number", title: "Degrees?"
        }
        section("Set heat setting...") {
            input "heatingSetpointNoMotion", "number", title: "Degrees?"
        }
    }

}

def installed()
{
    subscribe(motion, "motion.active", eventHandler)
    subscribe(motion, "motion.inactive", eventHandler)
}

def updated()
{
    unsubscribe()

    subscribe(motion, "motion.active", eventHandler)
    subscribe(motion, "motion.inactive", eventHandler)
}

def eventHandler(evt)
{
    //log.debug "value: $evt.value, event: $evt, settings: $settings, handlerName: ${evt.handlerName}"

    def delay = (after != null && after != "") ? after * 60 : 3600
    if (evt.value == 'active') 
    {
        runIn(0, setTemp('active'))    
    } 
    else if (evt.value == 'inactive') 
    {
        runIn(delay, setTemp('inactive'))        
    }
}

def setTemp(state)
{
    if (state == "active") 
    {
        thermostat.setCoolingSetpoint(coolingSetpointMotion)
        thermostat.setHeatingSetpoint(heatingSetpointMotion)
    } 
    else 
    { 
        thermostat.setCoolingSetpoint(coolingSetpointNoMotion)
        thermostat.setHeatingSetpoint(heatingSetpointNoMotion)
    }

    thermostat.poll()
}

// catchall log.debug "value: $evt.value, event: $evt, settings: $settings, handlerName: ${evt.handlerName}"

Not unsubscribe; unschedule().

Hah ya, I figured that out last night doh :slight_smile: Thanks!

Also, if your console stops working, just log out and log back in to the IDE. Should bring it right back.