File data from Python, send it by MQTT to chart node and plot

Hi, I would like to chart following example data which I read from a Python file, transmit it by means of JSON an MQTT to my Node-red Dashboard and chart it.

nodedata = [{"datum": 1500049458014,"tempc": 15.7}, {"datum": 1500049459014,"tempc": 25.9},
{"datum": 1500049459114,"tempc": 23.4}, {"datum": 1500049469214,"tempc": 15.1},
{"datum": 1500049470114,"tempc": 23.8}, {"datum": 1500049480214,"tempc": 17.1}]

I send it by means of MQTT to my dashboard:
js_nd = json.dumps(nodedata)
client.publish("datatoplotter"), js_nd)

In the dashboard I receive the data by means of a mqtt-in node, transfer it to a function node coded like this:

var newMsg = {};
newMsg.payload = [{
key: "temp",
values: , label: "Temp"
}];
for (i = 0; i < msg.payload.length; i++) {
newMsg.payload[0].values.push (
msg.payload[i].datum, msg.payload[i].tempc);
}
msg = newMsg;
return msg;

The output of the function node feeds the chart node.. but it doesn't work.

The debug window shows this:

23.3.2022, 21:11:56node: 46e676c23f9c6725msg : string[15]

"Bad data inject"

23.3.2022, 21:11:56node: 69fe935de3ecf2edmsg.payload : array[1]

array[1]

0: object

key: "temp"

values: array[12]

[0 … 9]

[10 … 11]

10: 1500049480214

11: 17.1

label: "Temp"

Someone can help me?
Thanks in advance,
Alf

Welcome to the forum @alfschuster

The data is not correctly formatted for the chart node. This document describes how it should be, in the section Stored Data. node-red-dashboard/Charts.md at master · node-red/node-red-dashboard · GitHub

Thanks, Colin..
I saw the mentioned example before in this forum. But how I do it? Setting up a function node which receives my data and code it into the schematic similar like in the link? How I must change the code in my function node to do this? Can you give me some hints?

  1. Fix data format already when it is created in the Python file
  2. If still needed, use the function node and code in javascript
    or
  3. use Change node(s) with JSONATA

It is a learning curve but the effort required is well worth it, you will need this knowledge also in future when you will explore Node-RED for new features

Hi Colin,
i'll try it...
Thanks!

iHi,
I followed the last hint from krambriw - formatting the data in Python, transfering be means if MQTT to the node-red chart, it works!
Thanks

1 Like

Hi,
I solved the problem to chart file data with node-red. BUT: the time stamp information in the plot is wrong.
The timestamp/data pairs send have a time gap of 1800 seconds, but the chart plort displays those pairs in a time distance of 2 seconds...
Some ideas?

what does the data look like now when it comes in from the mqtt-in node?
and what does it look like going into the chart?

Docs fuer Forum 2


like this... the timestamps are ok, it seems..

Nope, you are providing them in seconds, but they must be in milliseconds. Multiply them all by 1000.

Hi Colin,
I'll try it and report... thanks in advance...
Alf

Oh Colin, great, that works!
I love it... thanks 1000 times!
Alf

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