My database table looks like this. These values are stored in different frequence, some in 5 minutes, others in 15 minutes.
I did the following UI to operate the chart. On the left there is a form which contains the display period and data points. You always have to select a period and a data point. I use the value of each element in the form. E.g. period/week, period/day, period/yesterday and the data points are always in <device>/<sensor> format. Based on what I am getting back from form I generate the SQL queries for SQLite, joining the individual results and convert the data to the object format expected by the chart:
So far I have all sorts of rubbish data so the chart looks all over the place due to auto scale. But it proves the concept.
I don't really like how the form looks like, so I will most probably try something with the UI Template, hence my questions in the other topic.
[{"id":"b89d8b25.670e18","type":"function","z":"15e1f6a1.aeab19","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 epoch = d.getTime();\nvar fromdate = epoch - Day;\nvar enddate = epoch;\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 = epoch-Day;\n enddate = epoch;\n break;\n case \"yesterday\":\n fromdate = epoch-2*Day;\n enddate = epoch-Day;\n break;\n case \"week\":\n fromdate = epoch-Week;\n enddate = epoch;\n break;\n }\n } else {\n output.push({ topic: \"SELECT * FROM sensor_data WHERE device='\"+parts[0]+\"' AND sensor='\"+parts[1]+\"' AND epoch >= \" + fromdate + \" AND epoch <= \" + 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,"x":297,"y":93,"wires":[["97710c81.04837"]]},{"id":"97710c81.04837","type":"sqlite","z":"15e1f6a1.aeab19","mydb":"1c25415d.b8427f","name":"DB","x":443,"y":94,"wires":[["d8204a71.a0b7b8"]]},{"id":"8770c8b5.64db38","type":"ui_chart","z":"15e1f6a1.aeab19","name":"Chart","group":"bd903d6.d9fc7c","order":0,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"%a %H:%M","interpolate":"linear","nodata":"","ymin":"","ymax":"","removeOlder":"1","removeOlderUnit":"604800","x":790,"y":194,"wires":[[],[]]},{"id":"445b8970.f10ee8","type":"function","z":"15e1f6a1.aeab19","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].epoch, msg.payload[i][j].value]);\n }\n msg2.push({ key: msg.payload[i][0].device+\"/\"+msg.payload[i][0].sensor, 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,"x":774,"y":96,"wires":[["8770c8b5.64db38"]]},{"id":"7bd93909.e45ba8","type":"inject","z":"15e1f6a1.aeab19","name":"Reset chart","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":132,"y":216,"wires":[["3dda44e3.798ecc"]]},{"id":"3dda44e3.798ecc","type":"function","z":"15e1f6a1.aeab19","name":"Empty payload","func":"msg.payload = [];\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":215,"wires":[["8770c8b5.64db38"]]},{"id":"6adefc94.74caa4","type":"ui_form","z":"15e1f6a1.aeab19","name":"Data points","label":"","group":"1e17cd0b.862863","order":0,"width":0,"height":0,"options":[{"label":"Period: today","value":"period/today","type":"checkbox","required":false},{"label":"Period: yesterday","value":"period/yesterday","type":"checkbox","required":false},{"label":"Period: this week","value":"period/week","type":"checkbox","required":false},{"label":"Miflora temperature","value":"miflora/temp","type":"checkbox","required":false},{"label":"Miflora battery level","value":"miflora/battery","type":"checkbox","required":false},{"label":"Miflora sunlight","value":"miflora/sunlight","type":"checkbox","required":false},{"label":"Miflora soil moisture","value":"miflora/moisture","type":"checkbox","required":false},{"label":"Miflora soil fertility","value":"miflora/fertility","type":"checkbox","required":false},{"label":"Solar power","value":"growatt/power","type":"checkbox","required":false},{"label":"Solar voltage","value":"growatt/voltage","type":"checkbox","required":false}],"formValue":{"period/today":false,"period/yesterday":false,"period/week":false,"miflora/temp":false,"miflora/battery":false,"miflora/sunlight":false,"miflora/moisture":false,"miflora/fertility":false,"growatt/power":false,"growatt/voltage":false},"payload":"","topic":"","x":102,"y":40,"wires":[["b89d8b25.670e18"]]},{"id":"d8204a71.a0b7b8","type":"join","z":"15e1f6a1.aeab19","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","timeout":"","count":"","x":585,"y":94,"wires":[["445b8970.f10ee8"]]},{"id":"1c25415d.b8427f","type":"sqlitedb","z":"","db":"/home/pi/sqlite/nodered"},{"id":"bd903d6.d9fc7c","type":"ui_group","z":"","name":"Report","tab":"1e3fe400.0baf5c","order":2,"disp":true,"width":"18"},{"id":"1e17cd0b.862863","type":"ui_group","z":"","name":"Selection","tab":"1e3fe400.0baf5c","order":1,"disp":true,"width":"5"},{"id":"1e3fe400.0baf5c","type":"ui_tab","z":"","name":"Reports","icon":"dashboard","order":9}]
--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/FhLUZT7q6Sw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/c3c043f3-f9af-4a22-80c7-f6c5dc024c69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi,I tried using this flow with my own database. I just changed your columns for mine. In the first function, I get the error
TypeError: Cannot set property 'complete' of undefinedAny idea what the problem could be? I haven't changed anything other than column names. I only made changes to the first function node where the topic is set and pointed the sqlite node to my DB. I didn't make any other changes to the code provided byCsongor Varga in the original post.EDIT:
CREATE TABLE data_table (
device_id integer NOT NULL,
data_type TEXT NOT NULL,
data_value REAL NOT NULL,
data_time DATETIME NOT NULL);
--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/a280f20d-3630-4b08-b637-f0449839408b%40googlegroups.com.
<Captura de pantalla de 2018-05-05 19-08-54.png>