Virtual momentary switch

I use virtual switches to solve a lot of challenges. Very often I need to use them to just turn on for a second to test a rule truth, and whenever possible I set them to turn off after 1 second in the rule, but this isn't always the right choice, so I often have to make a separate rule just to toggle them off after 1 second.

@Cobra had created a virtual switch driver that does just this, but I would like to request that a feature like this be made standard for the virtual switch if possible. What I would like to see is a preference for "Turn OFF after [x] seconds" that could be saved to the device settings.


sure, can do, i need something simple to do right now...


Thanks Mike. Happy Independence Day!

1 Like

so... my coding isn't good enough for you? :slight_smile:

I need to find a bridge to jump off!



I'm sure it was fine, the crew wants it built in, what more can in say!


I find a corkscrew to be a very useful tool. I think a bottle opener comes in quite handy too. But a corkscrew with a bottle opening on the end, were talking convenience. :smiley:

Question for @mike.maxwell - Can this Virtual Momentary Capability be added to the Generic Component Switch as well? That would give access to generic pushbutton behavior.

1 Like

Was this ever implemented?

"I would like to request that a feature like this be made standard for the virtual switch if possible. What I would like to see is a preference for "Turn OFF after [x] seconds" that could be saved to the device settings."


1 Like

I'm real new to Hubitat. I'm not seeing that option, where would it be?

When using the "virtual switch" device type the first option in the device's preference section is "enable auto off" which is a dropdown to select the time delay to auto turn off the device after being turned on.



So I put this together after figuring out that Alexa needs a little delay between toggling. If "enabled" it will delay about 1 second. (updated to include refresh function which isn't needed)

 *  Copyright 2020 Bloodtick Jones
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *  Virtual Momentary Switch
 *  Author: bloodtick
 *  Date: 2020-08-24
metadata {
	definition (name: "Virtual Momentary Switch", namespace: "bloodtick", author: "Hubitat", ocfDeviceType: "oic.d.switch") {
		capability "Actuator"
		capability "Switch"
		capability "Refresh"
		capability "Momentary"
		capability "Sensor"
		capability "Relay Switch"

preferences {
    input(name:"delay", type: "bool", title: "Delay Switching Off", defaultValue: false)

def installed() {
    settings.delay = false

def parse(String description) {
    log.debug description

def push() {
	sendEvent(name: "switch", value: "on", display: false)
    sendEvent(name: "momentary", value: "pushed", isStateChange: true)
	if (delay)
        runIn(1, sendEventOff)
        sendEventOff() "${device.displayName} was pushed" + ((delay) ? " with delay" : "")

def on() {

def off() {

def sendEventOff() {
    sendEvent(name: "switch", value: "off", display: false)
def refresh() {
    log.debug "${device.displayName} called refresh"

OK talk about dusting off a post.

I have a virtual switch which I just need to toggle and go off after say 1 second, long enough for it to be acted upon.

On the Dashboard set up I have selected this virtual Switch for the DEVICE and for the TEMPLATE I have selected Momentary with the thought that what I really want to encourage is the concept that this is just a quick button depression to cause a single action.

But....I am not getting anything out of that Momentary template. It works when I make it a switch, and the timing I set up in the driver does set it back to off as intended.

Am I needing to make a virtual Button for this. Is what you have asked Mike about tied up in the behavior I am seeing out of this as I went about it?

Confused. Virtual buttons exist. If that's what you need, why doesn't that meet your need. Can you explain the requirement and what you're missing in a different way?

To be honest I went down the virtual button path first and was confused by the driver options Push and Release not seeing how it would "self release" once I went on to use this....whereas in the virtual switch I immediately saw the option to set a timed off (500ms etc).

First time needing a "Momentary" so I'll admit to not realizing where to do what and when I couldn't test this successfully at the Driver point I went with what gave me the response I need.

Released is a separate event from Pushed for devices that support it. For example, one might program a dimmer button of a Pico remote to start dimming when the down arrow is pushed, and stop dimming when it's released. If you just need to have a momentary activation, then you would just use pushed, and you ignore released (don't define anything for it). Is the 500ms timing crucial for what you need to accomplish, or would pushed work (the device, driver or application you're trying to control would need to respond to pushed which would be "ON" for less than 500ms.

1 Like

Ah, I gotcha, thanks.

Yes, this is purely virtual where upon a dashboard button press you trigger a rule to send a GET command. I wanted one triggering, no sloppy lingering on the button, just boom...go do the rule and return the button to ready.

Will go back in there and re-look because a standard switch was the wrong choice for a number of reasons including UI intuitive. This was one of those things where the button played a minor role in what I was trying to achieve so I didn't belabor it.

OK, I'm back looking at this and happened to see another thread about the topic, shouldn't have but I posted in there. May as well just reply here where we started but here's what I asked over there.