Graph with stored data

Hi everybody,
I'm pretty new to NodeRed. I have a problem with making graph with data stored. My data are in Json format like :{"t":"2018-12-12T15:00:00.000Z","temp_in_cooling":"320.00"}. i Can make graph using directly these data by means of $.feeds.{"x":t, "y" :temp_in_cooling} with the exchange node. In this way i get an array of object (objects are x and y}. The problem is that i have so many others data for each time stamp that i need to process before (i have to make some moltiplications and if statement, so with a for loop i convert, the data from string to numbers and i do my operations and finally i gat an array. but i can't display that array in a graph, i guess i'd have to convert that array in a array of object or something like that.

This what i get if i create an array of number for processing the data:

Can anybody help me please?
Thank you!

First thing is to explore the documentation Chart with stored data
Format of the data to be feed to chart must be done correctly. So investigate, try and ask when you cant understand something.

Ok, i know which format i should use, the point is that i do not understand how to convert the format i have to the one i have to have to feed the chart! I should switch from number array to 'object' array? Or? I've tried everything..
Thank's

Show us what have you tried. Share the function or flow where you try to manage your data into correct format for chart.
Also, there is many discussions have been done about same topic. Have you searched and read them? Many of them have almost ready to use solutions

1 Like

Hi everyone. Can you help me please?First i am sorry for my english. :slight_smile:

I would like to create a graph from my "smart home unit Athom" Homey. I successfully create a json file. but i have a trouble with reading of this file to graph. Can you help how can i do it or where is my issue? Debugger gives me a error: "ReferenceError: data is not defined (line 9, col 42)".
Thanks for your help.

Here is my nodes:

[{"id":"57fda489.419f3c","type":"mqtt in","z":"dc26b425.e05be8","name":"coffeeplug/measure-power","topic":"homie/homey-5ac5f38e42b2c4296e150d77/coffeeplug/measure-power","qos":"2","datatype":"auto","broker":"f18ffc79.bc2648","x":260,"y":600,"wires":[["c44b087.0631e78","4aee7d24.6bc194","73880317.157a24"]]},{"id":"73880317.157a24","type":"function","z":"dc26b425.e05be8","name":"Modify Payload","func":"var today = new Date();\nvar date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate()+',';\nvar time = today.getHours() + \":\" + today.getMinutes() + \":\" + today.getSeconds();\nvar dateTime = date+' '+time;\nvar data=[];\nvar series=[\"Data\"];\nvar labels=[\"Labels\"];\nvar values = ({\n    \"dateTime\": dateTime,\n    \"payload\": msg.payload,\n}); \n\nmsg.payload = [{series,values,labels}];\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":590,"wires":[["c6ec3305.53a418"]]},{"id":"c6ec3305.53a418","type":"json","z":"dc26b425.e05be8","name":"","property":"dateTime,payload","action":"","pretty":false,"x":950,"y":590,"wires":[["5ba76edf.d9686"]]},{"id":"5ba76edf.d9686","type":"file","z":"dc26b425.e05be8","name":"","filename":"/home/pi/logs/test.log","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":1120,"y":590,"wires":[["3b46a832.6825a"]]},{"id":"3b46a832.6825a","type":"change","z":"dc26b425.e05be8","name":"Data Array","rules":[{"t":"set","p":"data","pt":"msg","to":"$.values.{\"dateTime\":dateTime, \"payload\":data}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1320,"y":590,"wires":[["8983f082.d731d8"]]},{"id":"8983f082.d731d8","type":"function","z":"dc26b425.e05be8","name":"Setup payload","func":"series = [];\npayload = [];\nlabels = [];\n\nseries.push(\"Data\");\npayload.push(\"Values\");\nlabels.push(\"Label\");\n\nmsg.payload = [{\"series\":series,\"Values\":data,\"labels\":labels}];\n\nreturn msg;","outputs":1,"noerr":0,"x":1510,"y":590,"wires":[["e8d22e12.54e158","b723c505.8152a8"]]},{"id":"e8d22e12.54e158","type":"debug","z":"dc26b425.e05be8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1730,"y":540,"wires":[]},{"id":"b723c505.8152a8","type":"ui_chart","z":"dc26b425.e05be8","name":"","group":"2cdaed12.b6920a","order":3,"width":0,"height":0,"label":"payload","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"","dot":true,"ymin":"","ymax":"","removeOlder":"1","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":1710,"y":610,"wires":[[]]},{"id":"f18ffc79.bc2648","type":"mqtt-broker","z":"","name":"Homey MQTT","broker":"192.168.1.4","port":"1883","tls":"","clientid":"","usetls":true,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""},{"id":"2cdaed12.b6920a","type":"ui_group","z":"","name":"Coffee","tab":"57d4c334.8436d4","order":5,"disp":true,"width":4,"collapse":false},{"id":"57d4c334.8436d4","type":"ui_tab","z":"","name":"Energy","icon":"fa-1x fa-bolt","order":3,"disabled":false,"hidden":false}]

Hi @kapusticka welcome to the forum.

First things first, in order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```

You can edit and correct your post by clicking the pencil icon.

See this post for more details - How to share code or flow json

Pretty much as the error says, data is undefined yet you attempt to assigndata to "values"

If you are passing data into the function then it's probably in msg.data or msg.payload.

Add a debug node (set to show complete message) BEFORE the function where the error occurs. Have a look for where your data is.

Ps, as you are new to node red, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

i have been correct my "code". if you can, can you edit my code and post node to this forum? thanks a lot.

Did you add debug node?

Show us your data in the debug message.

I can't see any data in those messages. Where is the graph data? I assume you are sending the graph data into the function where you are getting the error? Can you show me only the debug message for the node BEFORE

PS, please give names or numbers to those debug nodes so I can see which one is which in the debug panel. Ta.

may i send you a nodes link again? it can helps.

graph data are saved in /home/log/.test.log on my raspberry pi. in the middle of my picture

For now, I only need to see what comes out of the debug node that comes from the "data array" node

I am not at a computer so can only help you if you provide certain information.

As I said several times now...

okay. now i understand. this comes out from data array.

    1. 2020, 10:17:02node: 5fb71f4f.cde0ahomie/homey-5ac5f38e42b2c4296e150d77/coffeeplug/measure-power : msg.payload : array[1]

array[1]

0: object

series: array[1]

0: "Data"

values: object

dateTime: "2020-10-25, 9:17:2"

payload: "0.9"

labels: array[1]

0: "Labels"

As i said earler,

Proof...

here you move data into msg.data

image

so in the next function you should use msg.data

now i receive bug from payload graph "bad data injection"

Snímka obrazovky 2020-10-25 o 10.36.48

On a slightly different note, I am not convinced your chart JSON format is correct.

Did you read the built in help and this

Your chart data should look like this..

[{
"series": ["A", "B", "C"],
"data": [
    [{ "x": 1504029632890, "y": 5 },
     { "x": 1504029636001, "y": 4 },
     { "x": 1504029638656, "y": 2 }
    ],
    [{ "x": 1504029633514, "y": 6 },
     { "x": 1504029636622, "y": 7 },
     { "x": 1504029639539, "y": 6 }
    ],
    [{ "x": 1504029634400, "y": 7 },
     { "x": 1504029637959, "y": 7 },
     { "x": 1504029640317, "y": 7 }
    ]
],
"labels": [""]
}]

but you seem to be adding series Values and labels to the payload object when it should be series data and labels according to the documentation.

You should change your function node "Values" : msg.data to "data" : msg.data

still having errors. 25. 10. 2020, 11:32:38node: Setup payloadfunction : (error)

"ReferenceError: data is not defined (line 6, col 1)"

You fixed this once all ready.

Any way, as i said before....

note the missing msg. part