Filter messages with a changing array size


I have an array output that varies in size depending on the data it is sending. The device sends data every 60 seconds on an occupancy count for a building. Every time the data changes, it added another item into the array, so if nobody enters, an empty message is sent, if 1 person enters over 60 seconds, the array contains one value, however if 10 people enter, there are 10 entries in the array. If 10 enter, and 5 leave, there are 15 values in the array, etc, etc.

I am looking for a way to grab the values out of the array, regardless of the index number. I am using a switch node to filter the empty values out, but if I do anything using = msg.payload[0].value, then it works for the first array item only, does that make sense? The "live" number may be vastly different to this.

If I cannot do it for each item, how would I always grab the LAST item in an array?

Have a read of: Array - JavaScript | MDN (

> x = [1,2,3,4,5,6,7,8,9]
  1, 2, 3, 4, 5,
  6, 7, 8, 9
> x[0]
> x[x.length-1]

There are lots of array functions and several ways of "walking" an array for each element.

Of course, if all you want is the count then x.length - 1 is what you want.

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