Push a list of open contact sensors

Hi all,

I am attempting to get a list of all opened contact sensors so I can push a notification to my phone. How do I create a list in groovy? I have tried google and I am just not getting it. I am familiar with Python, but I seem to be missing something:

for(sensor in sensors) {
	if(sensor.id != evt.deviceId && sensor.currentValue('contact') == 'open') {
        openContacts.add($sensor)
    	isOpen = true            
	}
    sendPush ("The following contacts are still open: " + openContacts.join(", "))

Sensors.each { sensor ->
If (bla bla)
}

Or

Def open = sensors.find { sensor.currentValue (“contact”) == “open” }

off the top of my head anyway

1 Like

What am I doing wrong here? The following code always returns result as null

def contactOpenList() {
    def result = sensors.findAll() { it.currentValue('contact') == 'open'; }
    return result
    log.debug "Open results: $result"
}


def thermostatChange(evt){
	if (thermostat.currentValue("thermostatMode") != "off"){
        log.debug "termostat is " + thermostat.currentValue("thermostatMode")
        contactOpenList()
        log.debug "Result of open check is $result"
        if (result){
              turnOff()
        } else {
              log.debug "Result of open check is $result"
        }
    }
}

Is it supposed to be sensors.find

for your use case try this:

definition(
    name: "demo",
    singleInstance: true,
    namespace: "MikeMaxwell",
    author: "Mike Maxwell",
    description: "device selection",
    category: "My Apps",
  	iconUrl: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/Cat-ModeMagic.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/Cat-ModeMagic@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/Cat-ModeMagic@3x.png"
)

preferences {
    page(name: "main")
}

def main(){
	dynamicPage(name: "main", title: "main", uninstall: true, install: true) {
        section(){
        	 input(
            	name			: "devices"
                ,title			: "select contacts"
                ,multiple		: true
                ,required		: true
                ,type			: "capability.contactSensor"
                ,submitOnChange	: true
            )	
            if (settings.devices){
            	paragraph("contacts are open? ${contactOpenList()}")
            }
        }
    }

}

def contactOpenList(){
	def result = settings.devices.currentValue("contact").contains("open")
	return result
}

then change to the following:

def thermostatChange(evt){
	if (thermostat.currentValue("thermostatMode") != "off"){
        log.debug "termostat is " + thermostat.currentValue("thermostatMode")
       def result = contactOpenList()
       log.debug "Result of open check is $result"
        if ( result ){
              turnOff()
        } 
    }
}

So you have to define a variable in contactOpenList as well as thermostatChange? I tried result = contactOpenList before I posted and it still returned null. I’ll give this a shot when I get home and let you know.

I plan on calling this in different functions (are these called functions in groovy?).

Thanks!

I tried sensors.find and I only returned 1 value. I am attempting to return all open sensors in a list so I can push the list as well.

the example I gave you doesn’t return that output, as I designed it to fit into the code you pasted.
Find returns the first match, find all returns all of them. If you intend to return the device name in your output the code I gave you won’t work, it simply returns true/false, which fits into the code block you pasted above.

I’m guessing you already figured this out, I just joined the community a few months ago and had a similar need and stumbled across this post. I slightly modified your function with the following to return a list of all open contacts.

def contactOpenList() {
def result = []
result = result += sensors.findAll() { it.currentValue(‘contact’) == ‘open’; }
return result
log.debug “Open results: $result”
}

1 Like