Node-Red ver 3.02
Hi all, I have some incoming MQTT sensor data, and I'm trying to plot the data onto the line chart node.
I'm having partial success however, the lines are not advancing on the chart.
This is the data I have coming in.
msg.payload : Object
object
Time: "2023-05-05T17:08:19"
ADS1115: object
A0: 17682
A1: 17684
A2: 17683
A3: 17683
This is the unction node I'm using
// Get the last timestamp from the flow context
var lastTimestamp = flow.get("lastTimestamp");
// If the last timestamp is not set, set it to the current time
if (!lastTimestamp) {
lastTimestamp = new Date().getTime();
flow.set("lastTimestamp", lastTimestamp);
}
// Format the data for the chart
var payload = {
series: ["A0", "A1", "A2", "A3"],
data: [
[], // A0
[], // A1
[], // A2
[] // A3
],
labels: []
};
// Add the new data point to the payload for each channel
payload.data[0].push({ x: lastTimestamp, y: msg.payload.ADS1115.A0 });
payload.data[1].push({ x: lastTimestamp, y: msg.payload.ADS1115.A1 });
payload.data[2].push({ x: lastTimestamp, y: msg.payload.ADS1115.A2 });
payload.data[3].push({ x: lastTimestamp, y: msg.payload.ADS1115.A3 });
// Add the timestamp to the chart labels
payload.labels.push(lastTimestamp);
// Update the last timestamp in the flow context
flow.set("lastTimestamp", lastTimestamp);
// Set the payload for the output message
msg.payload = [payload];
// Return the output message
return msg;
The result is that the data gets to the chart but the x axis is not moving forward and remains on the left.
I assume that this is happened because the data is not retained?
Any help would be greatly appreciated.