Is anybody else having problems creating child devices at the moment? I find that they create fine, but they get diassociated from the parent upon creation, hence the parent can no longer send commands/updates to the children (and vice versa).
I have simplified the code to give the most simple implementation below and it still gives a problem. Can somebody verify this for me please? I have tried fiddling with tons of settings for hours but with no luck!
Basically if you press open/close on the parent then it should update the relevant child, however it seems to work on the first created child and not the second. If you look in live logging you will see the error that it cannot find the child (even though it exists), and if you press list children you will see it only finds one child.
To fix it: if you then hit remove devices (once) it will remove the working child (not the broken one - no idea why), but then you can press create devices again and it will re-create the working child and magically the broken child will start working (sometimes you need to do this a few times). - NOTE this fix only works if you have 2/3 children, if you have 4+ Ive never managed to get every child working!
/**
* Test children devices
*
***************************************************************************************
*/
metadata {
definition (name: "Test children", namespace: "cjcharles0", author: "Chris Charles") {
capability "Contact Sensor"
capability "Sensor"
command "rmvChildDevices"
command "crtChildDevices"
command "lstChildDevices"
command "open1"
command "open2"
command "close1"
command "close2"
}
simulator {
}
tiles(scale: 2) {
standardTile("contact1", "device.contact1", width: 3, height: 2) {
state "open", label: '${name}', icon: "st.contact.contact.open", backgroundColor: "#e86d13", action: "close1"
state "closed", label: '${name}', icon: "st.contact.contact.closed", backgroundColor: "#00a0dc", action: "open1"
}
standardTile("contact2", "device.contact2", width: 3, height: 2) {
state "open", label: '${name}', icon: "st.contact.contact.open", backgroundColor: "#e86d13", action: "close2"
state "closed", label: '${name}', icon: "st.contact.contact.closed", backgroundColor: "#00a0dc", action: "open2"
}
standardTile("crtchildren", "crtchildren", inactiveLabel: false, decoration: "flat", width: 2, height: 1) {
state "default", label:'Create Child Devices', action:"crtChildDevices"
}
standardTile("rmvchildren", "rmvchildren", inactiveLabel: false, decoration: "flat", width: 2, height: 1) {
state "default", label:'Remove Child Devices', action:"rmvChildDevices"
}
standardTile("lstchildren", "lstchildren", inactiveLabel: false, decoration: "flat", width: 2, height: 1) {
state "default", label:'List Child Devices', action:"lstChildDevices"
}
}
main(["contact1"]) //, "temperature1"
details(["contact1","contact2","crtchildren", "rmvchildren", "lstchildren"])
}
def crtChildDevices(){
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("smartthings/testing", "Simulated Contact Sensor", "${device.deviceNetworkId}-ep${i}", device.hub.id,
[completedSetup: true, name: "${device.displayName} (Contact${i})", isComponent: false])
}
} catch (e) {
log.debug "Error adding child ${i}: ${e}"
}
}
}
def rmvChildDevices() {
log.debug "Removing Child Devices"
try {
getChildDevices()?.each {
try {
deleteChildDevice(it.deviceNetworkId)
} catch (e) {
log.debug "Error deleting ${it.deviceNetworkId}, probably locked into a SmartApp: ${e}"
}
}
} catch (err) {
log.debug "Either no children exist or error finding child devices for some reason: ${err}"
}
}
def lstChildDevices(){
log.debug getChildDevices()
}
def open1() {
sendEvent(name: "contact1", value: "open", descriptionText: "$device.displayName (1) is opened manually")
try {
def childDevice = getChildDevices()?.find { it.deviceNetworkId == "${device.deviceNetworkId}-ep1"}
log.debug "Changing child ${childDevice} to open/inactive"
if (childDevice)
childDevice.sendEvent(name: "contact", value: "open")
} catch (e) {
log.error "Couldn't find child device, probably doesn't exist...? Error: ${e}"
}
}
def close1() {
sendEvent(name: "contact1", value: "closed", descriptionText: "$device.displayName (1) is closed manually")
try {
def childDevice = getChildDevices()?.find { it.deviceNetworkId == "${device.deviceNetworkId}-ep1"}
log.debug "Changing child ${childDevice} to closed/active"
if (childDevice)
childDevice.sendEvent(name: "contact", value: "closed")
} catch (e) {
log.error "Couldn't find child device, probably doesn't exist...? Error: ${e}"
}
}
def open2() {
sendEvent(name: "contact2", value: "open", descriptionText: "$device.displayName (2) is opened manually")
try {
def childDevice = getChildDevices()?.find { it.deviceNetworkId == "${device.deviceNetworkId}-ep2"}
log.debug "Changing child ${childDevice} to open/inactive"
if (childDevice)
childDevice.sendEvent(name: "contact", value: "open")
} catch (e) {
log.error "Couldn't find child device, probably doesn't exist...? Error: ${e}"
}
}
def close2() {
sendEvent(name: "contact2", value: "closed", descriptionText: "$device.displayName (2) is closed manually")
try {
def childDevice = getChildDevices()?.find { it.deviceNetworkId == "${device.deviceNetworkId}-ep2"}
log.debug "Changing child ${childDevice} to closed/active"
if (childDevice)
childDevice.sendEvent(name: "contact", value: "closed")
} catch (e) {
log.error "Couldn't find child device, probably doesn't exist...? Error: ${e}"
}
}