I don't know Java

I got this driver code when I wanted to communicate with Alexa:

metadata {
definition (name: "Virtual motion with Switch", namespace: "cw", author: "cwwilson08") {
capability "Sensor"
capability "Motion Sensor"
capability "Switch"
}
}

def on() {
sendEvent(name: "motion", value: "active")
sendEvent(name: "switch", value: "on")
runIn(12, off)
}

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

def installed() {
}

It looks as though when the Virtual Switch shows "on", the code sets it to "off". Right?
If I wanted to leave it in the "on" state, do I remove the "runIn(12, off)" statement?
I want Alexa to control the Virtual Switch or control it thru the Dashboard if Alexa is down.

Sort of--the on() and off() methods are just called when the "On" or "Off" commands are issued from Hubitat (whether that's you doing from the device detail page manually, an app like the Alexa integration doing it for you, or anything else). I'm not sure if that's part of what you're asking. But, yes, the runIn() call will set the switch to off (and the motion, in turn, to inactive) 12 seconds after it gets turned on.

In this driver, that feature is hard-coded; you can remove that line as you suggest, and it will stay on/active (or off/inactive, though that was never in question) indefinitely. Alternatively, you might want to look into another driver like this one by @ogiewon that can do the same but also has options to control more of the behavior:

1 Like

Thanks!
I'll comment out and try it, which is what I should've done instead of asking.
Trying to read other's code is the way I got into coding back in the 70's.

1 Like