Controlling garage light with virtual switch, inputs momentary and contact sensor

Hello,

I am new to smartapp development and am trying to control my garage light. I was able to emulate my garage wall control panel and can use a momentary switch to control the light. From the momentary switch I am unable to tell if the light is on or off. I setup a sensor that monitors the 120V on the light bulb and lets me know if the light is on or off. I now have a sensor and a momentary switch that I would like to culminate into a virtual switch. The virtual switch will tell me if the light is on or off and allow me to switch it on or off in a master/slave and slave/master setup. Also, the switch can be turned on by the app, wall control panel, when opening the garage down either by control panel or remote. Here is my code so far, but it doesn’t seem to be working. Any help would be greatly appreciated.

definition(
	name: "Momentary Contact Virtual",
	category: "Convenience",
)

preferences {
	section("VSwitch") {
		input "vswitch", "capability.switch", required: true
	}
	section("MSwitch") {
		input "mswitch", "capability.momentary", required: true
	}
    section("Lightsensor") {
		input "lightsensor", "capability.contactSensor", 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, "switch.on", onHandler)
	subscribe(vswitch, "switch.off", offHandler)
	subscribe(lightsensor, "contact.open", openHandler)
       subscribe(lightsensor, "contact.closed", closedHandler)  
}

def onHandler(evt) {
    log.debug "onHandler called: $evt"
    if(vswitch.latestValue("switch").contains("on")){
		mswitch.push()
    }
}

def offHandler(evt) {
    log.debug "offHandler called: $evt"
    if(vswitch.latestValue("switch").contains("off")){
		mswitch.push()
    }
}

def openHandler(evt) {
	log.debug "OpenHandler called: $evt"
	if(lightsensor.latestValue("contact").contains("open")){
    	vswitch.on()
       }
}

def closedHandler(evt) {
	log.debug "closedHandler called: $evt"
	if(lightsensor.latestValue("contact").contains("closed")){
    	vswitch.off()
        }
}

can you provide more detail about what is or isn’t working?

As Tony said, any additional context will help. I’m assuming you’ve cleared any errors and now have a smartapp that runs but not as intended, and that lightsensor opens when the light is on.

It looks like all of your IF statements will always evaluate to true. Take this one for example:

subscribe(vswitch, “switch.on”, onHandler)

def onHandler(evt) 
{
    log.debug “onHandler called: $evt”
    if(vswitch.latestValue(“switch”).contains(“on”))
    {
        mswitch.push()
    }
}

onHandler is called whenever (and only when) vswitch is turned on. Therefore, vswitch.latestValue(“switch”).contains(“on”) is always true when it’s evaluated, and mswitch is pushed each time. If you delete all four IF control structures, I think you’ll find that the smartapp runs exactly the same as it currently does.

What I would expect from your smartapp as currently coded is to repeatedly cycle through all four event handlers in an endless loop. I think what you need is an IF in onHandler and offHandler to check the state of lightsensor, and only push mswitch if lightsensor doesn’t match the state of vswitch.

philh30, Thank you that worked excellent. It is now functioning as expected! The secret was having two condition necessary in the if statement to allow the action to happen.