Looking for structure definitions for handler "evt" and devices in Groovy

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
}

Lose the $.

1 Like

Yes:

https://docs2.hubitat.com/developer/event-object

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).

1 Like

Ok thanks both of you! I think this is helping me get there.

My log is:

app:5252023-12-11 10:24:49.214 PMdebugLowBatt: battery for [id: 757, name: Dome Motion Sensor, label: Motion Furnace Room z, deviceNetworkId: 7F, hubId: 1, parentDeviceId: null, isComponent: false, displayAsChild: false, locationId: 1, deviceTypeId: 29, zigbeeId: null, lanId: null, driverType: sys, linkedDevice: false] at null

And the log line for that is

if (debugLog) log.debug "LowBatt: battery for $it.device at $it.currentValue"

So I think I can use it.device.id which I then need to see if that's matching 757 as a string or int

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.

1 Like

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?

Anyway, Here is the logging showing the ignore.

Now I need to move to the other warning sign of a dead battery which is communications being too stale.

Over and above the documentation a simple

log.debug "${evt.properties}"

will show all of the defined entries for the event.

1 Like

Excellent!!

I added this quick hack in the initialize() function

def initialize() {
  subscribe(contactSensors, "battery", handlerContactBattery)
  subscribe(motionSensors, "battery", handlerMotionBattery)
  subscribe(waterSensors, "battery", handlerWaterBattery)
  subscribe(smokSensors, "battery", handlerSmokeBattery)

  smokeSensors.each {
    if (debugLog) log.debug "LowBatt: $it.device.properties"
  }

  log.debug "LowBatt: installation initialize..."
}

Excellent, the .properties was super useful!