How to turn Virtual Switch into a push button?


(Raul Pesch) #1

Hi, I’m new to SmartThings and almost done configuring my devices & scenarios. I use Virtual Switches to trigger Lighting scenes (eg. Late Afternoon, Evening, Late Evening). Use Virtual Switches as well to change stations on Sonos. It works as expected however, the “Device Type” code is currently written as a switch (with on & off state). Since lighting scenes do not neccessarily have to be switched off, I would like to turn this into a push button which does not visually change status.

The code I used to create the new Device Type is from Juan Risso:

    metadata {
        definition (name: "Virtual Switch", namespace: "smartthings", author: "Juan Risso") {
        capability "Switch"
        capability "Refresh"        
    }

	// simulator metadata
	simulator {
	}

	// UI tile definitions
	tiles {
		standardTile("button", "device.switch", width: 2, height: 2, canChangeIcon: true) {
			state "off", label: 'Off', action: "switch.on", icon: "st.Kids.kid10", backgroundColor: "#ffffff", nextState: "on"
			state "on", label: 'On', action: "switch.off", icon: "st.Kids.kid10", backgroundColor: "#79b821", nextState: "off"
		}
		standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
			state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
		}        
		main "button"
		details(["button", "refresh"])
	}
}

def parse(String description) {
}

def on() {
	sendEvent(name: "switch", value: "on")
}

def off() {
	sendEvent(name: "switch", value: "off")
}

Any hints how to turn this into a push button?

Thanks!
Raul


(Chuckles) #2

Any reason you wouldn’t use the existing “Momentary Button Tile” device type? This is a virtual push button (momentary on switch).


(Andrew Cockburn) #3

I do this too - I have code in the smart app to turn the button off after it has done its processing. The idea wasn’t mine, there are examples elsewhere in the forum.


(Raul Pesch) #4

Great, I replaced my old code with the “Momentary Button Tile” template code and now the on/off button turned into a push button. Thanks!!


(llcanada) #5

@raulpesch can you show me your finish app I would like to see if it would work for me? thanks