Motion Sensor Does not send Inactive status

smartapp
motionsensor
hubv2

(Jonathan B) #1

Hello all,
I am just getting into the smartthings stuff and wanted to try my hand at creating a smartapp. Anyways this is very similar to what they do in the tutorial but mine doesn’t seem to be working. Long story short when my motion sensor goes inactive it does not send that event so that I can act upon it.

Below is my code:

preferences {
    section("When there's movement...") {
        input "motion1", "capability.motionSensor", title: "Where?", multiple: true
    }
    section("Turn on a light...") {
        input "switch1", "capability.switchLevel", multiple: true
    }
    section("Dimmer value in Day mode...") {
        input "dimmerDay", type: "number", title: "Dimmer value (1-99) for day mode", required: true
    }
    section("Dimmer value in Night mode...") {
        input "dimmerNight", type: "number", title: "Dimmer value (1-99) for night mode", required: true
    }
    section("Turn off light after...") {
        input "delay", type: "number", title: "Delay (in minutes) before turning off lights (0 is off)", required: true
    }
}

def  turnOffRequests = 0

def installed()
{
    subscribe(motion1, "motion", motionHandler)
}

def updated()
{
    unsubscribe()
    subscribe(motion1, "motion", motionHandler)
}

def motionHandler(evt) {
    log.debug evt.value
    if (evt.value == "active")
    {
        log.debug "Calling motionActive"
        motionActive()
    }
    else if (evt.value == "inactive")
    {
        log.debug "Calling motionInactive"
        motionInactive()
    }

}

def motionActive (){
    if (switch1.currentSwitch.contains("off"))
    { 
        sendNotificationEvent("Motion detected, turning on lights")
        def currMode = location.mode // "Home", "Away", etc.
        if (currMode == "Day")
        {
            log.debug "Light is off and mode is " + currMode + " so setting to " + settings.dimmerDay + "%"
            switch1.setLevel(settings.dimmerDay)
        }
        else
        {
            log.debug "Light is off and mode is " + currMode + " so setting to " + settings.dimmerNight + "%"
            switch1.setLevel(settings.dimmerNight)
        }
    }
    else
    {
        sendNotificationEvent("Motion detected but light is already on, doing nothing")
    }
}

def motionInactive() {
    if (settings.delay != 0)
    {
        turnOffRequests = turnOffRequests + 1
        sendNotificationEvent("Turning off lights in ${settings.delay} minutes.")
        def delaySeconds = settings.delay * 60
        runIn(delaySeconds, turnOff)
    }
}

def turnOff()  {
    log.debug "turnOffRequests = " + turnOffRequests
    if (turnOffRequests == 1)
    {
        switch1.setLevel(0)    
    }
    turnOffRequests = turnOffRequests - 1    
}

Thanks,
Jon


(Anthony S.) #2

You’re not alone I’m seeing the same exact issue


(Jonathan B) #3

Thanks for the reply. At least I know that I am not the only one and not crazy!


#4

Tagging @jody.albritton


(Jonathan B) #5

I figured out my issue. Apparently I am not allowed to do the above. Found it in the documentation, now everything seems to be working as I expected.

Thanks,
Jon