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()
}
}