Joining Arrays


(Im Brian) #1

I have a subscribe set on init to monitor multiple contact sensors. Nothing special there. I run the following to find all currently open sensors and populate an array. That array of opened contact sensors is sent to a function:

def init() {
  state.deviceTriggers = []
  subscribe(contacts, "contact.open",  triggerContact)
}

def triggerContact(evt) {
  def open = contacts.findAll { it?.latestValue("contact") == "open" }
  triggerDevice(open)
}

Within the “triggerDevice” function, I want to take a state.devices variable and add the new device array I just defined:

def triggerDevice(devices) {
  state.deviceTriggers.addAll(devices)
}

I’m not super familiar with Groovy, but there’s something going on that I don’t quite understand. From what little I understand - I’m generating an array of device names. I’m passing that array to a function that than combines that device array with (basically) a global array - thereby creating a big array of opened devices. I’ve obviously cut out the parts I’ve deemed unimportant (timers, actually doing stuff with this state.deviceTriggers array, etc).

It appears the “state.deviceTriggers.addAll(devices)” line is throwing this error:

error Cannot get property 'id' on null object

I replaced the “devices” array variable in the .addAll call with “[1,2]” and it does appear to work. So this is hinting to me that perhaps the device array is not an array at all? Anyone with expertise here willing to shed some light?

Thanks!


(Im Brian) #2

I understand that the “open” var is a collection and I believe that state.deviceTriggers is defined as an array. Trying to explicitly set open.toArray() doesn’t appear to help.