Pass data between child apps

TL;DNR: Is there a good, easy and/or best way to pass data between child apps?

Background: I have a custom app to handle my Pico remotes, and one to handle my MagicCubes. They're all but identical aside from setup screen and button numbers. My next project will be an app to handle time based events - at time, or between time X & time Y, turn on, off, toggle, dim, brighten, and change temp. Again, essentially the same thing as the Picos and MagicCubes, just with different triggers. The Pico and MagicCube apps also allow setting a default level and temp for each remote, but I was thinking it'd be better to move that to the "time" app. That way I could set different default levels for different times, as well as not having defaults repeated between Picos and Cubes. So, rather than having a defualt level for turning on a light being tied to the PIco, each light would have i a setting that both Pico and MagicCube could see.

[I think] That would require folding all three apps into an app to rule them all, but not sure how that could/would work. If I have a child app for Living Room Time 1 , Living Room Time 2, and Living Room Pico, is there a way to allow Living Room Pico "see" the settings in the Living Room Time 1 and Living Room Time 2 child apps? I'm thinking I could maybe use State, but it would get complicated quickly. And, I can't see how Living Room Pico, Living Room Time 1 and Living Room Time 2 would be "linked", as opposed to Kitchen Pico getting the Living Room Time settings aside from just matching names? Any way to accomplish or get close to being able to do something like this, or am I just getting too greedy?

Probably didn't explain well, but this is obviously still in the "Hmmmm.... I wonder if I could..." phase. Honestly not even sure which question(s) I need to ask to be pointed in the right direction... In general, I'm thinking having the Pico, MagicCube and time branch off from the main app screen, but would still need to allow each one save independently, while being linked. Like having three different child apps...?

I can’t remember exactly how to do it but you can get the ‘child’ apps to ‘talk’ to the parent.
So ...
One parent app to cover all 3 child apps with the child apps sending to and reading from the parent app.
It’s done by using a method() on the parent to do something when called from the child
This could be setting a state.variable or returning one.

I can’t remember the commands at the moment and I’m not near my PC but I’m sure @bravenel or @mike.maxwell will tell you soon :slight_smile:

Andy

It's just

parent.myMethod()

from the child. Any method you want, and you can pass params to it. The parent can call child methods also:

	childApps.each { child ->
		child.childMethod()
	}

That could be more refined as to a specific child to call. You'd pick which one based upon it's appId, or name, or appLabel...

Could I get that broken down to the 'parent/child for dummies' version. Any working examples to look at would be great.

In my parent app, I have this:
input "msgDigit1", "text", required: true, title: "IR Code to Send - 1", defaultValue: ""

How could I get that 'msgDigit1' into the child app?

Thanks

Your child app would need a method to "receive" it from the parent. Let's say it's called myMsgDigit().

In the child you would have this:

def myMsgDigit(msgDigit) {
// do whatever with msgDigit
}

In the parent you would call it like this:

if(msgDigit1) child.myMsgDigit(msgDigit1)

The if is so you don't call it unless the value has actually been input from your input statement.

Where to come up with the specific child is the last issue. Are you going to pass this to every child or to just a specific child. If just a specific child, how do you know which one?

1 Like

Thanks Bruce,

Yes, I would like to share it with all childs created.

Then you would do this:

	if(msgDigit1) childApps.each { child ->
		child.myMsgDigit(msgDigit1)
	}
1 Like

Thank you very much! I'll play around with this and try to wrap my head around it.

For what I'm trying to do, I'm thinking I will need to pass from one child app to another child app. That possible?

Yes, but you have to use the parent as an intermediary. This is how Rule Machine does things like having one Rule set the Private Boolean of another.

You can have a child call a parent method with

parent.myMethod(params)

Then the parent can call the appropriate child. In the case of RM, the child has to pass some identification of the sibling child it wants to send a command to. Where does it get this information? From the parent, obviously. So the parent is the central exchange that holds the essential information for child to child communication.