Order of code execution (in simulator)?

I’m really perplexed here. I’m writing a smartapp that creates a component device. In my code which I’ve been debugging, I have a number of log.debug statements that run from 1 to 5. However, in my Live Logging, they appear in a random order. The ultimate problem is that my addChildDevice code is being run before the variables are set. Is this normal???

Here is my code (snippet) from the composite (parent) device:

            log.debug "1.  no device found? $device"
            if (group.Colr==0){
                groupType = "Monochrome"
            }
            else {
                groupType = "Color"      

            }
            def lightGroup = "Luxor $groupType Group"
            def componentName = "${group.Name}componentname".replaceAll("\\s","")
            log.info "Creating Luxor $groupType light group #:${group.Grp}, name ${group.Name}, Intensity ${group.Inten}, Color ${group.Colr}"

            log.debug "2.  values? for $lightGroup  $childMac, ${hub.id}"
			log.debug "3.  getDataValue(controllerIP) ${getDataValue('controllerIP')}    getDataValue(controllerPort)  ${getDataValue('controllerPort')}"
            def params = ["label"         : "${group.Name} basic label",
                          "completedSetup": true,
                          "data"          : [
                              "intensity": group.Inten,
                              "color" : group.Colr,
                              "group" : group.Grp,
                              "controllerType"	 : "ZDC",
                              "controllerIP": getDataValue("controllerIP"),
                              "controllerPort": getDataValue("controllerPort")

                          ],
                          "isComponent": false, 
                          "componentName": componentName, 
                          "componentLabel": "${group.Name} group label"
                         ]
            log.debug "4.  params are $params"     
			log.debug "5.  about to add child with values namespace: tagyoureit \n  lightgroup $lightGroup \n childMac $childMac \n hub.id $hub.id \n params: $params"
            device = addChildDevice("tagyoureit", lightGroup, childMac, hub.id, params)
            log.debug "6.  Light Group Added $device"

and yet here is my live logging output.

687aa7c6-86e5  3:49:02 PM: debug 3. getDataValue(controllerIP) 11.11.11.13 getDataValue(controllerPort) 80
687aa7c6-86e5  3:49:02 PM: debug 2. values? for Luxor Monochrome Group 001EC018BC5C-PergolaLights-1, 36b7cf48-53a2-447a-93fe-f704308cb841
687aa7c6-86e5  3:49:02 PM: debug 0. device is found??? null
687aa7c6-86e5  3:49:02 PM: debug group [Name:Pergola Lights, Colr:0, Inten:0, Grp:1]
687aa7c6-86e5  3:49:02 PM: debug GroupListGet response: [Status:0, GroupList:[[Name:Pergola Lights, Colr:0, Inten:0, Grp:1], [Name:Step Lights, Colr:0, Inten:53, Grp:2], [Name:Bubbler Pot Lights, Colr:0, Inten:100, Grp:3], [Name:Garage Path Lights, Colr:0, Inten:0, Grp:4], [Name:Pool Down Light, Colr:6, Inten:0, Grp:5], [Name:pergola mid light, Colr:5, Inten:0, Grp:6]]]
687aa7c6-86e5  3:49:06 PM: error java.lang.NullPointerException: Cannot invoke method addToChildDevices() on null object
687aa7c6-86e5-4adf-a1ce-653ff64913a0  3:49:02 PM: debug 5. about to add child with values namespace: tagyoureit 
lightgroup Luxor Monochrome Group 
childMac 001EC018BC5C-PergolaLights-1 
hub.id 36b7cf48-53a2 
params: [label:Pergola Lights basic label, completedSetup:true, data:[intensity:0, color:0, group:1,     controllerType:ZDC, controllerIP:11.11.11.13, controllerPort:80], isComponent:false,     componentName:PergolaLightscomponentname, componentLabel:Pergola Lights group label]
687aa7c6-86e5  3:49:02 PM: debug 1. no device found? null
687aa7c6-86e5  3:49:02 PM: debug 4. params are [label:Pergola Lights basic label, completedSetup:true, data:[intensity:0, color:0, group:1, controllerType:ZDC, controllerIP:11.11.11.13, controllerPort:80], isComponent:false, componentName:PergolaLightscomponentname, componentLabel:Pergola Lights group label]
687aa7c6-86e5  3:49:02 PM: info Creating Luxor Monochrome light group #:1, name Pergola Lights, Intensity 0, Color 0

Here is another output that returns an error on addChildDevice even though my logs have not all outputted.

2fecbf4a-b112-426a-9535  4:14:37 PM: error java.lang.NullPointerException: Cannot invoke method addToChildDevices() on null object
2fecbf4a-b112-426a-9535  4:14:34 PM: debug 3. getDataValue(controllerIP) 11.11.11.13 getDataValue(controllerPort) 80
2fecbf4a-b112-426a-9535  4:14:34 PM: debug 2. values? for Luxor Monochrome Group 001EC018BC5C-PergolaLights-1, 36b7cf48-53a2-447a
2fecbf4a-b112-426a-9535  4:14:34 PM: debug GroupListGet response: [Status:0, GroupList:[[Name:Pergola Lights, Colr:0, Inten:0, Grp:1], [Name:Step Lights, Colr:0, Inten:53, Grp:2], [Name:Bubbler Pot Lights, Colr:0, Inten:100, Grp:3], [Name:Garage Path Lights, Colr:0, Inten:0, Grp:4], [Name:Pool Down Light, Colr:6, Inten:0, Grp:5], [Name:pergola mid light, Colr:5, Inten:0, Grp:6]]]
2fecbf4a-b112-426a-9535  4:14:34 PM: debug POST /GroupListGet.json HTTP/1.1 

Is this normal? Does this only happen in the simulator?

There is only one thing I can say about the simulator…
Don’t use it!

It does not always work at all, let alone work the same as the mobile app

My advice is always to just publish the app for yourself and use virtual or physical devices to test any app you create.

1 Like

Ok… I will go that route. Bummer it isn’t reliable.

And I just did publish/try my code. I still see the logs in the wrong order, but this time my code continues to add additional child devices even though the first one fails. I guess more work to be done…

thanks very much @Cobra.