Toggle with Smart Lighting

definition(
    name: "Toggle Light Button",
    namespace: "personal",
    description: "This app create a toggle button for light switch",  
    category: "Convenience"
)

preferences {
    section("The button that will toggle the light") {
        input "toggleButton", "capability.button", title: "Select", required: true
    }

    section("The switch to be controlled") {
        input "controlledSwitch", "capability.switch", title: "Select", required: true 
    }
}

def installed(){
    subscribe(toggleButton, "button", toggleHandler, [filterEvents: false])
}

def updated(){
    unsubscribe()
    subscribe(toggleButton, "button", toggleHandler, [filterEvents: false])
}

def toggleHandler(evt) {
    if (evt.name == 'button' && evt.value) {
        if (controlledSwitch.latestState("switch").value == "off") {
           controlledSwitch.on()
        } else {
           controlledSwitch.off()
        }
    }
}
1 Like