Return device.currentValue


(DawgOnKing) #1
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.


(DawgOnKing) #2

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}


(Adam Lanners) #3

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


(DawgOnKing) #4

You’re a good man @adam. A good man indeed.


(DawgOnKing) #5
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.