Code help: Button event handler

I’m writing my very first smart app and struggling with some of the basics. I’ve successfully subscribed to temperature events and have working code based on these events, but now I’ve run into a problem with Button events. I’ve stripped everything away down to the basic code and I still can’t get my code to work.

The Use Case here is that in my Smart App, I define an Iris Smart Button in the configuration screen and then when that button is pressed, I log some information to debug. Super simple, but I cannot make it work. Clearly I’m doing something wrong with the event handler.

Any help would be greatly appreciated!

/**

  • Test Code
    */

definition(
name: “Test Code”,
namespace: “wjarrettc”,
author: “W. Jarrett Campbell”,
description: “test”,
category: “Convenience”,
iconUrl: “http://cdn.device-icons.smartthings.com/Appliances/appliances1-icn@2x.png”,
iconX2Url: “http://cdn.device-icons.smartthings.com/Appliances/appliances1-icn@2x.png
)

preferences {

section("Choose the button... "){
  input "buttonDevice", "capability.button", title: "Test Button", multiple: false, required: true

}

}

def installed()
{
initialize()
}

def updated()
{
unsubscribe()
initialize()
}

def initialize()
{

subscribe(buttonDevice, "button", buttonEvent)
log.debug "subscribed to BUTTON"

}

def buttonEvent(evt)
{
log.debug “$evt.device.displayName reported event $evt.stringValue”
}

I copied and pasted it, worked fine for me. What device are you using for a button?

1 Like

I was testing it in the simulator using virtual button and could not make it work (my previous experience using virtual temperature devices worked fine).

I switched to an actual button device (Iris Smart Button) and it works like a charm. So now I guess my question is what am I doing wrong in using the simulator :slight_smile:

Thanks for testing it for me @whoismoses!

1 Like

Likely nothing. A lot of people around here will say “don’t use the simulator”.

3 Likes