InfluxDB help needed: inputting multiple data manually with a loop

Hello,

I am trying to input multiple data manually into an influxDB database using this guide here:

My data consists of 6 columns and 12 rows as seen below:
image

Following the guide, I tried to input the time and data by converting the data into JSON and creating a loop that generated a string similar to the example provided in the link above. My code looks like this:

//Creating buffer
buffer = "[";
    for(var i = 0; i < n; i++){ //n is number of data
        if(i < (n-1)){
            buffer += "[ \
                            { \
                                time:" + content.time[i] + ", \
                                k1:" + content.k1[i] + ", \
                                k2:" + content.k2[i] + ", \
                                k3:" + content.k3[i] + ", \
                                k4:" + content.k4[i] + ", \
                                k5:" + content.k5[i] + " \
                            } \
                        ],"
        } else{
            buffer += "[ \
                            { \
                                time:" + content.time[i] + ", \
                                k1:" + content.k1[i] + ", \
                                k2:" + content.k2[i] + ", \
                                k3:" + content.k3[i] + ", \
                                k4:" + content.k4[i] + ", \
                                k5:" + content.k5[i] + " \
                            } \
                        ]"
        }
    }
buffer += "]";

msg.payload = buffer;
return msg;

//EXAMPLE from the guide to input 2 rows of data manually:
msg.payload = [
    [
        {
            numValue: 10,
            randomValue: Math.random()*10,
            strValue: "message1",
            time: new Date("2015-12-28T19:41:13Z").getTime()
        }
    ],
    [
        {
            numValue: 20,
            randomValue: Math.random()*10,
            strValue: "message2",
            time: new Date("2015-12-28T19:41:14Z").getTime()
        }
    ]
];

However, when I hooked the "function" node where I generated the string to the "influxdb out" node the output didn't come out as I expected:
image

I was hoping I could make 12 rows of data with 5 columns, not 1 row of data with the generated string as the value. How do I make this work? I appreciate any kind of help!

Thank you in advance!

best way to get help when you have a problem

  1. provide the name of the platform and version of the OS (like Pi - stretch lite 2018-11093(
  2. provide the versions of Node-RED and node.js (can be found in the NR startup log)
  3. make a small version of your flow demonstraiting your issue - including inserts of sample data
  4. provide the names of any node-red-contrib-??? nodes you are using in the flow
  5. provide a copy of your flow demonstrating the issue.

This is a bit of work for you, but it makes it easier for someone - who you are asking for help from - to quickly look at your issue and (hopefully) give you pointers so you can learn and solve your problem.

P.S. you should put a debug node on the output of your fuction node (display the complete msg object) - that may give you a hint at what is wrong.

P.P.S. you can add debugging to a function node by using node.warn. For example after the line:
if(i < (n-1)){
you could add:
node.warn("buffer="+buffer);
and it would display the buffer in the debug log each time the IF was true

1 Like

Hi zenofmud,

Thank you for your input!
I apologize for that. I will add those points into my question right away!

  1. The platform on which I'm using Node Red is Windows 7 Home Premium.
  2. The Node-RED version I'm using is v0.19.4, whereas the node.js version is v8.12.0
  3. The nodes I'm using is node-red-contrib-influxdb

3,5. I have solved the problem :smiley: The code is provided below.

The code works fine. I have attached a debug node and it shows that it has successfully created a string like the example. The problem is, when I try to input the generated string into "influxdb out", it refused to create a 12 rows, 5 columns database, instead it only created a row, and 2 columns with the first column being the timestamp when the string is injected, and the second column being the generated string.

=====Solution====
But anyway after mulling for some time, I have finally found the solution! Turned out the string has to be converted to JSON first before going to the database so I had to put a JSON node before the influxdb out node. Here's the code:

[{"id":"eebd77d4.12ae28","type":"tab","label":"input multiple data to influxDB manually","disabled":false,"info":"Example for node-red forum"},{"id":"f1d4b488.912018","type":"inject","z":"eebd77d4.12ae28","name":"inject data (JSON)","topic":"","payload":"{\"t\":[1548832805642,1548833525464,1548834375577],\"k1\":[1,0,0],\"k2\":[1,0,1],\"k3\":[1,0,0],\"k4\":[1,1,1],\"k5\":[0,0,0]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":166,"y":77,"wires":[["6991807f.01aa8"]]},{"id":"6991807f.01aa8","type":"function","z":"eebd77d4.12ae28","name":"input multiple data to influxDB","func":"content = msg.payload;\nn = 3 //number of data\n\nvar buffer;\nbuffer = \"[\";\n    for(var i = 0; i < n; i++){\n        if(i < (n-1)){\n            buffer += \"[ \\\n                            { \\\n                                \\\"time\\\":\" + content.t[i] + \", \\\n                                \\\"k1\\\":\" + content.k1[i] + \", \\\n                                \\\"k2\\\":\" + content.k2[i] + \", \\\n                                \\\"k3\\\":\" + content.k3[i] + \", \\\n                                \\\"k4\\\":\" + content.k4[i] + \", \\\n                                \\\"k5\\\":\" + content.k5[i] + \" \\\n                            } \\\n                        ],\"\n        } else{\n            buffer += \"[ \\\n                            { \\\n                                \\\"time\\\":\" + content.t[i] + \", \\\n                                \\\"k1\\\":\" + content.k1[i] + \", \\\n                                \\\"k2\\\":\" + content.k2[i] + \", \\\n                                \\\"k3\\\":\" + content.k3[i] + \", \\\n                                \\\"k4\\\":\" + content.k4[i] + \", \\\n                                \\\"k5\\\":\" + content.k5[i] + \" \\\n                            } \\\n                        ]\"\n        }\n    }\nbuffer += \"]\";\n\nmsg.payload = buffer;\nreturn msg;\n\n/* Message payload will be:\n\"[[ { time:1548832805642, k1:1, k2:1, k3:1, k4:1, k5:0 } ],[ { time:1548833525464, k1:0, k2:0, k3:0, k4:1, k5:0 } ],[ { time:1548834375577, k1:0, k2:1, k3:0, k4:1, k5:0 } ]]\"\n\nReformatted for easy reading:\n\"[\n    [\n        { \n            time:1548832805642, \n            k1:1, \n            k2:1, \n            k3:1, \n            k4:1, \n            k5:0 \n        }\n    ],\n    [ \n        { \n            time:1548833525464, \n            k1:0, \n            k2:0, \n            k3:0, \n            k4:1, \n            k5:0 \n        } \n    ],\n    [ \n        { \n            time:1548834375577, \n            k1:0, \n            k2:1, \n            k3:0, \n            k4:1, \n            k5:0 \n        } \n    ]\n]\"\n*/","outputs":1,"noerr":0,"x":447,"y":77,"wires":[["a7f5b1d.1cc6f5","a153f08b.adff"]]},{"id":"a7f5b1d.1cc6f5","type":"debug","z":"eebd77d4.12ae28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":838,"y":20,"wires":[]},{"id":"d0363093.c8a46","type":"influxdb out","z":"eebd77d4.12ae28","influxdb":"a5f8b61d.9ff568","name":"","measurement":"data","precision":"","retentionPolicy":"","x":912,"y":76,"wires":[]},{"id":"4c301c5a.b9a004","type":"influxdb out","z":"eebd77d4.12ae28","influxdb":"a5f8b61d.9ff568","name":"","measurement":"example","precision":"","retentionPolicy":"","x":679,"y":226,"wires":[]},{"id":"3bef82df.e336ee","type":"function","z":"eebd77d4.12ae28","name":"input 2 row of data manually","func":"//REFERENCE: inputting 2 row of data manually ======================\nmsg.payload = [\n    [\n        {\n            numValue: 20,\n            randomValue: Math.random()*10,\n            strValue: \"message1\",\n            time: new Date(\"2015-12-28T19:41:13Z\").getTime()\n        }\n    ],\n    [\n        {\n            numValue: 30,\n            randomValue: Math.random()*10,\n            strValue: \"message2\",\n            time: new Date(\"2015-12-28T19:41:14Z\").getTime()\n        }\n    ]\n];\n\nreturn msg;","outputs":1,"noerr":0,"x":352,"y":226,"wires":[["4c301c5a.b9a004","faa6f6b3.6594b8"]]},{"id":"fb01f719.c1a2f8","type":"inject","z":"eebd77d4.12ae28","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":131,"y":226,"wires":[["3bef82df.e336ee"]]},{"id":"a6d12d7e.b8396","type":"comment","z":"eebd77d4.12ae28","name":"Example from nodered.org","info":"URL:\nhttps://flows.nodered.org/node/node-red-contrib-influxdb","x":161,"y":175,"wires":[]},{"id":"faa6f6b3.6594b8","type":"debug","z":"eebd77d4.12ae28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":601,"y":182,"wires":[]},{"id":"a153f08b.adff","type":"json","z":"eebd77d4.12ae28","name":"","property":"payload","action":"","pretty":false,"x":664,"y":76,"wires":[["a7f5b1d.1cc6f5","d0363093.c8a46"]]},{"id":"a5f8b61d.9ff568","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"test_dellater2","name":"","usetls":false,"tls":""}]

Thank you for your help! :smiley:

2 Likes