Timer Code

I’m trying to write a new app that will have 2 timers in it for cycling a device on and off. But i can’t find any info about how to do timers in Groovy. Does any have a basic setup info for doing timers. Groovy is always kicking my butt.

I take it these type of scheduling functions aren’t what your looking for?

1 Like

That might work. Thanks. Is there a way to cancel the runIn() once the timer starts if it’s not needed?


Thanks for your help.

Is there a way to see if a runIn() is running? Or is it okay to do unschedule() even if it has executed already?

In the IDE you can see when things are scheduled to run and when they ran last…
My Locations>smartapps

From there you can select the smartapp you want to check and it will show quite a bit of info on the app you select, along with the info mentioned above…

I’m talking about in the code. When the eventHandler() runs and a runIn(600, dothis) is not needed is there a way to only unschedule(dothis) if the runIn() timer is still counting down? I seen somewhere when reading that unschedule() was costly and do not want to do it if the timer is not running.

I don’t know of anyway to ask ST if it’s running, but there may be, my programming experience in groovy is limited, but you could use a state variable to keep track if you started a timer or not.

okay will see how it does and if i start having problems i will do that

There are also options for delay processing. The runin option is your best option if the task that is being run is over 30 seconds away, If it is less that that it won’t work well. Keep in mind that the run in option is a best effort and will make it’s best attempt to run at the interval, but is not 100% I had a process that was trying to run every 2 min and sometimes it ran at 2 min 3 seconds and sometimes it was as long as 4 min. The majority of the time the added time was between 3 and 6 seconds.

I seen were it recommends not using a runIn less then 60 sec so right now that is my slowest one. The delay may come in useful though. I have a feeling I am going to want faster timers in some places. If I read right this hvacstopHandler([delay: 5000]) will run that handler after 5 sec? Is there a max time a delay can be?

I use Cron Schedules for delays that run every month, and you can schedule things years out, so practically I would say no.

You may want to review rate limits. Keep in mind that delays will function a bit differently in the code as well. Delays will actually start a timer on the command on the hub and will submit all of the iterations at time of execution of the method in the smartapp. You can see that in the Live Logging. The run in will actually schedule the method execution so much time out. That means it sits dormant until it executes. Much less overhead. Think of it like if you have something you want to run every 5 seconds for 10 min. That means at time of execution you would submit 120 different timers. If you have more then one device or item involved that number grows rapidly. Depending on how your app works it may be a combination of both.