Error in formatting data for a chart

I am trying to plot an XY chart but I keep getting this error and I am not sure how to fix it.

Schermata 2021-11-01 alle 16.56.28

This is my flow:

[{"id":"09b0f6b095ce5f87","type":"smooth","z":"55ad5ee6a30d9819","name":"","property":"payload","action":"low","count":"3","round":"","mult":"single","reduce":false,"x":380,"y":220,"wires":[["b8a0abc4a3c680ed"]]},{"id":"0be572f8a33414b4","type":"file in","z":"55ad5ee6a30d9819","name":"","filename":"/home/pi/Downloads/FullDischargeReddd.csv","format":"lines","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":250,"y":80,"wires":[["986890a84de25f63"]]},{"id":"e1d0211b1518cb2a","type":"debug","z":"55ad5ee6a30d9819","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":100,"wires":[]},{"id":"3c9bbb85c826f617","type":"inject","z":"55ad5ee6a30d9819","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":140,"wires":[["0be572f8a33414b4"]]},{"id":"986890a84de25f63","type":"function","z":"55ad5ee6a30d9819","name":"","func":"//read payload\nvar stringValue = msg.payload;\n\n//convert string to float to 2 decimal places\nvar floatValue = parseFloat(stringValue).toFixed(3);\n\n//set and return payload\nmsg.payload = floatValue;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":240,"y":220,"wires":[["09b0f6b095ce5f87"]]},{"id":"a51a94bf5c99d19c","type":"function","z":"55ad5ee6a30d9819","name":"","func":"[{\n\"series\": [\"A\"],\n\"data\": [\n    [{ \"x\": payload.Time, \"y\": payload.Current },\n    ]\n],\n\"labels\": [\"\"]\n}]","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":220,"wires":[["4a2f6c067ad4dcd8"]]},{"id":"b8a0abc4a3c680ed","type":"function","z":"55ad5ee6a30d9819","name":"","func":"msg.payload= {Time: Date.now(), Current:msg.payload};\nreturn msg;\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":220,"wires":[["e1d0211b1518cb2a","a51a94bf5c99d19c"]]},{"id":"4a2f6c067ad4dcd8","type":"ui_chart","z":"55ad5ee6a30d9819","name":"","group":"e19f5a79f1d20fa8","order":0,"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":650,"y":300,"wires":[[]]},{"id":"e19f5a79f1d20fa8","type":"ui_group","name":"Chart","tab":"8ce359da5b421c0e","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"8ce359da5b421c0e","type":"ui_tab","name":"Charts","icon":"dashboard","disabled":false,"hidden":false}]

I looked at it many times and I am not sure where I am wrong. I would appreciate some direction on how to solve this problem.
Thank you

In your function

[{
"series": ["A"],
"data": [
    [{ "x": payload.Time, "y": payload.Current },
    ]
],
"labels": [""]
}]

change "payload.Time" and "payload.Current" to "msg.payload.Time" etc.

Thanks a million. That sorted out the error. Now I need to understand why noting is showing up on the chart and nothing is coming out from that function. :frowning:
Help

Your function doesn't return anything, nor are you assigning your constructed array to anything.
Does this work?

msg.payload =[{
"series": ["A"],
"data": [
    [{ "x": msg.payload.Time, "y": msg.payload.Current },
    ]
],
"labels": [""]
}];
return msg;
1 Like

Also you are feeding one msg at a time from the file node, you need to output a array from the file node, then construct the chart object from that array.

Thanks. That was a big mistake from my side. Learning.......

I see. I am just experimenting using a txt file but in reality the data will come from another flow one by one every 100ms. How can I manage that to get a chart realtime?
Thanks a million for helping

Then all you need to do is set the value to msg.payload and set a topic, the chart node will then use the time it received the payload .

This is all new for me. Sorry to abuse.... Can you elaborate on that or give me an example?

e.g.

[{"id":"3c9bbb85c826f617","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":70,"y":3340,"wires":[["344b5ee2.2e2472"]]},{"id":"344b5ee2.2e2472","type":"template","z":"b779de97.b1b46","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1\n2\n3\n4\n5\n6\n7\n\n8\n9\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1\n2\n3\n4\n5\n6\n7\n8\n9","output":"str","x":250,"y":3340,"wires":[["93bf03ba.738338"]]},{"id":"93bf03ba.738338","type":"split","z":"b779de97.b1b46","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":390,"y":3340,"wires":[["25c875e6.b5b512"]]},{"id":"25c875e6.b5b512","type":"delay","z":"b779de97.b1b46","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"10","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":250,"y":3400,"wires":[["986890a84de25f63"]]},{"id":"986890a84de25f63","type":"function","z":"b779de97.b1b46","name":"","func":"//read payload\nvar stringValue = msg.payload;\n\n//convert string to float to 2 decimal places\nvar floatValue = parseFloat(stringValue).toFixed(3);\n\n//set and return payload\nmsg.payload = floatValue;\nmsg.topic = \"chartline1\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":220,"y":3460,"wires":[["4a2f6c067ad4dcd8"]]},{"id":"4a2f6c067ad4dcd8","type":"ui_chart","z":"b779de97.b1b46","name":"","group":"e19f5a79f1d20fa8","order":0,"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,"x":390,"y":3460,"wires":[[]]},{"id":"e19f5a79f1d20fa8","type":"ui_group","name":"Chart","tab":"8ce359da5b421c0e","order":1,"disp":true,"width":"6","collapse":false},{"id":"8ce359da5b421c0e","type":"ui_tab","name":"Charts","icon":"dashboard","disabled":false,"hidden":false}]

edited to use template and split and delay to simulate data coming in.

1 Like

Thank a million. I see the dynamics to follow now. Really appreciated.

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