Subscribing To Button

I have a Holdable Button. In a custom app I want to subscribe to the button.

I can subscribe to "pushed" and I can subscribe to "held". But is there a way to just subscribe to the button and let the routine decide if it was pushed or held?

Unless something is different in Hubitat compared to ST (or I just drastically misunderstand ST's model), then you can't exactly do this. You'll have to create three separate subscriptions since your only options are subscribing to a specific device event (e.g., button 1 pushed) or all events for that attribute (e.g, pushed). However, either handler method takes the same parameter: an Event object. Therefore, if your goal is just to cram all of your logic into one method rather than two (or more) separate ones, you can certainly do that--just specify the same handler for all three subscriptions and perform your logic there instead.

For example:

subscribe(myPushableButton, "pushableButton", buttonHandler)
subscribe(myHoldableButton, "holdableButton", buttonHandler)

and:

def buttonHandler(evt) {
    if (evt.name == "pushed") {
        log.debug "button ${evt.value} pushed"
    } else if (evt.name== "held") {
        log.debug "button ${evt.value} held"
    }
}

Fair warning: I am writing this off the top of my head and have not run my code through the Groovy editor or actually tested it, so my apologies for any typos or mis-rememberings. :slight_smile:

it is completely different:

So I have to make 2 subscriptions, one for PUSHED and one for HELD.

I meant different with regards to event subscriptions in general, not the button model. But I see the issue now: I guess on ST, a single subscription would have been possible since you could just subscribe to "button" and figure out the rest from there--all the other data was crammed into the Event object. If the original poster came from ST originally (whose button model I had almost forgotten about until you mentioned this), I totally understand where the question is coming from.

...though the answer is still no, due precisely to the different model. :slight_smile:

Yep! They generate different events, and you need a subscription for each event. (These aren't all there are, by the way: some buttons may implement DoubleTapableButton [sic.] or ReleaseableButton.)

1 Like

lowercase pushed, and held, but yes one subscription for each.

1 Like

It's surprising how many times I'be been stymied by a capital letter. Something won't work and it turns out I capitalized it one place and not the other. Seems like I never learn...:grinning::grinning:

Download the Hubitat app