Limit notifications code

can someone explain this syntax?

// Don't send a continuous stream of text messages
def deltaSeconds = 5
def timeAgo = new Date(now() - (1000 * deltaSeconds))
def recentEvents = accelerationSensor.eventsSince(timeAgo)
log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaSeconds seconds"
def alreadySentSms = recentEvents.count { it.value && it.value == "active" } > 1

if (alreadySentSms) {

         bla bla bla

        }

I understand the first 2 dev lines and the log.trace, but on the next def line that defines alreadySentSms, I have the following questions:

What is ‘it’? The object isn’t defined anywere I can see.

What exactly is that line counting?

What is “active”?

Just does not make sense. And, in testing in the Simulator, it never seems to return a value that would make it 0.

The code is from the sample “It Moved” app.

You can see it all here: http://support.smartthings.com/entries/21605665-It-Moved

@dukiedog – Basically, what’s happening is that the app retrieves a collection of all events for the configured acceleration sensor in the time window, then it counts the number of events in the collection whose value is “active” by using the .count method. The count method basically iterates through the collection and runs the comparison code - “it” is the default object name for the current object in the collection. So:

def alreadySentSms = recentEvents.count { it.value && it.value == “active” } > 1

Would set alreadySentSms to true if there are two or more ( > 1) events that have the value of “active”, and false if there is only 1 event with the “active” value. Acceleration sensor events have either an “active” or “inactive” value.

Thanks Dan.

That’s what I thought. The implementation I see in regards to the temp sensor is going to cause problems with this. The code I’ve seen assumes that the temps vary slowly and produce few events over time. I can easily see this to not be the case. Imagine a fire or other such heat source. You will get a very steady change in temp, but if you use the following (which I see in virtually all the temperature related apps) will produce FEWER messages:

def timeAgo = new Date(now() - (1000 * 60 * deltaMinutes).toLong())
def recentEvents = temperatureSensor1.eventsSince(timeAgo)
def alreadySentSms = recentEvents.count { it.doubleValue > tooWarm } > 1

In effect, this will trigger a new message not over time, but over quiet time. If the temp is steadily going up, the events will trigger often. Now, on a cooling side it might be more realistic, but still, if you have a sensor that was in the sun and suddenly a cold rain starts, it would trigger a steady stream of events.