Basic value compare not working?


(Greg B) #1

I’m creating my first app that will check if I left the garage open when the mode changes. I’m running into an odd issue though. I can’t seem to get it to properly check if the sensor is open. I’ve looked at every example I can find and it seems like this should work. Here’s the event handler.

def modeChangeHandler(evt) {
log.debug "Mode change to: ${evt.value}"

if (evt.value == newMode) {
    log.debug "Door ${door.displayName} is ${door.latestValue("contact")}"
	def isOpen = door.latestValue("contact").value == "open"
    log.debug "isOpen=${isOpen}"
	if (isOpen) {
    	def msg = "'${door.displayName}' was left open!"
        log.info msg
        //send(msg)
    }
}

Here’s the debug output.

3:46:54 PM: debug isOpen=false
3:46:54 PM: debug Door [Garage Door] is [open]
3:46:54 PM: debug Mode change to: Night
3:46:52 PM: debug Mode change to: Home

Notice it says the lastValue(“contact”) is “open”, but when I do a string compare, it ends up false. There has to be something simple I’m missing.

Thanks,
Newbie


(Chrisb) #2

I think there’s a problem in how you’re checking to see if the door is open. Here’s how I do it for a program I have to see if the door is closed:

    def garageClose(evt) {
    	//check if door is open
    	if (door.currentContact == "closed") {
            PERFORM TASK HERE
        }
        else {
            PERFORM DIFFERENT TASK HERE
        }
    }

(Chrisb) #3

I’d say try this line of code for the def isOpen

def isOpen = door.currentContact == "closed"

See if that works.


(Greg B) #4

Ok, just so I don’t go insane, when I compare door.currentContact == “closed”, it means the door is actually open? I tried this and it does work, but the fact that if you debug door.currentContact it prints “[open]” is extremely confusing.


(Chrisb) #5

I’m sorry… @gbronzer, that should be “open”. I’m not sure why it would work when closed.

That said, I think you’re putting in an extra step that you don’t really need to do it. If you look at my code you can just do the comparison right in the if statement:

if (door.currentContact == "open") {

(Greg B) #6

Actually, == “closed” is what works in my case, but I think it’s a quirk of the sensor. This is a garage door sensor, but I’m using a SmartThings Multi. So “closed” is actually based on the angle, rather than the actual contact sensor. I think to make the app truly multi-purpose, I’d have to detect how the door is setup. When you setup a door as a garage door with a Multi, SmartThings handles it slightly differently.

Anyways, I’ve got it working for my needs right now so thanks for the help.