First try at coding HB < Why Not Work LOL

Seems to work kinda .. you can pick a Button device, pick the lights, pick the button.
but the lights do not turn on :frowning:
What Am I doing wrong ?

input "buttonDevice", "capability.pushableButton"
IS the only way I found to get a list of my "switches and battery operated 4-button remotes

preferences {
    section("Settings") {
        input "buttonDevice", "capability.pushableButton", title: "Select Button Device", multiple: false, required: true
        input "lights", "capability.switch", title: "Pick Lights to Turn On", multiple: true, required: false
        input "selectedButton", "enum", title: "Select Button to Turn On Lights", options: ["1", "2", "3", "4"], required: true, defaultValue: "1"
    }
}

def installed() {
    subscribe(buttonDevice, "button", buttonHandler)
}

def updated() {
    unsubscribe()
    subscribe(buttonDevice, "button", buttonHandler)
}

def buttonHandler(evt) {
    log.debug "Button ${buttonDevice} was pressed with value: ${evt.value}"

    if (evt.value == "pushed" && evt.displayName == selectedButton) {
        turnOnLights()
    } else {
        log.warn "Button press event with unexpected value: ${evt.value} or unexpected button: ${evt.displayName}"
    }
}

def turnOnLights() {
    log.debug "Turning on lights with button: ${selectedButton}"
    lights.each { light ->
        light.on()
    }
}

Your subscription should be to "pushed".

subscribe(buttonDevice, "pushed", buttonHandler)

2 Likes

Would I be right in thinking that the "name" column from the Events page for a device holds the name of the Event you would use when subscribing?

image

So are the list of possible events basically a one-for-one with the attributes listed for the capability? So buttons are split out into PushableButton, HoldableButton, etc, with attributes that match the events they can include?

Yeap think thats it .. will give it try in a few thanks for the hint :slight_smile:

Nope .. get this ..

Button press event with unexpected value: 1 or unexpected button: Switch - Porch 2

In addition to @csteele stated, I found it more intuitive if I chose a more descriptive name in the input command.... for instance

preferences {
    section("Settings") {
        input "buttonDeviceHandle", "capability.pushableButton", title: "Select Button Device", multiple: false, required: true
    }
}

def installed() {
    subscribe(buttonDeviceHandle, "pushed", buttonHandler)
}
[/quote]