Adding virtual device programmatically

Hi everyone !

I really don't like to ask such basic questions, however, since I can't find an answer for it, I'm asking for help.

I'm trying to create a virtual switch programmatically, but I'm failing miserably.

I have in my code something like this:

def childDevice = addChildDevice ("hubitat", "Virtual Switch", "ABC123", [prop1: "XXX"])

So, my questions:
a) What am I doing wrong?
b) Is there a list of virtual drivers (and its namespace)?

Thanks in advance!

Marco

The above code works verbatim for me in a test app I created to try exactly this, though you might want to specify a device name or label so a bunch of devices don't get created displaying as "Virtual Switch." If you can provide a minimal non-working example, that may help someone troubleshoot or help you figure out the problem on your own as you remove other, potentially problematic code. :slight_smile:

Also, there's no explicit list of devices, but you can get a list of driver names from the "Type" dropdown on any device page, and the namespace for all built-in drivers is "hubitat", as you are already using.

This is my original code:

//
// Install a device paired to a bridge
//
def addGrandchildDevice ()
{
logDebug "addGrandchildDevice: IN"

def deviceData = [:]
deviceData.DeviceType = "Switch"
deviceData.DeviceTypeName = "Virtual Switch"
deviceData.DeviceId = "${device.data.data.DeviceId}-2"
deviceData.Name = "${device.name} latch switch"
deviceData.DebugLoggingRequired = debugLogging    // if this device is running with debugLogging, the child device will run with it too

def deviceProperties = [:]
deviceProperties.label = deviceData.Name
deviceProperties.name = deviceData.DeviceTypeName
deviceProperties.data = deviceData
deviceProperties.isComponent = true               // the child device will be "attached" to this device

try 
{
    //logDebug "addGrandchildDevice: trying to install device with nukiId = ${nukiDevice.nukiId}"
    def deviceDNI = "${device.dni}-Latch"
    def childDevice = addChildDevice ("hubitat",               // namespace - must be the same for this app and driver
                                      deviceData.DeviceTypeName,    // typeName = driver of the child device - must have been previously loaded into this HE hub
                                      deviceDNI,                    // deviceNetworkId
                                      deviceProperties)
    // if we pass through here, it means that the device was correcly added. Let's flag it!
    logDebug "addGrandchildDevice: device with deviceId = '${deviceData.DeviceId}' and deviceDNI = '${deviceDNI}' successfully added"
    
    //nukiDevice.successfullyInstalled = true
}
catch (com.hubitat.app.exception.UnknownDeviceTypeException e)
{
    logWarn "${deviceData.DeviceTypeName}: Failed to install device with nukiId = ${deviceData.DeviceId}. Driver (${deviceData.DeviceTypeName}) not installed on this Hubitat Elevation hub; install it before attempting to run this app again."
    //nukiDevice.successfullyInstalled = false
}
catch (error) 
{
    logWarn "${deviceData.DeviceTypeName}: Failed to install device with nukiId = ${deviceData.DeviceId}. Error = ${error}"
    //nukiDevice.successfullyInstalled = false
}

logDebug "addGrandchildDevice: OUT"

}

The try block is catching the exception "com.hubitat.app.exception.UnknownDeviceTypeException".

Thanks again!

Marco

This works for me if done from an app (with minor changes to fill in some blanks that would otherwise be missing from the data). I don't have a good "minimal driver" to test this with, but perhaps something is different there.

Thank you guys.

I'll do some research and when I have a solution I'll return here.

Marco

Try changing it to:

addChildDevice ("hubitat", // namespace - must be the same for this app and driver
	deviceData.DeviceTypeName, // typeName = driver of the child device - must have been previously loaded into this HE hub
	deviceDNI, // deviceNetworkId
	null, 
	deviceProperties)

This is unrelated, but I'm pretty sure dni isn't an attribute of device so you might want to change the line below to use deviceNetworkId...

def deviceDNI = "${device.dni}-Latch"

Being the Virtual Switch's namespace "hubitat" and my app namespace "maffpt" may cause a problem?

No, I do that all the time.

Did you try the change I suggested?

First @krlaframboise, I'm sorry for taking so long to answer you - kinda busy here.

Not exactly ... this what I did:

    def childDevice = addChildDevice ("hubitat",           // namespace - must be the same for this app and driver
                                      "Virtual Switch",    // typeName = driver of the child device - must have been previously loaded into this HE hub
                                      "anyDniWillDo",      // deviceNetworkId
                                      deviceProperties)

I've used simple strings wherever possible, to make it simple. And it worked ! Why it did not work before? Well, this is for the phase 2 ...

Thanks a lot!

Marco

1 Like