Spawn or Instantiate a Virtual Device in SmartApp?

If this has been covered or is documented, please just point me in the right direction. I’m not getting anything from searches using the terms I can think of. . . .

I’m thinking about an application where it would be nice if a SmartApp could spawn/instantiate a virtual switch. I would code the SmartApp to respond to this switch appropriately (maybe an on-off switch for the app if you wanted to be able to manually disable it without having to uninstall and then re-install, for example)

Ideally, for ease-of-use, the SmartApp would do the following automagically:

  1. Spawn or instantiate the virtual switch with predetermined labeling/naming or, perhaps, even custom graphics. So, when it showed up in the things view, the user would recognize it as part of the SmartApp they just installed.

  2. Subscribe to the events coming from this specific switch without the user having to select it.

  3. Maintain some sort of linkage to the SmartApp so that if the SmartApp is uninstalled, the virtual switch is also deleted automagically

So, is this contemplated and/or supported? If so, is there documentation, samples, threads for me to review on the subject?

What you are describing is called a child device and the SmartApp is called a Service Manager.

http://docs.smartthings.com/en/latest/cloud-and-lan-connected-device-types-developers-guide/building-cloud-connected-device-types/building-the-service-manager.html

http://docs.smartthings.com/en/latest/cloud-and-lan-connected-device-types-developers-guide/building-lan-connected-device-types/building-the-service-manager.html

1 Like

From my previous research it is not possible to create a child device that is not of your own name space. I.e, you will not be able to create a virtual switch which is a standard ST virtual switch. However, if you clone the device type and call is something else, you might be able to create your own device with your own Smart App.

I never actually got to try this. I never got a response from ST devs either, but if you achieve something, please let us know.

@AaronZON,

As stated above, this is a classic “Service Manager” and “Child Device” scenario type setup. If setup correctly, ST will handle the relationship and addition/deletion of devices tied to the Manager. So if you delete the manager (Parent), the devices (children) will be deleted. Also if you delete the last child device, the manager will be deleted (new since latest update).

All kinds of functionality can be created/instantiated with the Service Manager and virtual/real devices. One in particular is creating mutual inclusivity/exclusivity between child devices and/or device their states. Think of something like additive modes. “I’m home”, “it’s dark” and “it’s Monday” so “turn on Football game”. While many are trying to get modes to do that, States of one or more virtual devices can report to the service manager which then brokers that data and can decide when to take some kind of action. What’s really nice is that you can bury a state in a virtual switch and therefore turn it on/off like a switch. I can go on and on, but I think you get the point.

HTH

2 Likes

Ok, but how do you actually do that?

When I tried to instantiate a virtual switch of type “On/Off Button Tile” from my SmartApp, there was an exception. Mind you this was a while ago.

Do you have a code example?

I was toying with the idea of a SmartApp holding a bunch of modes/states, just like you described, and then I got sidetracked with ActiON Dashboard.

Thanks for these seeds, @notoriousbdg, @625alex, and @wackware . I am more or less familiar with a Service-Manager as a thing required for LAN or cloud-connected devices. What I’m seeing in the docs seems to be having to do with devices discovered on a LAN or cloud and then creating child devices from them. I’m not seeing how this can be applied to a regular SmartApp spawning a child device that is a virtual switch. I don’t see anything in the docs describing methods available to a SmartApp to create child devices. Not that it doesn’t make sense - it does - It’s just not obvious from what is documented how you would actually do it.

Are there examples of SmartApps creating child devices independent of any LAN/cloud discovery process? Reviewing some specific examples may help, however, it may be that this is just a bit beyond my capabilities at the moment . . . I might just have to swing back to this once I get a little further up the learning curve ;-).

@625alex,]

The device must be in the same namespace as the service manager.

1 Like

Hi @AaronZON. You can take a look at my AlarmThing Manager SmartApp as an example. It is creating virtual devices to represent the zones in an alarm system (connected via the ST shield). https://github.com/obycode/DSCAlarmSmartThings/blob/master/AlarmThingManager.groovy

3 Likes

Check out https://github.com/cooperglee/MultiSwitch-device-to-Multiple-Switches from @cooperglee for an example using a virtual switch that isn’t dependent on LAN or cloud.

All I have is LAN stuff right now but it shouldn’t matter. Look in my github at the Quirky stuff and see if you can see past the LAN goo.

I’m working on an upgrade to the “Scene Machine” app called “Mo-Scene Machine”. I’ll add a section to do what you’re asking for.

1 Like

Excellent! I can work with this. Much appreciated. I will dig into the examples provided and see if I can sort this out. Seems like what I need should be here. Thanks again!

No formal training here so I’m just feeling my way through this. . . .

I found this in the some of the example code suggested:

def addedvt = addChildDevice("ms_w_vts", "vTile_ms", ddni(vt), null, [name:vt, label:label, completedSetup: true])

Seems like addedvt is a new device object being created by the constructor addChildDevice(). "ms_w_vts" is the namespace and "vTile_ms" is a custom device type in that namespace. Correct so far?

I’ve seen documentation for Java constructors. It seems like addChildDevice() must be a ST custom constructor. Right? Is there documentation of this somewhere?

Correct. The vTile_ms device type is also available at the link I sent which makes it easier to see the relationship between the app and device.

The only documentation I have found is what I linked to originally. There are a couple other posts on addChildDevice on the forums that have a little more info…

2 Likes

For a little more explanation the ddni(vt) is actually calling a process to create the Device Network Id (DNI) for the new device - it is its unique identifier within your hub and you can see it when you list your devices. It is considered “good ST practice” to have the first part of the DNI be the “parent app.id” so that the children devices are “related” to their parent. So the particular process ddni(vt) is creating a string with app.id + “/” + newdevice.id for the DNI…

is for which “Hub” the device is being created on. You can lookup up the hub and specify it, however, it auto-populates for the current “Hub” if it is left null I have not found much use for this at this moment, however, it looks to me like this is where you can specify “Virtual” and have a device-type that doesn’t actually show up in “things”…

name is the “hidden” name which is used within ST and for the parent/child relationship to communicate, label is what the user will see as the name and can also be set through preferences and changed by the user, and completedSetup determines whether or not the device will show up with a blue “+” in things and need user interaction prior to use.

Hope that provides some additional clarity

7 Likes

Great info here. I’ll improve my apps with this new knowledge! Thanks for sharing.

3 Likes