Scheduler and Polling quits after some minutes, hours, or days

How about a pace maker approach?

Have something reliable from outside of ST hit the endpoint and forcefully reset the schedules?

1 Like

If you’ve got some sort of *nix box, you could use something like curl to GET an endpoint that tells you whether the scheduler has died and if certain text is found, GET the reschedule endpoint. Run it periodically via cron and you’re set.

I’m sure there’s a windows equivalent, I just don’t speak that language. =)

1 Like

This has been happening to me with my two SmartApps. I switched to runEvery5Minutes and runEvery1Hour hoping that would help, but both apps only seem to work for a few days before quietly giving up.

If I’m just going to call an outside service to monitor whether or my scripts are working, it seems like it would be best just to move the whole app there


1 Like

If you’re going to do that why do you need a scheduler? Just have your app send a poll() whenever its REST endpoint is called. It will be essentially a cron proxy.

1 Like

I’d try to the “external monitor / jumpstart” method out of optimism that SmartThings scheduling will miraculously start working once it is knows it is being watched


At this point, though, I guess it may have to wait until Hub V2 takes some load off the cloud? It’s difficult to understand why the scheduler can’t queue up jobs, regardless of how long the queue gets.

We could get 2 hubs so they would give each other an occasional kick. And a third one to make sure the other two don’t kill each other.

4 Likes

I think I’d like to do something similar, except instead of remote checking the schedule, just have the remote endpoint totally replace the scheduled job altogether. The external app would hit a GET endpoint every X minutes to kick off whatever job I need done in the SmartApp. Only trouble is, I’ve not messed with the Smartthings inbound API. Any good code examples out there that are anything close to this?

1 Like

A reliable scheduler would be infinitely cleaner and “the right way” to do it. We don’t have that, though, so we’re getting creative in order to overcome the one of the platform’s longest-standing issues.

Here’s what I’m doing. Remember you’ll also need to enable OAuth.

mappings {
  path("/stamp") {
    action: [
      GET: "html",
    ]
  }
  path("/reschedule") {
    action: [
      GET: "reschedule",
    ]
  }
}

def html() {
    def result
    if (now() - state.timestamp < ((settings.interval_1.toInteger() + 5) * 60 * 1000)) {
        result = "FIRING"
    } else {
        result = "FAIL"
    }
    render contentType: "text/html", data: "<!DOCTYPE html><html><head></head><body>${result}<br><hr><br>App: ${app.name}<br>Last timestamp: ${new Date(state.timestamp)}</body></html>"
}

def reschedule() {
  log.trace("Rescheduled via web API call!")
  render contentType: "text/html", data: "<!DOCTYPE html><html><head></head><body>Rescheduled ${app.name}</body></html>"
  initialize()
}
1 Like

Thanks. It’s the OAuth piece I’m trying to think through at the moment. I can get a token manually via Postman’s tool, but I was trying to figure out a way to handle that generation through some local application or scripting that didn’t require me to host something for the URI callback. I’m really new at this, so still figuring it all out.

The docs team is currently working a new WebServices Basics Tutorial. To help you out right now, here are the manual steps you can take to get the token (once you’ve got your SmartApp published and OAuth enabled). You can do this with requests through a web browser (we’ll also add a sample Sinatra app to the tutorial show an app following these steps):

Get the OAuth Authorization Code

In your web browser, paste in the following URL, replacing the CLIENT_ID with your OAuth Client ID::

https://graph.api.smartthings.com/oauth/authorize?response_type=code&client_id=CLIENT_ID&scope=app&redirect_uri=https%3A%2F%2Fgraph.api.smartthings.com%2Foauth%2Fcallback

Once authenticated, you will be asked to authorize the external application to access your SmartThings. Select some devices to authorize, and click Authorize.

This will redirect you to a page that doesn’t exist - but that’s ok! The important part is the OAuth authorization code, which is the “code” parameter on the URL. Grab this code, and note it somewhere. We’ll use it to get our API token.

Get the API token

Using the code you just received, and our client ID and secret, we can get our access token. Paste the following into your web browser’s address bar, replacing CLIENT_ID, CLIENT_SECRET, and CODE with the appropriate values::

https://graph.api.smartthings.com/oauth/token?grant_type=authorization_code&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&code=CODE&redirect_uri=https%3A%2F%2Fgraph.api.smartthings.com%2Foauth%2Fcallback&scope=app

This should return JSON like the following, from which you can get the access_token:

{
    "access_token": "your_token",
    "expires_in": 1576799999,
    "token_type": "bearer"
}

Discover the Endpoint URL

You can get the endpoint URL for your SmartApp by making a request to the SmartThings endpoints service, specifying your access token.

In your web browser, paste the following into your address bar, replacing ACCESS_TOKEN with the access token you retrieved above.

https://graph.api.smartthings.com/api/smartapps/endpoints?access_token=ACCESS_TOKEN

That should return JSON that contains information about the OAuth client, as well as the endpoint for the SmartApp:

[
  {
  "oauthClient": {
    "clientId": "myclient",
    "authorizedGrantTypes": "authorization_code"
  },
  "url": "/api/smartapps/installations/8a2aa0cd3df1a718013df1ca2e3f000c"
  }
]

Make API Calls

Now that you have the access token and the endpoint URL, you can make web requests to your SmartApp endpoint using whatever tool you prefer.

Just make sure to preface http://graph.api.smartthings.com to the beginning of the URL returned above, and any endpoints your SmartApp exposes (e.g., /switches) to the end of the URL.

You can either specify your access token via the access_token URL parameter as above, or (preferably) use the Authorization header ("Authorization: Bearer <API TOKEN>").

1 Like

Ive been seeing the same thing too
 but i’ve noticed that it seams to happen more around certain times of the day. From 2:00pm - 2:01pm CST is the first time frame i noticed it. I kinda got me thinking that almost all the jobs are being set to run on the 0 second of the minute IE “schedule(“0 59 * * * ?”, “runStuff”)” so i started testing scheduling off the 0 second and minuet like this schedule(“57 4/8 * * * ?”, “runStuff”). This is an eight minuet cycle off the 4min 57 sec point instead of 0. So far it’s been working well but i’m still watching it.

Wow, thanks! That was super helpful. I must have just been doing something weird when passing the code back to to get the token, but I got it working.

1 Like

I think you’re right. I started to look into the scheduled job history for both my apps. It looks like it’s a roller coaster ride if the second ends with 0 while the jobs wwith off 0 seconds gets executed quickly. Attached below : MyQ is scheduled to run once a minute and iComfort running once every 5 minutes on 00 seconds.


I think that there’s plenty of events that can forcefully trigger to reset the schedules within ST. Changing of modes, sunrise/sunset. If you happen to have something constantly polling like energy meter, theoretically you can subscribe to the event and piggy back on the polling instead of relying on cron schedule.

I’m still testing with these ideas. Only time will tell if they work.

1 Like

How do you see that screen? I don’t see that in my IDE


My Locations > Installed SmartApps > (your smartapp)

Well so far it doesn’t look to resolve the issue, but it does make the execution delay a lot more consistent. Well i’m off to code up a workaround.

So i updated a smartapp that polls every 8 mins to get weather and temp info to auto restart in the case of SSDS and it had been working well till last night when both scheduled tasks died. From the looks of the logs both task tasks put up a hell of a fight till they both died and i reinitialized the app.


@Tyler @April Guys
 Whats going on here?

Cool, did not know this existed (like so many things in ST :smile:)

My outside light failed to turn off three hours after sunset. Sure enough, this page shows it simply did not fire


I wonder where it went?

1 Like