Return device.currentValue

preferences {
section("Doors") {
	input "doors", "capability.contactSensor", title: "Door to monitor:", multiple: true, required: true
}
}

def installed() {
log.debug "Installed with settings: ${settings}"

initialize()
}

def updated() {
log.debug "Updated with settings: ${settings}"

unsubscribe()
initialize()
}

def initialize() {
log.debug doors.displayName
doorStates()
}

def doorStates() {
log.debug doors.currentValue("contact")
def doorState = doors.currentValue("contact")
log.debug doorState
if (doorState == "closed") {
	log.debug "The door is closed"
} else {
	log.debug "The door is open"
}
}

Alright, itā€™s late and Iā€™ve been triggering log errors for a while. The above works correctly and says that the device value is closed. So why is the if/else statement returning ā€œThe door is openā€ā€¦?

Again, this has to be my unfamiliarity with Groovy. This is painful to lose time on an if/else statementā€¦

Thanks.

I got this to return correctly by converting the returned currentValue to a string (currentValue.join()) for the comparison. I donā€™t feel like thatā€™s the preferred method however - if someone can chime in Iā€™d appreciate it.

Iā€™m simply checking the value of a set of doors for a Good Night app. If the doors are open, close them. So while I have the above solved, I want to iterate the returned values and trigger and event depending on their state. Iā€™m going to have to check the values of the returned array so I donā€™t think the method I just used will work.

Going to bed. {sleepy}

Try using if (doorState.contains('closed')) {} instead.

1 Like

Youā€™re a good man @adam. A good man indeed.

def doorStates() {
def doorState = doors.collect{it.label + ': ' + it.currentValue("contact")}
/*def doorState = doors.currentValue("contact")*/
log.debug doorState
if (doorState.contains('closed')) {
	log.debug "The door is closed"
} else {
	log.debug "The door is open"
}
}

This is still returning ā€œThe door is openā€ when all are closed. The commented out ā€˜def doorStateā€™ returns ā€œThe door is closed.ā€

Any idea why? By defining doorState as deviceLabel + deviceCurrentValue I get an incorrect return.