# Can I use a button to toggle a device on and off? Can I use conditions in scenes?

**URL:** <https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815>\
**Category:** Automation Ideas\
**Created:** [August 4, 2017, 11:15pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815 "2017-08-04T23:15:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![discofreakboot](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/discofreakboot/32/202896_2.png) [@discofreakboot](https://community.smartthings.com/u/discofreakboot)\
**Post date:** [August 4, 2017, 11:15pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/1 "2017-08-04T23:15:49Z")

</div>

Perhaps I’m in the wrong section, but here goes…

I just bought and connected a NodOn Soft Remote. ST registers all the button presses, but I can’t seem to find a way to link one of the buttons directly to a device. For instance, I’d like the open circle to toggle my bedroom light on or off, depending on which state it’s already in. Please tell me there’s a simple way to do this instead of having to create a separate scene to turn the light on, and another to turn it off, and another to set my ceiling fan to full, and another to set the ceiling fan to low, etc.

Also, when creating an automation, is it possible to set conditions? i.e. “If bedroom light is on and button 4 is pressed, turn it off, else if bedroom light is off and button 4 is pressed, turn it on.”

---

<div class="post-metadata">

**Author:** ![JH1](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/jh1/32/37723_2.png) [@JH1](https://community.smartthings.com/u/JH1)\
**Post date:** [August 4, 2017, 11:37pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/2 "2017-08-04T23:37:28Z")

</div>

You can do it with Webcore for sure…

Including the conditions you are talking about…

I don’t know of anyway to that without Core, Webcore, Rule Machine or one of the other logic engines. I don’t think SmartLighting is that flexible.

---

<div class="post-metadata">

**Author:** ![ogiewon](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/ogiewon/32/13743_2.png) [@ogiewon](https://community.smartthings.com/u/ogiewon)\
**Post date:** [August 4, 2017, 11:41pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/3 "2017-08-04T23:41:43Z")

</div>

Just use the built in Smart Lighting app to use a button to toggle a light.

---

<div class="post-metadata">

**Author:** ![JDRoberts](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/jdroberts/32/56577_2.png) [@JDRoberts](https://community.smartthings.com/u/JDRoberts)\
**Post date:** [August 5, 2017, 1:09am UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/4 "2017-08-05T01:09:19Z")

</div>

Welcome! 🕶 That’s a very cool device. It’s finally available now in the US, it was released in Europe first, and I’m sure it’s going to be very popular.

First things first… You need to use a device type handler (DTH) which will report each press as a different button “number.” Once that is happening, any smartthings automation, including the official smartlighting feature, which can handle a “button controller” will let you assign different actions to the different button “numbers.”

You can find device type handlers that have been contributed by other community members by using the quickbrowse lists in the community – created wiki. In this case, look on the list for “remotes and buttons.” I know there’s at least one there for the octan device.

[http://thingsthataresmart.wiki/index.php?title=How\_to\_Quick\_Browse\_the\_Community-Created\_SmartApps\_Forum\_Section#Quick\_Browse\_Links\_for\_Device\_Type\_Handlers](http://thingsthataresmart.wiki/index.php?title=How_to_Quick_Browse_the_Community-Created_SmartApps_Forum_Section#Quick_Browse_Links_for_Device_Type_Handlers)

If you haven’t used custom code before, it’s pretty simple. Basically you’re going to copy the author’s code and paste it into your own account. The following FAQ discusses that general process.

> [@\[OBSOLETE\] FAQ: An Overview of Using Custom Code in SmartThings (SmartThings Classic, Groovy Code)](https://community.smartthings.com/t/faq-an-overview-of-using-custom-code-in-smartthings/16772):
>
> The original FAQ information in this thread refers to the original smartthings architecture, which was in place through the end of 2022 and a little beyond. Custom code was written in the groovy programming language and ran in the smartthings cloud. As of May 2023, you can no longer add new Groovy DTH‘s or smartapps to your smartthings account. So if you came here because of a link in an old forum thread, or on another site, I’m afraid the Groovy information is no longer usable. All of this was replaced by a new architecture. Cloud to cloud integrations use the smartthings API. Hub connected devices, mostly Zigbee and zwave, now use “Edge Drivers,” written in the LUA programming language. Here’s the official announcement on those: The process for using a custom Edge Driver in the new …

Ok, that takes care of getting the button presses reported to the SmartThings Hub. 🕶 Now you want to set up some conditional logic.

The official SmartThings features don’t really support much conditional logic. They’re more for the simple kind like “if button three is pressed, turn on that light.”

But as already mentioned, webcore is a very powerful community – created rules engine which can do everything you asked for and more. 🎉 Setting it up is a little complicated, but there are many community members who will be glad to help:

> [@FAQ: What is WebCoRE? (And what was CoRE?)](https://community.smartthings.com/t/faq-what-is-core/59981):
>
> CoRE (now WebCore) is a very powerful community – created rules engine for SmartThings. It allows you to set up stacked conditionals like “If A then B but only while C and not if D.” Setting it up can be quite complex, but there are many community members who will be glad to help you. For example, here is a piston that one member created to notify him if the dog had already been fed twice that day and he picked up the dog bowl for a third time. He needed this because he has a large family and people didn’t always remember whose turn it was to feed the dog. dogblush [image] The original version of CoRE Has now been replaced by a new version, WebCoRE, which moves the data entry to a web interface. This allows for flexibility and an architecture that requires more memory than is av…

All of that said, as @ogiewon mentioned, The official smart lighting feature does let you set up a button to toggle a light on and off. So if that’s all you need to, you can just do it with the official feature. 🕶 Unfortunately at the present time the smart things mobile app is not navigable by voiceover, so I can’t show you a screenshot. But you should be able to find it if you just set up a new smart lighting automation.

[https://support.smartthings.com/hc/en-us/articles/205949776-How-to-create-lighting-automations-with-Smart-Lights](https://support.smartthings.com/hc/en-us/articles/205949776-How-to-create-lighting-automations-with-Smart-Lights)

---

<div class="post-metadata">

**Author:** ![Steerpike](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@Steerpike](https://community.smartthings.com/u/Steerpike)\
**Post date:** [May 2, 2019, 5:17am UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/5 "2019-05-02T05:17:29Z")

</div>

I believe the problem with the built-in Smart Lighting app (and its ‘toggle’ feature) is that if any other ‘event’ can turn on/off the light being controlled by the ‘toggle’, things get out of sync. Eg - you touch the button and the light turns on; you touch the button again and the light turns off. Great. But lets say you have a motion-sensor that also turns on the light. The next time you touch the button, nothing happens because the button ‘thinks’ the light is off so it tries to turn it on, but it’s on already so the end-user thinks the button was ignored, and has to press again for the ‘off’ action. I found a post on Reddit that solves this. I’m actually really surprised that it worked (for reasons I’ll explain later). Here’s the post on Reddit for reference:

> **[r/SmartThings - How to get button to toggle based on the target light's...](https://www.reddit.com/r/SmartThings/comments/a3nqrt/how_to_get_button_to_toggle_based_on_the_target/)**
>
> 3 votes and 10 comments so far on Reddit

  
I followed the steps to the letter and it worked great.

What I don’t understand is, is it perfectly legal to define two separate Smart Lighting ‘automations’ that are tied to the same event (a button press)? I would have thought that could lead to problems. Are there any such limits or is it a case of ‘buyer beware’?

For those who don’t read the Reddit post, or just in case it disappears, the steps are:

Using the classic app, add or open the Smart Lighting SmartApp. You’ll need to have two separate automations, one for On and one for Off.

For the On status create a new automation:

- _Devices to control_ : [your light(s)]
- _What do you want to do:_ Turn on
- _Select trigger:_ Button -\> [your button]
- _Button action:_ pushed
- _Toggle on and off_ : **OFF**
- More options…:
  - Only if switch: [light of choice] is **Off**
  - [leave the others untouched unless you want to set additional parameters]

Then create a second automated action which just replaces all the Offs to Ons and vice versa.

---

<div class="post-metadata">

**Author:** ![JDRoberts](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/jdroberts/32/56577_2.png) [@JDRoberts](https://community.smartthings.com/u/JDRoberts)\
**Post date:** [May 2, 2019, 1:37pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/6 "2019-05-02T13:37:12Z")

</div>

I can’t read code very well, but if I’m hearing right, I believe it’s the “only if“ which is saving you in this regard. 🕶

You can definitely have multiple automations trigger off of the same event without any problems. That would be the button press.

But if I’m understanding things correctly the “only if” is essentially the same as setting up one rule for a device if it’s Monday and a different rule for the same device under the same conditions but with a different outcome if it’sTuesday. Multiple rules applying to the same trigger event, but with different outcome filters. 🕶

---

<div class="post-metadata">

**Author:** ![Steerpike](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@Steerpike](https://community.smartthings.com/u/Steerpike)\
**Post date:** [May 2, 2019, 4:53pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/7 "2019-05-02T16:53:10Z")

</div>

Clearly the ‘Only If’ is what’s making this work. But my concern was related to how exactly ST invokes these routines. To explain further, let’s describe the two actions as

1. If button pressed, turn on lights but only if off
2. If button pressed, turn off lights but only if on  
So, I could imagine a scenario / implementation as follows: light is off, and button is pressed. ST runs routine 1, which checks status; light is off, so it turns light on. Now the light is ON. Next, ST runs routine 2, which checks status; light is now ON, so it ‘should’ turn light off. So in this scenario, the light would only come on momentarily. Clearly that’s not the case (lights do in fact come on and stay on), so that tells me the timing between running actions 1 and 2 is very fast, and the check of status for action 2 is being done before the ‘turn light on’ action from action 1 is complete. But it seems inherently risky to rely on this!

It all comes down to how the ‘engine’ for ST does it’s work behind the scenes.

---

<div class="post-metadata">

**Author:** ![JDRoberts](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/jdroberts/32/56577_2.png) [@JDRoberts](https://community.smartthings.com/u/JDRoberts)\
**Post date:** [May 2, 2019, 5:39pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/8 "2019-05-02T17:39:17Z")

</div>

Yep, there’s definitely a potential race condition. Just have to try it and see how it goes I guess. 🕶

---

<div class="post-metadata">

**Author:** ![orangebucket](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/orangebucket/32/81590_2.png) [@orangebucket](https://community.smartthings.com/u/orangebucket)\
**Post date:** [May 2, 2019, 7:25pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/9 "2019-05-02T19:25:38Z")

</div>

How about:

- Switch is off.
- A button is pressed.
- Automation 1 (turns switch on if off) and Automation 2 (turns switch off if on) are found to use that as a trigger.
- Automation 1’s conditions are checked. The switch is off so it is eligible to run.
- Automation 2’s conditions are checked. The switch is off so it is not eligible to run.
- Automation 1 runs and turns the switch on.

---

<div class="post-metadata">

**Author:** ![Steerpike](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@Steerpike](https://community.smartthings.com/u/Steerpike)\
**Post date:** [May 2, 2019, 7:31pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/10 "2019-05-02T19:31:52Z")

</div>

Sorry, I don’t quite follow what you are saying. Are you suggesting a different way to implement, or just speculating about how the ‘engine’ may be running things behind the scenes?

ETA - never mind, I see that you are speculating about how the engine works, and your interpretation makes sense.

ETA 2 - but … if I had two automations as follows - 1) turn light on if Monday and 2) turn light on if Dark, there will be times when both could be true, and so the engine would have to be smart enough to say ‘evaluate automation 1 and set it eligible’; ‘evaluate automation 2 and set it to eligible’; run automation 1; run automation 2. In other words it needs to stack up the various evaluations, then execute all eligible automations. Certainly do-able, but I could imagine rules / limitations about how this works!

---

<div class="post-metadata">

**Author:** ![Steerpike](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@Steerpike](https://community.smartthings.com/u/Steerpike)\
**Post date:** [May 2, 2019, 8:21pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/11 "2019-05-02T20:21:46Z")

</div>

Following on from this ‘desire’ to fix toggle actions, I found this thread with some custom ‘Smart App’ code.

> [@Toggle with Smart Lighting](https://community.smartthings.com/t/toggle-with-smart-lighting/109307/10):
>
> could you share that SmartApp?

The code as presented in that thread wouldn’t save but once I added ‘author’ and some icon url statements, I got it to save. But when I try to run (use, configure to a light) that SmartApp, it asks me for the ‘button’, and I choose my “Aeon Wallmote”, but it doesn’t ask me which button within the wallmote to use. Using a built-in SmartApp, I am asked to choose the button and when I select the WallMote, it then asks me for the specific button number. I would have thought that part would be outside of my responsibility (to realize this is a multi-button device and thus to ask for specific button).

Any thoughts on what I need to do to fix this? The code looks like this currently (borrowed heavily from the version in the referenced post):

(how do I paste in code? 🙂 … I’m familiar with using tags in other forum tools but I don’t see an equivalent here) - GOT IT - just use the good old fashioned ‘code’ tags even though there’s no button at the top for it 🙂

```auto
definition(
    name: "Toggle Light Button",
    namespace: "personal",
    description: "This app create a toggle button for light switch",  
    category: "Convenience",
    author: "(me)",
    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("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()
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![orangebucket](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/orangebucket/32/81590_2.png) [@orangebucket](https://community.smartthings.com/u/orangebucket)\
**Post date:** [May 3, 2019, 8:58pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/12 "2019-05-03T20:58:28Z")

</div>

Thinking about it again bringing in my rudimentary understanding of SmartApp programming, perhaps a more likely model is actually that each automation subscribes to the switch events and maintains an ongoing enabled/disabled status ready for when the button press event comes along.

---

<div class="post-metadata">

**Author:** ![Steerpike](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@Steerpike](https://community.smartthings.com/u/Steerpike)\
**Post date:** [May 15, 2019, 5:16pm UTC](https://community.smartthings.com/t/can-i-use-a-button-to-toggle-a-device-on-and-off-can-i-use-conditions-in-scenes/94815/13 "2019-05-15T17:16:18Z")

</div>

Just to let everyone know, once I updated the firmware on my Leviton dimmer switch, the toggle action has been working perfectly. So now, I can press buttons on my ‘Aeon Wallmote’, press the on/off buttons on the device itself, and/or send on/off commands from the phone app, and they all work, and the buttons on the wallMote really do ‘toggle’ - a single press always flips the light to the opposite state, no matter how it got there. So I’d say this approach is very sound!
