Nesting Parent/Child Apps like Group and Scenes or Rule Machine

I apologize as I am new to the Hubitat platform. I am trying to figure out how to install a "Master" app and then install "Child" apps and have it appear in Hubitat like the Group and Scenes or Rule Machine app does?

Thanks in advance!

Can I assume you're writing a custom app? (If you're dealing with a built-in app, you're stuck with whatever structure it provides; if you're using or writing a custom app, you can certainly code it to do whatever, though if it's someone else's app, I'd beware if you ever want to upgrade it.)

If so, what you'll need to do is provide a way for a user to create a new child app. Here is a minimal example of a preference page inside an app that can do what you're looking for:

// ...
preferences {
  page(name: "pageMain", title: "My App", install: true, uninstall: true) {
    section("My Section") {
      paragraph "Hey, this is my app!"
      app name: "myChildApps", appName: "My Child App Name", namespace: "MyNamespace", title: "Add new child app", multiple: true
      input name: "mySensors", type: "capability.motionSensor", title: "Choose motion sensor(s), multiple: true
    }
  }
}
// ...

The key is the app line you see above (I included an input too so you can see that it's basically the same idea, if you're familiar with that--you would probably not need to actually include anything like that if you parent app is more or less just a container). This creates a button that will instantiate a new child app instance. As you can probably infer, this assumes you also have an app whose metadata looks something like this--or whatever you pass to app in the example above:

definition(
   name: "My Child App Name",
   namespace: "MyNamespace",      
   parent: "MyNamespace:Parent App Name", // <-- must include! (adjust as needed)
   // ....

As noted above, the new piece of information here is that the child app must also include a parent that points to the namespace and name of the parent app.

As demonstrated by Groups and Scenes (and even Rule Machine if you have multiple Rule versions in use), you can have multiple child apps (e.g., the Group child app and the Scene child app) or child app versions (e.g., different versions of Rule). These are different child apps--different entries in "Apps Code" in the case of a user app--and you can have more than one app input, one referring to each type of child app. (Rule Machine does a bit of trickery to hide old app versions if you don't have any installed, which I've also done in one of my apps and seems to work well, but i wouldn't try this if you don't need it; and most built-in apps do something to hide the list of installed child apps in the parent app UI--in all cases they'll still appear under the parent in the "Apps" list--which I don't know how to do, if it's even possible in user apps, but I can't image why this would be a problem in most cases).

Hope this helps!

2 Likes

Yes. I am trying to write a custom app that I want to share with the community. Thank you for responding and I will most certainly try this out this evening! I can't wait to start contributing to the community!

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.