Send csv file to influxdb using node-red

Hello everyone,I am new to node-red and Infludb .I am tying to push data to influxdb via node-red.
I am not able to write correct json script in function node .
Please have a look into the problem that i have posted in influxdb community.

help me to resolve the problem.
thankyou in advance .

How far have you got?
Have you managed to read the data into an array? If so feed it to a debug node and show us what it shows for the structure of the data.
Which node are you using to write the data to influx?

Thankyou for your interest.

yes I have managed to read the data into an array

also I am using function node to write the data to influxdb in with a tag as 'coil_id' (its a column in my csv).

if you need any other details then please let me know.

Why don't you use one of the influx nodes to wrtie the data? hy use a function node?
If you must use a function node, without showng it how can anyone else see if there is a problem with it?

Make sure you convert all the numbers from strings to numbers. Influx requires them to be numbers. Also you will have to generate a javascript timestamp from the timestamp string.
Also as @zenofmud says most find it easier to use the influxdb nodes. If you are writing a single sample at a time you can use the basic Out node, if you have an array of values then use the influxdb batch node.

1 Like

I used the function node because I want to use tag key .

I read in nodered that if in a function node in msg.payload if two objects were created then first will be considered as field key in influxdb and second as tag key. But i am unable to write json script in msg.payload to add csv file data in it.

also there is problem in timestamp conversion when I am sending my csv file directly to in influxdb

We understood your previous post to say you were writing to influx in a function node. If you meant you were using a function node to reformat the data and then writing it to the db using the influx node that that is fine.
Put the function node reformatting the data (assuming that is what it is doing) put a debug node on its output and show us what you get. No point connecting to the db till you have got the format right.
Look at the Info tab for the influx node to see how the data should be arranged.
If you haven't already done so the read the node red docs page Working with Messages which has a lot of useful data.

ok, I will do the same and post my result

You see the error message you are getting? The timestamp can not be a string, it needs to be a number with nanosecond precision. With that in mind, you need to convert your times (check out https://www.w3schools.com/js/js_dates.asp)

1 Like

Thank you for the concern now I can see the data in influxdb but need to use the tag key. Can you please help to know that how to write the tag key in function node.(tag : Ziehkraft_IST_LIN) from my data.

If you look at the INFO tab of the influx node you will see

If msg.payload is an array containing two objects, the first object will be written as the set of named fields, the second is the set of named tags.

Here is an example
Screen Shot 2020-06-24 at 10.19.04 AM

n this case 'node', 'device' and 'location' are all tags.

1 Like

I get it but in object 1 , i want to have the csv file and object 2 as tag key . How can i use this csv file as first object ?

Post the code of your function node so we can see what you have. Click the </> button and paste the function node code there. What do you want as the tag?


msg.payload = [{
        
},
{
    tag:"Ziehkraft_IST_LIN"
}];
return msg;

you need to build the the array of objects. I will give you an example of what I do. I have data coming into the function node that looks like this:

{
  "_event" : "node:9a8228ea.126e",
  "_msgid" : "f6d98534.55ecd8",
  "device" : "bme280",
  "location" : "geodome",
  "measurement" : "humidity",
  "mqtt" : "local",
  "node" : "node44",
  "payload" : 78.54,
  "qos" : 0,
  "retain" : false,
  "timestamp" : 1593010032187,
  "topic" : "wsremote/data/node44/bme280/humidity",
  "type" : "data"
}

I run it thru my function node which is this:

// create object and populate with Influx tags
var tags = {}
tags.node = msg.node
tags.device = msg.device
tags.location = msg.location

// create object for measurement values
var v = {}
v.value = msg.payload // the measurement value

// create and fill array for the payload
var arr = [] 
arr[0] = v            // the measurement value
arr[1] = tags

// put the array into msg.payload and send it off
msg.payload = arr
return msg;

And this is what comes out (and I feed this to the influx node):

{
  "_event" : "node:9a8228ea.126e",
  "_msgid" : "f6d98534.55ecd8",
  "device" : "bme280",
  "location" : "geodome",
  "measurement" : "humidity",
  "mqtt" : "local",
  "node" : "node44",
  "payload" : [
    {
      "value" : 78.54
    },
    {
      "device" : "bme280",
      "location" : "geodome",
      "node" : "node44"
    }
  ],
  "qos" : 0,
  "retain" : false,
  "timestamp" : 1593010032187,
  "topic" : "wsremote/data/node44/bme280/humidity",
  "type" : "data"
}
1 Like

this is related image to function node:

ok , I will try the same

thankyou so much for your suggestion. Now I am able to send my csv file to influxdb using field and tag keys. But not able to convert the time into influxdb timestamp . Could you please suggest what to edit in my function node

var tags = {}
tags.Ziehkraft_IST_LIN_int= msg.payload.Ziehkraft_IST_LIN_int

var fields = {}
// fields.value = msg.payload
fields.Ziehkraft_IST_LIN= msg.payload.Ziehkraft_IST_LIN

fields.Ziehkraft_IST_LIN_int= msg.payload.Ziehkraft_IST_LIN_int

// fields.pushing = msg.pushing_force

var arr =[]
arr[0] = fields
arr[1] = tags

msg.payload = arr
return msg;


What format is the timestamp that you need to convert?

date format

in this time format and my actual time format is :time
15.08.2017 23:03:23.100000
15.08.2017 23:03:23.110000
15.08.2017 23:03:23.120000