How to create a Child Device: Momentary on/off tile

Hey all,

I’ve tried to research this for a number of hours now and I’m just coming up blank. I got a lot of pieces and parts, but I think I’m still missing something and I just can’t seem to get it all together.

I’m trying to write a new SmartApp that will be triggered when a user turns on a virtual on/off tile. I could write the app with the user manually creating the tile, but I’d like to do it via a virtual child device. Unfortunately I’m just not getting how it all works.

I’ve read the documentation but that seems more geared towards LAN devices.

So, the specific questions I guess are:

1.) Can this app work? That is, can I create a virtual device AND subscribe to that device as my trigger for when to run the app?

2.) What’s the command to create a child device? How is this done? Is it it’s own procedure?

3.) How to I specify the name of this device so that the user can fine/use it later?

Sorry, I know this question has come up before and I tried to read previous threads to figure it out, but I’m just not getting it.

Two examples of apps that are doing exactly what you’re looking for:


I did see those before, but they didn’t help so much, at least the first time. I looked again at Switch Mania, specifically the “sub apps” that actually do the creating and I think I might have found something.

There’s a link in the initialize part of virtual switch creator that looks like this:

def childDevice = addChildDevice("smartthings", "Momentary Button Tile", deviceId, null, [label: switchLabel])

So in theory, could I put that line in the initialize procedure and then AFTER that, subscribe to when that device was turned on?

If so, can we run down the extra data here so I know what it is?

“smartthings” = The library to look for the type?
“Momentary Button Tile” = the device type
deviceID = ???
null = ???
[label: switchLabel] = presumably the internal name of this device. This is what I’m going to subscribe too in my next line, yes?

Where do I give the device a “friendly” name so that the user knows what to look for?

Okay, did some more research…

“smartthings” = namespace. Do I need to make this the same name space as my app?

“Momentary…” = name of the device type. I’m guess just leave this as is.

deviceID = Network device ID. Not sure what this is or if I have to do anything with it.

“null” = hub, optional. Don’t think I need to change this at all.

Then the last part is the properties map. I’m assuming in here is where I give it a friendly name, but not sure of the property options/syntax.

1 Like

Okay, I’m getting closer, but I’m running into a problem. In my setup pages I have the following:

section("Button Name") {
		input "switchLabel", "text", title: "Enter a friendly name.", required: true
	}    

Just asking the user for a name for the virtual device I’m going to create. Later in my initialize() procedure, I do this:

def newTrigger = addChildDevice("sirhcb", "Momentary Button Tile", app.id+"/gnsh", null, [name:switchLabel, label:switchLabel, completedSetup: true])

The problem seems to be that when that line runs it isn’t seeing the text I entered when I setup the app and just putting ‘null’ for the name and label, which crashes the app. And ideas?

@bravenel provided me an example to this very question as well. Maybe his example will help you:
Announcing the "ST_Anything" Arduino/ThingShield Project

The device types for the virtual devices need to be installed in your IDE login. There is source code available.

Thanks… I got it working almost. It works in the IDE is I specify a name for it.

My problem here is that I’m trying to let the user specify the name of the device. But I collect that name via:

input “switchLabel”, “text”, title: “Enter a friendly name.”, required: true

I can’t seem to get the variable name into the initialize() procedure. It always shows up null.

The child app for SwitchMania is just Eric Roberts’ “Virtual On/Off Switch Creator” app (with a few edits to make it a child app and to allow for Momentary Switches). Search for his app in the IDE templates.

Well, another set back. While this works in the IDE, it fails when trying to install it from my phone. It doesn’t create the child device.

Chris, I can show you how to do this but I’m on a rental car bus. Look at Lutron Gsteeay on my Github. You need to use settings.find.

Thanks for all your help on this guys. I’m still struggling with this app but I’m not sure why. I’m feel frustrated honestly at this point and the ‘child device’ is only part of it.

I’m basically trying to re-create my Goodnight Ubi app but set it up for any TTS device and refine it to have better options and use “pages” in the preferences section.

But I seem to be failing miserably. I coded everything and it saves without issue (yeah!). I can install it in the simulator using the child device and it actually creates a device (yeah!) but here’s the problem: None of my physical devices show up in the simulator so I can’t really test it in the simulator to see if it works.

Interestingly, if I remove the pages and just put in a bunch of sections in the prefs area … the old way of doing things, then my real devices show up, but when I try to install I just a flood of errors saying all kinds of crap about unacceptable values, but no references to lines.

When I install it on my phone, it doesn’t work… it doesn’t create the child device in my device list at all.

I finally made a version that that uses an externally created virtual device, and that seemed to work… except it wasn’t sending TTS to my tablet… so I’ve got problems all over the place and not much of an idea how to tackle them.

What you have to do, is to have Live Logging running when you attempt to install it on the mobile app. Then you will see the error message, and hopefully a line number. There’s some little detail you’re missing. Just work forward from the beginning of the install process of the app. Put log.debug messages in the dynamic pages as needed to see where it’s falling down, and on what values it’s missing, etc.

Code debugging 101 to the rescue. log.debug is your friend, even for dynamic pages! :grinning:

Thanks @bravenel… I’m getting closer here.

I’ve abandon the pages (for now) to get the rest working. I’ve got just about everything going. Next step to tackle: Letting the user set the name of the virtual childDevice I’m creating.

I looked through you Lutron app and I see that is has something to do with settings.find.

Can you help with the syntax here? I’m requesting buttonName as a text variable earlier. If I want to use the variable buttonLabel in my childDevice call I’m thinking the command will be something like this:

def buttonLabel = settings.find {????}

But what goes in the braces?

Ha ha!

Okay, through a lot of trial and error, I think I got it!

def buttonLabel = settings.find {it.key == “buttonName”}

  • Settings of course is the array of data created automatically when a SmartApp is initialized.
  • Settings.find scrolls through the items in the array.
  • “it” is the individual items in the array.
  • it.key is the key record for each item. When it finds an item with the key record equal to buttonName (that’s what I called my variable), it assigns that value to “buttonLabel.”

Then in the childDevice creation process I’ll use buttonLabel.value as the label part.

1 Like

Yep, you’ve got it figured out now!! .key and .value were the breakthrough for me!

Well, after tinkering forever and playing with settings, I think I finally got it all worked out…

…only to find out that Amazon Echo apparently can’t see virtual child devices. ARGH!!

This whole app was to be used with Echo so that’s a big fail. Well, not a total fail cause I can still used non-child virtual devices. And I at least learned (sorta) how to create child devices so if I need 'em for a future project I’ll know how to do it, or at least I’ll know how to get started doing it.

Did you authorize the Echo integration to access those devices and have Alexa run a re-discover?

D’oh!!

I did run the rediscovery, but no… I kinda forgot that I’d have to authorize 'em first in the echo app. Welp, looks like I’ve got some more work to do tonight.

(Part of me was almost happy that when the childDevice didn’t show up in Echo. Using the childDevice is a lot more elegant method for the end user, but man is it a pain programming-wise to get it to play nice… at least it has been for me.)

1 Like

Yep, sure enough… when I add it to the app and then do rediscovery, it finds it.

Thanks reminder @btk