Sleep() in smart app?

I am having trouble implementing a sleep(60000) in my while loop. How can I pause a loop for a while so I dont flood the API im calling?

im getting this error
java.lang.SecurityException: Invoking class script14057093772401260321749.sleep() is not allowed @ line 140

I tried forever to figure this out too.

I finally found pause(milliseconds) worked. Well, I mean it worked in the groovy language but I quickly ran into another barrier. I have a loop and I paused for just 12 seconds. However after the 4th run through the loop I got this error:

12:53:52 PM: error java.util.concurrent.TimeoutException: Execution timed out after 40 units. Start time: Thu Jul 03 16:53:02 UTC 2014 @ line 72

I finally ended up getting around the issue by being able to combine my variables and send it all in one http request. Here’s the details of my saga:

So, while pause should work as far as the groovy language is concerned, I think you might run into the same time out issue.

1 Like

Schedule your smart app to run in 60000 and quit? You’ll probably want save some state for the next iteration.

finally got an answer from ST

runIn(60, myhandler)

my whole loop ended up looking like this

def checkTime(){
	if (now() > timeToday(checkTime).time && now() < timeToday(arrivalTime).time){ 
        runIn(60, scheduledRun)
    else {
    	runIn (5*60, checkTime)

def scheduledRun() {
	log.debug "Running"

That looks too complex. Why runIn(60, scheduledRun)? Why not just call the function directly there? Also, if your SmartApp skips a bit (as it happens sometimes) it will stop working until you update it. I’d use cron to fire the app every five minutes, call the function directly and voila (unless you have something else more complex going on in your app).

I was having trouble with creating a seudo loop that didnt crash the IDE. This is the last point that I left off before code cleaning.

I’m not familiar with running cronjob on groovy, do you have a code example? I wouldn’t mind having you look over my full code since you seem to be better at DRY coding then me :smile:

Im trying to implement hue’s right now, but I dont own any and the virtuals dont seem to work.

Cron is now working in the updated code!

1 Like