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();
}
}