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(", "))
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"
}
}
}
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?).
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â
}