How to add a "command" to a virtual device

I am trying to create a "toggle" function for a virtual switch. I just want a simple way to toggle to the other state, depending on the current state (on or off).

I started with the information at:

But creating the toggle function didn't end up in the "commands" of the device when installed:

Here is the code. Is the issue that the capability "Switch" doesn't know to use the function toggle()?

metadata {
   definition (name: "Virtual Switch w Toggle", namespace: "mySpace", author: "myName") {
      capability "Actuator"
      capability "Switch"
   }

   preferences {
      // none in this driver
   }
}

def installed() {
   log.debug "installed()"
}

def updated() {
   log.debug "updated()"
}

def on() {
    state.foo = "on"
    sendEvent(name: "switch", value: "on", descriptionText: "${device.displayName} switch is on")
    log.debug "on()"
}

def off() {
    state.foo = "off"
    sendEvent(name: "switch", value: "off", descriptionText: "${device.displayName} switch is off")
    log.debug "off()"
}

def toggle() {
    log.debug "toggle()"

    if (state.foo == "off") {
        on();   
    } else {
        off();
    }
}

I see there is a list of device capabilities, but sadly none list "toggle". Let me see if it is called something else?

Or I could maybe hack a different capability command but that's annoying.

That worked, I added capability "DoubleTapableButton" and then renamed my toggle function to the defined value def doubleTap(buttonNumber) but as I said, that's a hack.

Do users have the ability to modify or add new capabilities?

It seems like a virtual switch would be improved if it supported a toggle() command. Things like lights are easy to see the current state if the input device is in the same room, and then you can use that simple momentary (e.g. arcade) button to toggle to the other state. And the on() off() are still there for absolute setting of course.

Maybe what I need to do is a feature request for "toggle()" in virtual switch driver?

In the "definition" area, try adding

command "toggle"

2 Likes

Right above preferences add command “toggle”

Then use custom commands without any parameters in your rule in your rule to run it

2 Likes

Perfect! Thanks both for the fast help/answer on this!

2 Likes