How to create messages from an array with a variable number of elements

Hi, I have a payload which contains Smart meter readings in sub-arrays in the payload.data array. The payload.data array can contain a variable number of sub-arrays, depending on when the last update took place. I am trying to find a way to extract the data in the sub-arrays so I can create separate MQTT messages for each reading to send to my Influx database.
If I was able to find out how many sub-arrays were in the payload.data array, I could cycle through them to create a message for each. I've searched for a solution and read this link JavaScript Array length Property (w3schools.com) but I'm unable to work out how to use that information for my purposes.
On the other hand, there could be a better solution from the one I've been failing to develop. Any help is appreciated.

Sorry, meant to include a screenshot of the deebug

Well since you can get the number of elements in the array, you can now use a loop to walk thru each element of the array - REMEMBER the first element of the array is [0] so looking at your debug, the third element in the array is referenced with [2].

Give it a try and if you have an issue show the flow you have created.

Note also, when using a function node, you can add node.warn('x=' + x) or node.warn(y) and those will be displayed in the debug side panel.

No need. Use a change node, move payload.data to payload then pass it through a split node. You will see a message for each array element in the data. Feed that to your database.

You might wish to put a delay node (set to rate limit mode) after the split/before the database node

@zenofmud Thanks for the response but the root of the question was that I was unable to find a way to count the number of sub-arrays.

@Steve-Mcl I will give your solution a go as it is using built-in nodes and is fairly easy to understand, but for future reference, I'd still like to know how to find the number of elements in an array from within a node red function.

It's more the "node-red way"

It's simply Array.length


In a function node...

const arr = msg.payload.data; // the data array
const len = arr.length;
node.warn(len);
msg.payload = len;
return msg;

Thanks for the answer to the length of array question, simple when you know how.
The split node is asking for a character to split the payload on but I have no idea what settings I would use to split after each object. Can you enlighten me?

Since you are splitting an array, concentrate on the Array part...

This is also explained in the built in help...
image

In other words - just leave it completely default.

example...

Thanks for the pointer. The arrays are of variable length. The first part is a UTC timestamp which is consistently 9 digits but as the second part is a float, it can have one or two digits before the decimal point and from three to thirteen digits after.

The length makes no difference to the split node. If that's what you're asking.

The arrays are not of variable length, they are all of length 2. The fact that the number may be displayed with a variable number of digits is irrelevant. The number is still a single element of the array.

Doh!! Just goes to show that it doesn't matter how much you think you understand, there's always room to better understand. I was still thinking in terms of a string rather than elements within an array. Thanks for the help.

All Number type variables take up the same space in memory in javascript. If you use the number 1.2, for example, it is actually 1.2000000000000 but when printing it the zeros get dropped off.

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