Composite Devices (Parent-Child Devices)

Hubitat now supports devices having child devices. The methods that are available are as follows:

Parent Device Methods:

ChildDeviceWrapper addChildDevice(String namespace, String typeName, String deviceNetworkId, Map properties = [:])
ChildDeviceWrapper addChildDevice(String typeName, String deviceNetworkId, Map properties = [:])

Creates a new child device and returns that device from the method call.

Parameters:

Type Parameter description
String namespace The namespace of the child driver to add as a child device (optional, if not specified it will default the the namespace of the parent)
String typeName The name of the child driver to add as a child device
String deviceNetworkId unique identifier for this device
Map properties optional parameters for this child device. Possible values listed below:

properties:

boolean isComponent true or false, if true, device will still show up in device list but will not be able to be deleted or edited in the UI. If false, device can be modified/deleted on the UI.
String name name of child device, if not specified, driver name is used.
String label label of child device, if not specified it is left blank.
List<ChildDeviceWrapper> getChildDevices()

Gets a list of all child devices for this device.

ChildDeviceWrapper getChildDevice(String deviceNetworkId)

Gets a specific child device with the device network id specified.

void deleteChildDevice(String deviceNetworkId)

Deletes a specific child device with the device network id sepcified.

Child Device Methods:

ParentDeviceWrapper getParent()

Returns the parent device when called from a child device.

6 Likes

Chuck,

Thank you! I'll give this a try with HubDuino as soon as I find some time. (Why does my job always seem to interfere with my hobby? :wink: )

Dan

3 Likes

Chuck,

Nicely done! I have a working version of HubDuino successfully using the Composite Device driver model! I need to perform some more rigorous testing before releasing, but so far it is working great!

Thank you very much,

Dan

3 Likes

@keithriley does this help with the Intermatic PE653

Absolutely! This is great news. I am working on the next major release of the PE653 DTH, converting it into a Composite Device. This eliminates the need for the deprecated Multi-Chanel SmartApp. Early testing looks good.

3 Likes

does the specifying the driver for the child work? If so how? I have been trying to port a UBS driver from ST but as I'm not a developer, I have just been bumbling through. It used you specify a route to the ST stock drivers but don't know what the HE version would be. Any help would be appreciated.

def createChildDevices(){
	log.debug "Adding Child Devices if not already added"
    for (i in 1..2) {
    	try {
        	log.debug "Trying to create child switch if it doesn't already exist ${i}"
            def currentchild = getChildDevices()?.find { it.deviceNetworkId == "${device.deviceNetworkId}-ep${i}"}
            if (currentchild == null) {
            	log.debug "Creating child for ep${i}"
				addChildDevice("Hubitat", "Virtual Contact Sensor", "${device.deviceNetworkId}-ep${i}", device.hub.id,
                	[completedSetup: true, name: "${device.displayName} (Contact${i})", isComponent: false]) //, label: "${device.displayName} (Contact${i})"
                /*addChildDevice(deviceHandlerName, "${device.deviceNetworkId}-${deviceName}${deviceNumber}", null,
         			[completedSetup: true, label: "${device.displayName} (${deviceName}${deviceNumber})", 
                	isComponent: false, componentName: "${deviceName}${deviceNumber}", componentLabel: "${deviceName} ${deviceNumber}"])*/

and

def createChildTempDevices() {
    log.debug "Creating Temperature children"
    for (i in 1..4) {
    	try {
        	//If we have a temperature reading from this sensor (1 to 4) then try to create a child for it
        	if (device.currentValue("temperature${i}") != null) {
            	log.debug "Have received temperature readings for termperature${i} so creating a child for it if not already there"
                def currentchild = getChildDevices()?.find { it.deviceNetworkId == "${device.deviceNetworkId}-temperature${i}"}
                if (currentchild == null) {
                    addChildDevice("smartthings", "Temperature Sensor", "${device.deviceNetworkId}-temperature${i}", device.hub.id,
                        [completedSetup: true, name: "${device.displayName} (Temp${i})", isComponent: false]) //, label: "${device.displayName} (Temp${i})"
                }

Not sure if this is possible so you may have to wait for an official response.

In the meantime, your createChildTempDevices() is still referencing smartthings. Also, as an alternative to using the built in virtual, you could create your own. In ST create a new driver and use the Virtual Contact Sensor as a template. Copy the code into HE and change the namespace to your own. Now you can update your component devices to point to your "custom" driver.

Yes, you are able to do this, however you have some problems with your code, you need to read the method signatures more closely to see what has changed when you are programming in our system. Also, the namespace of most of our drivers is hubitat. So for you example addChildDevice method, it should be coded like this:

addChildDevice("hubitat", "Virtual Contact Sensor", "${device.deviceNetworkId}-ep${i}", [name: "${device.displayName} (Contact${i})", isComponent: false])
1 Like

how would I point it to one of my drivers? what would i change hubitat too? They have a virtual contact sensor just not a temperature.

@chuck.schwer is there plans to have a virtual temperature driver? Thanks for your help with the code its working great now :smile:

Ideally for child devices the driver should be view only, that way they can only be forced out of or in sync though the parent device.

I have a Fibaro dual relay and use the Fibaro FGS-222 Double Relay Switch driver.
WHERE do I input your code to create the child device for the switch 2?
If I do it right, will I end up with another device in my list to use with RM?

Maybe this might help:

Sorry, but I am just learning Hubitat so bear with me.
1st I create this driver. Then I use it on my dual relay device and it will create my child devices (switch 1 and switch 2).

After doing above, do I go back and use my original driver on my relay, or do I just leave your driver in place?

This driver is for the Monoprice dual relays but it may work for your relays too. Be sure to install both drivers then on your device select just the parent driver. Then click the recreate child devices and it should create the two child switches under the parent and it will automatically use the child device driver. You can then rename the two child switches to whatever you want. Don’t change the driver of either the parent device or children.
Good luck!

Very nice! It seems to be working for my Fibaro dual relay.

Thank you

I thought this was working for my fibaro, but alas it's throwing a lot of errors.

So how do I remove these child devices so I can load a different driver for this device?

You wrote that you use FGS-222 Double Relay Switch driver.
Does it work without any modifications?

Can't get my FGS-222 to work with this driver. On/Off button doesn't trigger any actions.

Anyone knows how to set the parent device's status to ACTIVE in DTH? Is there a specific event I can return from parse() that does nothing but sets the status to active (NOP event)?

All the events from parent device's parse() are sent to child devices, therefore the parent device remains INACTIVE, even though the parent device is the one that does the heavylifting :slight_smile:

2 Likes

7 posts were split to a new topic: Help porting alarm system bridge

A post was split to a new topic: Help with temp sensor code

A post was merged into an existing topic: Help with temp sensor code