New Developer trying small mod to existing code

Hello SmartApps community. I am seeking to make a small modification to an existing SmartApp.

The app in question flashes a selected switch when a selected input has been “triggered.” However, the app occasionally leaves the switch in a state other than the original. I am trying to modify the code to check the initial state of the switch, and then check again after the “Flash” code has run.

private flashLights() {
def doFlash = true
def onFor = onFor ?: 1000
def offFor = offFor ?: 1000
def numFlashes = numFlashes ?: 3
def switchattr = switches.latestValue(“switch”)
log.debug “current switch: $switchattr”

Above, I added code to check the state of the switch before flashing.

 log.debug "current switch: $switchattr"
 //Should be able to set the state of switch to swtichattr right here.
switches.on([delay: 1000])
      log.debug "current switch: $switchattr"

At the end of the code I have the above to test simply forcing the switch to the On. I do get the debug log. But the switch state is not changed and no error is generated.

 log.debug "current switch: $switchattr"
 //Should be able to set the state of switch to swtichattr right here.
def turnON(){
    switches.on([delay: 1000])
    }
      log.debug "current switch: $switchattr"

Changing the code to the above produces an error indicating definition is not expected here and to define in the appropriate place or use a block/Closure instead. Being new to Groovy, I’m not really sure what this means.

Any insight or suggestions greatly appreciated.

Ahh, another attempt at “fixing” The Flasher… :grin:
FWIW, here is the link to ST docs on Closure. IMO though, this endeavor is doomed to fail. I’ve been down this road before. It’s not that the app fails to restore state, but that cloud latency causes some of these short duration events to be “eaten.” I think the protocol-specific “delay” method described by @bravenel (below) is a better approach, and likely to be much more reliable:

For 100% reliability, consider replacing your smart switch with an Aeon microcontroller. These cost about the same as smart switches, but have a firmware controlled flash and strobe capability in addition to the normal on/off. The Aeon units can be used with a regular “dumb” switch or their own touch-panel, thus allowing you to re-purpose the smart switch being replaced.

Thanks for the insightful reply Bob!

You are correct. This is an attempt at fixing The Flasher. Or at least modifying it to operate differently. :slight_smile:

Although I am new to Groovy and SmartApps, I have worked as a LabVIEW developer in industrial test and automation for the last 15 years. My very first thoughts when testing The Flasher app was that the problems were due to cloud latency and events as you put it, “Being eaten.”

Thanks for the links and the information. :+1:

Can anyone help me please. I am trying to activate flasher after X minutes (if a door is left open…). I don’t code but can place it in the script. Can someone share ideAs?