Thanks for any help you can share. I am currently a hack at the groovy language.
I'm looking for the structure definition for both "evt" that's in the handler and for the list of sensors that the user selects per the input code below.
I found this doc but it didn't give the structure. It shows "name" and "value" are parts of the evt but is there anything else? I'm looking for the device id.
My code has the following parts for example. But I get a Null pointer exception when trying to look at "$it.id" meaning the each loop items (contactSensors) don't have "id" in their structure.
Is there a eaiser way to debug this? Right now I just do the worst thing and trial and error. But is there documentation on the structure for the contactSensors, or evt value somewhere?
...
input "contactSensors", "capability.contactSensor", title: "Select Contact Sensor", submitOnChange: true, required: false, multiple: true
...
subscribe(contactSensors, "battery", handlerContactBattery)
...
def handlerContactBattery(evt) {
if (debugLog) log.debug "LowBatt: contact battery event $evt.device $evt.value"
def outletDevice = getChildDevice("LowBatteryOldActivityAlert_${app.id}")
if(isThereLowBattery(evt, contactSensors) == true) {
triggerAlarm()
} else {
// nothing to alarm here...
}
}
def isThereLowBattery(evt, sensors) {
def foundLow = false
sensors.each {
if (it.currentBattery <= lowBatteryThreshold) {
if ($it.id == 757) {
// TODO: bad device that won't go above 66 % battery ???
if (debugLog) log.debug "LowBatt: IGNORING!! battery for $it.device at $it.currentValue"
} else {
if (debugLog) log.debug "LowBatt: battery for $it.device at $it.currentValue"
foundLow = true
}
}
}
if (debugLog) log.debug "LowBatt: battery foundLow $foundLow"
return foundLow
}
But the name (if your handler handles multiple attributes--otherwise you already know) and value properties that you already discovered are probably by far the most useful.
You can also use device to get the device and then things like device.id or device.idAsLong to get the device ID (as a String or Long), or the deviceId property on the event object directly (but don't ask me the data type for that off the top of my head...).
I think you want that over id on the event object itself, if that is even a thing (not documented and probably just a value without any external meaning if so).
This sounds like a different problem, but currentValue is not a property on the event. You could use that property on the device, or even better, just the value property on the event itself--after all, the new (now current) value is what generated the event in the first place.
This is why you see "null" as the last thing in that log line.
Great feedback, you're totally right. This was part of why I was asking the original question about documentation for the structure contents.
Good news also, now my app is doing this part of the total goal correctly, and ignoring this one weird sensor that must have a hw issue as it reports 66% with two brand new batteries. Ohmic solder joint or something?