Function to get multiple arrays

I have a Node-RED flow that uses an API request for my cycle routes. It returns a huge array up to 2000 points each for elevation, speed & grade. I would like to pull these values out and plot them onto a ride graph. Is there a way of being able to do this with a loop function as the amount will differ per trip and I don’t fancy having to pull each one out individually.

Example

image

Path for first elevation payload.trip.track_points[0].e

@JamesMcCarthy79
James, I assume that you have copied and pasted this post from https://community.home-assistant.io/t/node-red-function-to-get-multiple-arrays/200024

The post above is missing the screenshot, please edit the post using the pencil icon and add your screenshot image.

1 Like

@Paul-Reed I did indeed but felt maybe this was a better place to post my question.

1 Like

Yes, I agree :wink:

...a ride graph would need a timestamp associated with each object on the array, wouldn't it?
I don't see this in your example.

Besides, the transformation to a chart datasets can be done in a function node, using js. Lopping over the array is not a problem in there...

In that case, can you share how it's done for the OP.

1 Like

...I can share an example later, how I do that for other data from my setup.

In the example screenshot above you can see payload.trip.track_points[0].t is the timestamp in question

Ah, OK...didn't get that while browsing from my mobile :wink:

I tried and quickly modify an example I use for another case, that might work for you.
At least it should give you some hints on how this might work.
Maybe @Paul-Reed is able to comment or improve, as I am not a JS guy :wink:

let array = msg.payload.trip.track_points;
var time=0;
var elevation=0;
var speed=0;
var grade=0;

var i=0;
var chartarray = [[]];


for(let i = 0; i < array.length; i++) {
    time = array[i].t;
    elevation = array[i].e;
    speed = array[i].x;
    grade = array[i].y;
    chartarray[0][i] = {"x":time,"y":elevation};
    chartarray[1][i] = {"x":time,"y":speed};
    chartarray[2][i] = {"x":time,"y":grade};
}

var chart = [{
    "series":["Elevation", "Speed", "Grade"],
    "data":chartarray,
    "labels":[""]
}];

msg.payload = chart;
msg.topic = "trip"

return msg;

Info on how a dashboard chart works can be found here: https://github.com/node-red/node-red-dashboard/blob/master/Charts.md

1 Like

Neither am I :thinking:
Perhaps @JamesMcCarthy79 can try with his data source, and let us know if there is a problem.

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