addChildDevice (semi) failures?

Has anyone else using addChildDevice seen issues where a call to that method seems to work, but later calls to getChildDevice() or getChildDevices() report that the SmartApp has 0 children? It doesn’t happen all of the time, only sometimes, but I can’t figure out any pattern in when it works and when this strangeness happens. The device is still created successfully, it just isn’t a child of the SmartApp. Any ideas?

Here is some logging from a case where this happened. You can see a child device being created, then shortly after, I call getChildDevices() and find 0 devices:

8:04:31 PM PDT: debug child devices: []
8:04:31 PM PDT: debug getChildDevices(false), children=0
8:04:30 PM PDT: debug creating child device 0A0001DB:D997

What’s your addChildDevice() syntax? What does it return? Where are the calls to getChildDevices() and addChildDevice() in relation to one another?

The call looks like this:

def d = addChildDevice("com.obycode", "obything Music Player", childId, location.hubs[0].id, [name:"obything Music Player", label:childName, completedSetup:true])

It returns a valid device as expected. I then call getChildDevices() after a few other operations, within the same function. It makes me think that addChildDevice is an asynchronous operation and I am calling getChildDevices before it has completed, but in the case where the new child does not show in the list, it does not show up later either; it never gets added.

By chance is the device attaching itself to a session in the IDE?
You could also do a runIn(60,myAddChildMethod) to give things time to settle in…

I don’t think so. I’m not running in the simulator if that’s what you mean.

Do you mean to add a delay before doing the other stuff, after adding the child? I could try this, but I’d still really like to know what is going on.

I’ve been having issues where the create child device call works as expected, but the device only gets installed to the IDE under a session for the device type, this isn’t related to your issue, just a curiosity on my end.
The suggestion on the runIn being to mitigate any asynchronous issues that you could be facing.
Using runin would give the app time to fully install (if that’s the issue), before the child device gets created.

1 Like

I see several strange things in regards to this:

a) calling getChildDevices() or getAllChildDevices() puts a debug message into the IDE log

  • getChildDevices() puts 8:04:31 PM PDT: debug getChildDevices(false), children=2

  • getAllChildDevices() puts 8:04:31 PM PDT: debug getChildDevices(true), children=2

Any reason that calling this built-in method always give debug messages?

What is the difference in the “true” vs. “false” in the debug messages?

@Jim ?

I’m not sure about the debug messages always being present, but the boolean argument specifies whether to also include virtual devices, as documented here: http://docs.smartthings.com/en/latest/ref-docs/smartapp-ref.html?#getchilddevices