Cant get Line Chart to advance with time stamp

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.

image

If this is live data you need to send the 4 readings in in separate messages in payload with distinct topics
The help text gives a link to this info

here is a little example

[{"id":"8021c9877ac6b2e7","type":"inject","z":"1d319bc3005a51a2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\t{\t   \"time\": $now(),\t   \"ADS1115\": {\t       \"A0\": $round($random()*20)+17000,\t       \"A1\": $round($random()*20)+17000,\t       \"A2\": $round($random()*20)+17000,\t       \"A3\": $round($random()*20)+17000\t   }\t}","payloadType":"jsonata","x":100,"y":540,"wires":[["410a0be18dfbf73f"]]},{"id":"410a0be18dfbf73f","type":"change","z":"1d319bc3005a51a2","name":"","rules":[{"t":"move","p":"payload.time","pt":"msg","to":"timestamp","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.ADS1115","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":540,"wires":[["7d5421ca48300270"]]},{"id":"7d5421ca48300270","type":"split","z":"1d319bc3005a51a2","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":470,"y":520,"wires":[["885bff3b373217f1","fab37c67c701e98e"]]},{"id":"885bff3b373217f1","type":"debug","z":"1d319bc3005a51a2","name":"debug 294","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":540,"wires":[]},{"id":"fab37c67c701e98e","type":"ui_chart","z":"1d319bc3005a51a2","name":"","group":"2d4fe667.28f8ba","order":24,"width":0,"height":0,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":510,"y":580,"wires":[[]]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":2,"disp":true,"width":"12","collapse":false,"className":""},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Thank you soooo much, I don't think I would have worked this out myself.
It is so nice to see the community helping each other and making the world a better friendlier place.

Thanks again.

1 Like

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