TimeoutException: Execution time exceeded 20 app execution seconds

I am encountering an error message during the run of the SmartApp I am building:
java.util.concurrent.TimeoutException: Execution time exceeded 20 app execution seconds

The function which encounters this loops through an XML response from a REST call. It then creates a new device with addChildDevice for each sub node in the xml. It is about 24 addChildDevice calls. I have stripped everything I can out of the function and it’s down to as small as it can be without finding a way to split it into multiple calls.

How do I avoid this issue? Is there a way to extend the maximum execution time? Any suggestions on how I break up a loop which just does addChildDevices? I can imagine addChildDevice is a heavy operation but what I am doing is pretty standard – creating devices in response to an XML response.

I don’t want to create some kind of state so I can chunk the operation if I can help it.

1 Like

A limit is a limit. There’s no way around it.

1 Like

Sounds like your REST interface is synchronous. Any way to refactor the app to be asynchronous?

For example shift the logic to the server and use the REST callbacks to trigger smart app actions.

As @geko mentioned, this is a brick wall. You’ve got 20 seconds…ready…set…GO!

1 Like

For this portion of my smartapp I’m making a REST call to the device (which is a home automation hub) and processing the response. I need to work with what the device offers and at the moment it’s a single REST endpoint which lists the devices attached to the hub. The looping through the list and creating the devices is what is taking the time – looking like the addChildDevice is expensive (and I understand why).

I’m working on an attempt now where I just call the endpoint multiple times, each time I process only a subset of the nodes. Horribly inefficient but it keeps the time for any one response process below the limit.

Once the devices are created the device I am working with will trigger callbacks which is nice. I just need to get to the point where I have the devices and I am ready.

I appreciate the suggestion.

1 Like

Ah, I misunderstood, I get it now. I assume you will only need to create the device once? That said initial detection floods the system in your case, but after that I would think you would only be interested in new devices:

REST (gimme)
Parse XML
Got it…
Got it…
Got it…
Need It! == addChildDevice

In this case it makes sense to set a limit to say, only add 5 new child devices per call. Anything beyond that you make another call and loop back through until you’ve detected all children, after that it’s just maintenance. That’s not inefficient in my opinion, that’s managing your constraints.

1 Like

Excellent point. That’s what I’ll do. Thank you!


1 Like