Cannot Write to Child Device Log

I'm writing a custom device driver, that is using the "Generic Component Switch" as a child device. When I try to write to the child device debug log with log.debug(), it ends up in the parent device log. Is there a way to fix that and send the log info from the child to the child device log?

There is only 1 device driver file. The child device is added this way:

def addChildren(){
logDebug("addChildren")
def devChild
devChild=getChildDevice("${device.id}:Power Switch")
logDebug(devChild)
if(devChild==null)
{
logDebug("no power switch found")
addChildDevice("hubitat", "Generic Component Switch","${device.id}:Power Switch",[isComponent:true,name:"Power Switch",label:"Power Switch"])
}
}

and the "on" event is handled this way:

def componentOn(devChild){
logDebug("componentOn(${devChild})")
switch (devChild) {
case "Power Switch":
devChild.sendEvent(name: "switch",value: "on")
break
default:
break
}
}

with this for debug output:

def logDebug(logText) {
if(debugEnabled) {
log.debug(logText)
}
}

Please wrap code in code blocks with this button image

def logDebug(logText) {
    if(debugEnabled) {
        log.debug(logText)
    }
}

I dont think you can, log.debug is not a method of the device object, so you cannot call it on the component device.

What I do in my drivers is I add the devChild.displayName in front of the log entry so you know it was for a child device. You would have to send the devChild device object, or the displayName to your function for it to work.

log.debug "${devChild.displayName}: ${logText}"

You can wrapper log.debug for the child and call it from the parent. That will force the log to show up for the child.

I assume that means you need to use a custom child / component driver? It appears they are using one of the system drivers so you can only work with what you go for those.

3 Likes

Missed that part.

Thanks for the info. I was a little concerned it might be the case that I can only write to the child device log if I created the driver myself.

I was really hoping there was an undocumented method that exposed the log object in the generic driver, but I guess not.

The events are what is important. IMO just send the debug logs from the parent device. They should only be used for debugging anyway, not full time.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.