Capabilty button not working

Hi Guys,

I tried this button capabilty, seems like its not working or I coded wrongly?
here’s the code I’m running…

preferences {
section(“Title”) {
// TODO: put inputs here
input “customButton”, “capability.button”, title: “Select Button”, required: false, multiple: true
}
}

def initialize() {
subscribe customButton, “button”, ButtonHandler
}

def ButtonHandler(evt) {
log.debug evt.value
log.debug “Pressed”
}

What’s not working? Are you not getting a list of buttons to select? Or, is your event handler not getting called?

1 Like

Hi Linda,

My event handler not getting called

It doesn’t look like you’re ever calling initialize() so the subscription is never happening.

You’ll want to do that when the app is installed (and possibly updated)

http://docs.smartthings.com/en/latest/capabilities-reference.html#button

Edit: this might be easier to see (just a snippet and it’s a different device, but you get the idea)

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

    initialize()
}

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

    unsubscribe()
    initialize()
}

def initialize() {
    subscribe(sensorOpen, "contact.open", contactOpenHandler)
    subscribe(sensorClose, "contact.closed", contactClosedHandler)
}
2 Likes

Hi Carinda,

I did, just that I thought that was default function so i didn’t added in my thread.

I followed this guide also http://docs.smartthings.com/en/latest/capabilities-reference.html#button

but somehow is not working

Do you maybe want to post the whole thing?

/**

  • TestButton
  • Copyright 2015 Corn
  • Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except
  • in compliance with the License. You may obtain a copy of the License at:
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  • on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  • for the specific language governing permissions and limitations under the License.

*/
definition(
name: “TestButton”,
namespace: “TestButton”,
author: “Corn”,
description: “Test Button”,
category: “”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)

preferences {
section(“Title”) {
// TODO: put inputs here
input “thebutton”, “capability.button”
}
}

def installed() {
log.debug “Installed with settings: ${settings}”

initialize()

}

def updated() {
log.debug “Updated with settings: ${settings}”

unsubscribe()
initialize()

}

def initialize() {
// TODO: subscribe to attributes, devices, locations, etc.
subscribe(thebutton, “button”, buttonHandler)

}

// TODO: implement event handlers

def buttonHandler(evt) {
if (evt.value == “held”) {
log.debug “button was held”
} else if (evt.value == “pushed”) {
log.debug “button was pushed”
}

// Some button devices may have more than one button. While the
// specific implementation varies for different devices, there may be
// button number information in the jsonData of the event:
try {
def data = evt.jsonData
def buttonNumber = data.buttonNumber as Integer
log.debug "evt.jsonData: $data"
log.debug “button number: $buttonNumber”
} catch (e) {
log.warn “caught exception getting event data as json: $e”
}
}

Have you tried an actual device, or is it just not working in the using a virtual device in the IDE?
I was just able to get a button device working an an app, but the virtual device didn’t work. You can test using physical devices.

Edit: In fact, your app is working for me with my minimote.

1 Like

I see, so the problem is on the simulator, hmm…
Alright Carinda, thanks for your help :smile:

No problem; good luck with the rest of your app