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.

© 2019 SmartThings, Inc. All Rights Reserved. Terms of Use | Privacy Policy

SmartThings; SmartApps®; Physical Graph; Hello, Home; and Hello, Smart Home are all trademarks of the SmartThings, Inc.