Testing the current value of a Contact Sensor


#1

Ok, I am either blind or just plain stupid but I can not find any problem with the code below.

    def doorState = autoDoorSensor.currentValue("contact")
    log.debug "The door is currently $doorState"  // **Given the door is closed, this line will output "The door is currently [closed]"**
	if (doorState=="closed") {  		// *have also tried if (doorState=="[closed]")*
    	log.debug "door is closed"		// **This line is never executed**
    }

The line “if(doorState==…” is always false even though the sensor is closed. What am I missing here???


(John S) #3

Ah, you have a LIST there, that’s why the log line is [closed]

If autoDoorSensor has multiple:true, then you get a list, even if only one is picked

So you need to check the list using array operators to see if they’re ALL closed, or at least one is closed, or whatever you’re trying to test for there. Or change to multiple:false in the input section.


#4

To make matter even more interesting…

def doorState = autoDoorSensor.currentValue(“contact”)
log.debug “$doorState” // Log prints “[closed]” which is the correct current state
if (doorState!="[closed]") { if the doorState is NOT [closed]" then…
log.debug “$doorState” ***// WTF: Log prints “[closed]” ***
}


#5

Told ya I was either \blind or stupid :blush:


(John S) #6

We’ve all been there :smile: