Garage door app with open, close sensors and momentary

I am working on build a garage door opening/closing app and running into a slight issue. I am using the DHT simulated garage door opener which is working. However, when I run the script it doesn’t align with animation. I press the virtual switch to open, the switch changes to opening, and then open. Once it says open the garage door is activate and opens. How can I make the action of the garage door opening line up with the actual physical action? Any help is greatly appreciated.

preferences {
	section("Choose the switch/relay that opens the garage?"){
		input "mSwitch", "capability.momentary", title: "Physical Garage Opener?", required: true
	}
	section("Choose the sensor that senses if the garage is closed? "){
		input "closeSensor", "capability.contactSensor", title: "Physical Garage Door Closed Sensor?", required: true
	}
	section("Choose the sensor that senses if the garage is open?"){
		input "openSensor", "capability.contactSensor", title: "Physical Garage Door Open Sensor?", required: true
	}    
	section("Choose the Virtual Garage Door Device? "){
		input "vSwitch", "capability.doorControl", title: "Virtual Garage Door?", required: true
	}

}

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

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

def initialize()
{

    subscribe(vSwitch, "contact.open", openHandler)
    subscribe(vSwitch, "contact.close", closeHandler)

}

// Garage door opening process ------------------------------
def openHandler(evt) {
    log.debug "openHandler called: $evt"
	if(closeSensor.latestValue("contact").contains("closed") && openSensor.latestValue("contact").contains("open")){
		mSwitch.push()  
	    runIn(2000, openHandlerClose)
    }
}

def openHandlerClose(evt) {
    log.debug "openHandlerOpen called: $evt"
	if(closeSensor.latestValue("contact").contains("open") && openSensor.latestValue("contact").contains("open")){
		vSwitch.open()
        runIn(1000, openHandlerClose)
    } else if(closeSensor.latestValue("contact").contains("closed") && openSensor.latestValue("contact").contains("open")){
		vSwitch.finishOpening()
    }
}

The script works it is just the alignment of the switch action that I am hoping to fix. Thanks!

Could you edit your post to put your code in a code block? Put ``` on a separate line above and below your entire section of code. It makes your code much easier to read for anyone trying to help you.

A few changes I’d suggest:

  • In the DTH, delete “runIn(6, finishOpening)” and “runIn(6, finishClosing)” from the open and close methods. Right now, they’re automatically making the door show as closed after 6 seconds. You want your contact sensors to do that for you.
  • In your smartapp, you want to subscribe to “door.closing” and “door.opening” instead of “contact.close” and “contact.open” for vSwitch.
  • In your smartapp, subscribe to “contact.open” and “contact.close” for both openSensor and closeSensor, then create handlers for each that will update the state of vSwitch (using vSwitch.finishOpening and vSwitch.finishClosing) based on the contact sensor state.
  • The smartapp code you posted uses an event handler called “closeHandler” that isn’t defined.
  • Delete your “openHandlerClose” method. First off, it shouldn’t have evt declared as a parameter. More importantly though, you’re using it as a workaround to update the status of your vSwitch, using chained runins to do so. The proper way to do that would be to leverage the contact sensor events (as I described in the second bullet).
2 Likes