Adding Button Actions (Pushed, Held, and Pushed Twice) to Custom SmartApp Code

Hello,

I am using a Smartthings Button to toggle Smartthings plugs and various lights. This was set up in the button’s settings in the Smartthings app.

The issue I’ve encountered is that sometimes devices are turned on and off using other methods such as Alexa, which causes the current on/off state to be out of sync with the button toggle. Then the button has to be pressed twice to correct this. I searched through all the previous threads on this topic, and encountered 2 solutions (not involving Webcore).

The first was to use the smart lighting SmartApp and create 2 automations - one to turn the device on and the other to turn it off, and setting the “Only if switch” setting to the opposite state. This way it would only turn on if the device is turned off, and off only if the device is turned on. People reported success with this, but in my testing, I have too many issues where the device gets briefly turned on then back off because there are 2 automations.

The second was a user’s custom SmartApp that would toggle based on the state of the device. I installed their code and it works great for single button presses.

The only thing missing from their code is a way to set this for button double presses and button holds, as the Smartthings button comes with 3 different ways to use it. I tried to figure out how to add this code but was not successful.

Does someone know how to add the 3 states to the custom SmartApp code? The Smart Lights SmartApp by SmartThings has an option after selecting the button you want to use to pick a button action of pushed, held, or pushed twice, so it should be possible.

Link to custom code forum post

Custom code being used:

definition(
name: “Toggle Light Button”,
namespace: “personal”,
description: “This app create a toggle button for light switch”,
category: “Convenience”,
author: “(me)”,
iconUrl: “URL removed by me due to new user url restriction”,
iconX2Url: “URL removed by me due to new user url restriction”,
iconX3Url: “URL removed by me due to new user url restriction
)

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()
}
}
}

Thank you.

I may have been the one who’s post you read regarding the creation of two actions, one for ‘on’ and one for ‘off’, with the ‘on’ having a condition of ‘only if the light is off’, and vice versa. I had concerns about ‘race’ conditions but everything has worked just fine, so I wonder why you are having issues. How long after you set it up did you try to use it? One thing I’ve learned about SmartThings is that you don’t pay any attention to any testing done immediately after a change … it seems to take a non-trivial amount of time for things to ‘settle down’ (how very technical!).

Anyway - I’m interested in following your approach. What button device are you using? I’m struggling with an Aeotec (Aeon) Wallmote Quad. It also has ‘held’ options (though they don’t seem to work well).

Hello Steerpike,

The code you posted works great, I use it everyday with my SmartThings buttons.

My dilemma is that the SmartThings buttons feature 3 button actions: press, double press, and hold. The code from your post only has a way to set an action for a single button press, but not the double press or hold actions.

I was wondering what code I needed to add to the SmartApp for this functionality. I tried to figure this out but couldn’t as I don’t have enough experience with writing code for SmartApps.

Thanks!