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

**URL:** <https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258>\
**Category:** Dashboard\
**Created:** [23 March 2022 20:51 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258 "2022-03-23T20:51:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [23 March 2022 20:51 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/1 "2022-03-23T20:51:31Z")

</div>

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:56[node: 46e676c23f9c6725](http://127.0.0.1:1880/debug/view/view.html#)msg : string[15]

"Bad data inject"

23.3.2022, 21:11:56[node: 69fe935de3ecf2ed](http://127.0.0.1:1880/debug/view/view.html#)msg.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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 March 2022 21:18 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/2 "2022-03-23T21:18:49Z")

</div>

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](https://github.com/node-red/node-red-dashboard/blob/master/Charts.md)

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [23 March 2022 21:47 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/3 "2022-03-23T21:47:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [24 March 2022 07:16 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/4 "2022-03-24T07:16:39Z")

</div>

> [@alfschuster](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [24 March 2022 18:30 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/5 "2022-03-24T18:30:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [24 March 2022 19:50 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/6 "2022-03-24T19:50:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [26 March 2022 19:56 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/7 "2022-03-26T19:56:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 March 2022 20:14 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/8 "2022-03-26T20:14:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [26 March 2022 20:16 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/9 "2022-03-26T20:16:37Z")

</div>

![Docs fuer Forum 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32cab67cee9556bcc7d087f20c2fe0af7e3d9e09.jpeg)

 ![Docs fuers Forum 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6bcbfa66e64af854c8757548d5cab2091572687e.jpeg)  
like this... the timestamps are ok, it seems..

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [26 March 2022 20:36 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/10 "2022-03-26T20:36:37Z")

</div>

![Docs fuers Forum 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a9c6d8eb21a45de3595b7dac49ba1d106484139e.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 March 2022 20:47 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/11 "2022-03-26T20:47:51Z")

</div>

> [@alfschuster](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [26 March 2022 21:06 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/12 "2022-03-26T21:06:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alfschuster](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@alfschuster](https://discourse.nodered.org/u/alfschuster)\
**Post date:** [26 March 2022 21:12 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/13 "2022-03-26T21:12:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 April 2022 21:12 UTC](https://discourse.nodered.org/t/file-data-from-python-send-it-by-mqtt-to-chart-node-and-plot/60258/14 "2022-04-09T21:12:22Z")

</div>

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