Passing parameters in a runin() statement


(Gerald) #1

I have newbie question, the code using is based on the sample app in the ST documentation.

I have several motion sensor setup, I’d like to use a “generic” time delay function for all of them, thus I’d like to pass in the sensor object via a runin() statement and return a true/false back.

TIA for any advice

code abstract
subscribe(doorMotion, “motion.inactive”, doorMotionStoppedHandler)

def doorMotionStoppedHandler(evt){
if (runIn(60 * minutes, checkMotion(evt))){entryLight.off()}
}
def checkMotion(themotion) {
log.debug “In checkMotion scheduled method”

def motionState = themotion.currentState("motion")
def lReturn = false

if (motionState.value == "inactive") {
    // get the time elapsed between now and when the motion reported inactive
    def elapsed = now() - motionState.date.time

    // elapsed time is in milliseconds, so the threshold must be converted to milliseconds too
    def threshold = 1000 * 60 * minutes

    if (elapsed >= threshold) {
        log.debug "Motion has stayed inactive long enough since last check ($elapsed ms):  turning switch off"
        lReturn = true
    } else {
        log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms):  doing nothing"
    }
} else {
    // Motion active; just log it and do nothing
    log.debug "Motion is active, do nothing and wait for inactive"
}
return lReturn

}


(Bobby) #2

def doorMotionStoppedHandler(evt){
runIn(60 * minutes, checkMotion)
}

def checkMotion{

def motionState = themotion.currentState(“motion”).comtains"inactive"

if(motionState){

entryLight.off()

}


(Gerald) #3

Bobby, @SBDOBRESCU Thanks for the reply and for the shorter syntax, but what I’d like to be able to do is use the checkMotion function for multiple motion sensors so that the checkMotion doesn’t contain any specific light/switch control.

Is it possible ?


(Bobby) #4

Everything is possible, but it gets a little more complicated when you allow multi select. The question is, what would your use case be? If ANY or ALL sensors are stil inactive then turn off one light?

You just have to loop through the list of sensors. I use list.each… but there are other methods as well


(Bobby) #5

Actually, I ignored your title and jumped on your code. Going back to your title…You can actually even pass a map with the runin that can contain the device name and the state to check…Something like this

def data =[ device:device name, attribute: desired state (attribute)]

Then…

runIn(minute*60, checkMotion, [data: data])


(Gerald) #6

Bobby, @SBDOBRESCU, Thanks for the reply, to answer your question about use case… what I’m attempt to do is “map/assign” a motion sensor to a light or lights… i.e. doorMotion sensor activity would be controlling entryLight, stairwayLight and landing. livingroomMotion is assigned to 3 lights in the living room area.

my apologies, being so new to ST, I’m not sure I understand the second reply, please explain the following and how do I use it? second part of my problem, is can I return a value back ?

def data =[ device:device name, attribute: desired state (attribute)]
runIn(minute*60, checkMotion, [data: data])

thanks in advance


(Bobby) #7

I am literally working on something very similar to implement “if contact stays open for x number of minutes” for folks using RemindR. I’ll let you know when is done and I’ll point out the line # to see what I mean :smile:

Actually, you can check the code now, it has many runin examples in the child app.


(Gerald) #8

@SBDOBRESCU Thanks Bobby, I really appreciate the help. I’ve dived into the child app , (best I can), but I don’t see a runIn example passing in a object to the function called by the runIn cmd, or am I missing something?

TIA


(Bobby) #9

That is the part I am working on now :smile:


(Gerald) #10

@SBDOBRESCU Thanks Bobby, is what I’m seeking to do normally not done? or should it be done in a more appropriate manner?

TIA, look forward to seeing your results.


(Bobby) #11

For your example you don’t need anything complicated. If you enable multi select for my Switches, then you don’t need to pass anythig through runIn. you just say runIn (minutes, handler). And under handler you have mySwitches.off() and will turn all of them…

passing parameters with runIn is not as common, because there are not many cases where you actually need that. It happens that the one I am working on calls for that :smile:


(Gerald) #12

@SBDOBRESCU Bobby, thanks… I understand what you are saying and have several lights setup that way with contact switches. In this case I’m mapping multiple lights “sets” to different motions sensors. I know it’s a pretty narrow business case for reusable code.

I appreciate the help and patience as I learn ST programming language/syntax.

TIA


(Bobby) #13

You may want to check Mike’s app. Don’t spin your wheels. If the use case gets complicated, someone else shared an app that can help you out. That’s the beauty of this community… :smile:

Smart Zone motion detector (Zone Motion Manager) 2.1.0 release