Hi there,
I'm struggling with JavaScript.
Via Form-Node I'm selecting the datapoints which I want to extract from sqlite and show them in a chart.
From sqlite I receive this: Pic1&2.
Pic1:
Pic2:
After that I let run a prep' node, which transforms my msg.payload into another array (Pic3-5.jpg).
Pic3:
Pic4:
Pic5:
But I need to transform that into an array which is able to be shown at the chart-node from dashboard - structure see here (Picture6.jpg).
Pic6:
If there is a need for the .db-file, please tell me how to upload it here. It is about 3.7 MB big but not allowed (file extension .db)
But: I'm too dumb and not really familiar with javascript to rearrange the content of this array.
This is the flow:
[{"id":"dd06a909.e84d28","type":"function","z":"7783ee4a.75705","name":"SQL","func":"// This will handle any device and any attribute as long as it is in the DB\nvar Week = 604800000 ; //7 Days\nvar Day = 86400000 ; // 1 Days\nvar d = new Date();\nvar timestamp = d.getTime();\nvar fromdate = timestamp - Day;\nvar enddate = timestamp;\nvar output = [];\n\nfor (var property in msg.payload) {\n if (msg.payload.hasOwnProperty(property)) {\n //output.push({ payload: property + \" has value \"+msg.payload[property] });\n if (msg.payload[property]) {\n var parts = property.split(\"/\");\n if (parts[0]===\"period\") {\n switch (parts[1]) {\n case \"today\":\n fromdate = timestamp-Day;\n enddate = timestamp;\n break;\n case \"yesterday\":\n fromdate = timestamp-2*Day;\n enddate = timestamp-Day;\n break;\n case \"week\":\n fromdate = timestamp-Week;\n enddate = timestamp;\n break;\n }\n } else {\n output.push({ topic: \"SELECT * FROM DATA WHERE device='\"+parts[0]+\"' AND sensorname='\"+parts[1]+\"' AND timestamp >= \" + fromdate + \" AND timestamp <= \" + enddate });\n }\n }\n }\n}\n\noutput[output.length-1].complete=true;\n\nreturn [ output ];\n\n//msg.topic = \"SELECT * FROM sensor_data WHERE device='growatt' AND sensor='power' AND epoch >= \" + fromdate + \" AND epoch <= \" + epoch ;\n//return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":1020,"wires":[["c411fc78.dabd9","91e8f4b7.b873f8"]]},{"id":"9f2c80f0.25273","type":"ui_form","z":"7783ee4a.75705","name":"Data points","label":"","group":"51e1679f.558fe8","order":2,"width":0,"height":0,"options":[{"label":"Zeitraum: heute","value":"period/today","type":"checkbox","required":false,"rows":null},{"label":"Zeitraum: gestern","value":"period/yesterday","type":"checkbox","required":false,"rows":null},{"label":"Zeitraum: diese Woche","value":"period/week","type":"checkbox","required":false,"rows":null},{"label":"Device 1 / Luftfeuchte","value":"device1/humidity","type":"checkbox","required":false,"rows":null},{"label":"Device 1 / Temperatur","value":"device1/temperature","type":"checkbox","required":false,"rows":null},{"label":"Device 1 / Luftdruck","value":"device1/pressure","type":"checkbox","required":false,"rows":null},{"label":"Device 2 / Luftfeuchte","value":"device2/humidity","type":"checkbox","required":false,"rows":null},{"label":"Device 2 / Temperatur","value":"device2/temperature","type":"checkbox","required":false,"rows":null},{"label":"Device 2 / Luftdruck","value":"device2/pressure","type":"checkbox","required":false,"rows":null},{"label":"Device 3 / Luftfeuchte","value":"device3/humidity","type":"checkbox","required":false,"rows":null},{"label":"Device 3 / Temperatur","value":"device3/temperature","type":"checkbox","required":false,"rows":null},{"label":"Device 3 / Luftdruck","value":"device3/pressure","type":"checkbox","required":false,"rows":null},{"label":"Device 4 / Luftfeuchte","value":"device4/humidity","type":"checkbox","required":false,"rows":null},{"label":"Device 4 / Temperatur","value":"device4/temperature","type":"checkbox","required":false,"rows":null},{"label":"Device 4 / Luftdruck","value":"device4/pressure","type":"checkbox","required":false,"rows":null}],"formValue":{"period/today":false,"period/yesterday":false,"period/week":false,"device1/humidity":false,"device1/temperature":false,"device1/pressure":false,"device2/humidity":false,"device2/temperature":false,"device2/pressure":false,"device3/humidity":false,"device3/temperature":false,"device3/pressure":false,"device4/humidity":false,"device4/temperature":false,"device4/pressure":false},"payload":"","submit":"send","cancel":"stop","topic":"","topicType":"str","splitLayout":false,"x":130,"y":1020,"wires":[["8e09d5fa.cfe388","dd06a909.e84d28"]]},{"id":"8e09d5fa.cfe388","type":"debug","z":"7783ee4a.75705","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":310,"y":940,"wires":[]},{"id":"c411fc78.dabd9","type":"debug","z":"7783ee4a.75705","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":940,"wires":[]},{"id":"91e8f4b7.b873f8","type":"change","z":"7783ee4a.75705","name":"device","rules":[{"t":"change","p":"topic","pt":"msg","from":"device1","fromt":"str","to":"8b3790","tot":"str"},{"t":"change","p":"topic","pt":"msg","from":"device2","fromt":"str","to":"7d945d","tot":"str"},{"t":"change","p":"topic","pt":"msg","from":"device3","fromt":"str","to":"8b39b7","tot":"str"},{"t":"change","p":"topic","pt":"msg","from":"device4","fromt":"str","to":"7d9ba3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":1020,"wires":[["3f81ad79.b5ac92","a2f94e78.758ad"]]},{"id":"3f81ad79.b5ac92","type":"debug","z":"7783ee4a.75705","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":940,"wires":[]},{"id":"a2f94e78.758ad","type":"sqlite","z":"7783ee4a.75705","mydb":"8274941a.73344","sqlquery":"msg.topic","sql":"","name":"","x":710,"y":1020,"wires":[["23de8469.09a0dc","55cd3e0.2e3e6c4"]]},{"id":"28c78e23.211732","type":"function","z":"7783ee4a.75705","name":"Prep","func":"var msg2 = [];\n\n// this is the logic when there are multiple data sets are received\nfor (var i=0; i<msg.payload.length; i++) {\n var output = [];\n for (var j=0; j<msg.payload[i].length; j++) {\n output.push([msg.payload[i][j].timestamp, msg.payload[i][j].value]);\n }\n msg2.push({ key: msg.payload[i][0].device+\"/\"+msg.payload[i][0].sensorname, values : output});\n //msg2.push({ key: \"test\", values : output});\n}\n\nmsg.payload=msg2;\n//msg.payload = [ { key: \"Power\", values : output} ];\n//msg.topic = \"Power\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1070,"y":1020,"wires":[["a74de472.7babf8"]]},{"id":"a74de472.7babf8","type":"debug","z":"7783ee4a.75705","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":940,"wires":[]},{"id":"c37e251d.04ec58","type":"ui_chart","z":"7783ee4a.75705","name":"Chart","group":"ffc23bf3.cc9cf8","order":1,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"auto","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"1","removeOlderPoints":"","removeOlderUnit":"604800","cutout":"","useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"x":1350,"y":1020,"wires":[[]]},{"id":"23de8469.09a0dc","type":"debug","z":"7783ee4a.75705","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":940,"wires":[]},{"id":"55cd3e0.2e3e6c4","type":"join","z":"7783ee4a.75705","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":930,"y":1020,"wires":[["94ec6043.75c1b","28c78e23.211732"]]},{"id":"94ec6043.75c1b","type":"debug","z":"7783ee4a.75705","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1050,"y":1100,"wires":[]},{"id":"4f2675e3.cb8bcc","type":"function","z":"7783ee4a.75705","name":"Prep2","func":"\nvar msg2 = [];\nvar output = [];\nfor (var i=0; i<msg.payload.length;i++) {\n for (var j=0; j<msg.payload[i].length; j++) {\n output.push([msg.payload[i][j].timestamp, msg.payload[i][j].value]);\n }\n msg2.push({ key: msg.payload[i][0].device+\"/\"+msg.payload[i][0].sensorname, values : output});\n}\n\nmsg.payload=msg;\nreturn msg;\n\n\n\n\n/*** \nvar msg2 = [];\n\n// this is the logic when there are multiple data sets are received\nfor (var i=0; i<msg.payload.length; i++) {\n var output = [];\n for (var j=0; j<msg.payload[i].length; j++) {\n output.push([msg.payload[i][j].timestamp, msg.payload[i][j].value]);\n }\n msg2.push({ key: msg.payload[i][0].device+\"/\"+msg.payload[i][0].sensorname, values : output});\n //msg2.push({ key: \"test\", values : output});\n}\n\nmsg.payload=msg2;\n//msg.payload = [ { key: \"Power\", values : output} ];\n//msg.topic = \"Power\";\nreturn msg;\n***/","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1210,"y":1020,"wires":[["da3d5b41.18e0d8"]]},{"id":"da3d5b41.18e0d8","type":"debug","z":"7783ee4a.75705","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1350,"y":940,"wires":[]},{"id":"51e1679f.558fe8","type":"ui_group","name":"Selection","tab":"8edd66b5.883ed8","order":1,"disp":true,"width":"6","collapse":false},{"id":"8274941a.73344","type":"sqlitedb","db":"/opt/sqlite/wetterdatenbank.db","mode":"RW"},{"id":"ffc23bf3.cc9cf8","type":"ui_group","name":"Report","tab":"8edd66b5.883ed8","order":2,"disp":true,"width":"6","collapse":false},{"id":"8edd66b5.883ed8","type":"ui_tab","name":"sqliteReport","icon":"dashboard","order":9,"disabled":false,"hidden":false}]
Can you help me?
Thanks!
Edit:
Change-Log:
1 - rearrange of the pictures
2 - add change-log to this post