Single to batch influxdb

Hello guys, I would like to know how to input this string into influxdb using a batch node.

[{"name":"airflow","description":"","unit":"","values":[{"value":0,"timestamp":"2021-06-09T08:40:46.237Z"}]},{"name":"current","description":"","unit":"","values":[{"value":227,"timestamp":"2021-06-09T08:40:46.237Z"}]}]

Currently im doing it in a way that i would need as many change nodes as measurements to change msg.payload to value and msg.measurement to the measurement name.


This poses an upsizing issue where if I had more measurements, I would have to add in more change nodes. Iv looked at flows help and examples but im having trouble getting set up with my string.
Please help

You don't need the json node, set the mqtt node to output the result of parsing the json string coming in.

Show us what what output you want to pass to influx for the message you posted.

Thanks for the json node tip, didnt know i could do that.
This is the input node just for reference

[{"name":"airflow","description":"","unit":"","values":[{"value":0,"timestamp":"2021-06-09T08:40:46.237Z"}]},{"name":"current","description":"","unit":"","values":[{"value":227,"timestamp":"2021-06-09T08:40:46.237Z"}]}]

This is the output I want

msg.payload = [
    {
        measurement: "sensor", 
        fields: {
            airflow: 0,
            current: 227
}}]

The measurement name dosent matter. What I really want is the fields. The format above is basically the input format for the influxdb batch node
TLDR; I want quote 1 to become quote 2

I actually made a simple sorting algorithm to extract the values from my original string and set it in the batch node format, but I was wondering if there's a simpler way to achieve this. It would be very hard to explain this to potential clients and the reason why I chose node-red is because its graphical. Even my boss disagrees with this method since we might as well just use a script.

 var input = msg.payload
 var values = [];
 var measurements = [];
 var myArray = [];
 var starter = '[{"measurement":"sensor","fields":{';
 
 for (var i = 0; i < input.length; ++i) {
   values[i] = input[i].values[0].value;
   measurements[i] = input[i].name;
 }
 
 for (i = 0; i < values.length; ++i) {
   if (i < values.length - 1) {
     myArray[i] = ('"' + measurements[i] + '"' + ":" + values[i]);
   } else if (i == values.length - 1) {
     myArray[i] = ('"' + measurements[i] + '"' + ":" + values[i] + "}}]");
   }
 }
 
 var output = starter.concat(myArray.join());
 msg.payload = output;
 return msg;

He actually called me an idiot for doing it in such a hardcode way. Pls teach me the node-red way

Why are you using the batch node, it would be a little simple with the basic out node.

Try Using JSONata expression to generate the fields.
eg.

[{"id":"327aee38.7d8842","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":2740,"wires":[]},{"id":"687e698f.e16a98","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t   {\t       \"measurement\": \"sensor\",\t       \"fields\": payload.${$.name: $.values.value}\t  }\t]         ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":2760,"wires":[["327aee38.7d8842"]]},{"id":"e5b8fc50.2447a8","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"name\":\"airflow\",\"description\":\"\",\"unit\":\"\",\"values\":[{\"value\":0,\"timestamp\":\"2021-06-09T08:40:46.237Z\"}]},{\"name\":\"current\",\"description\":\"\",\"unit\":\"\",\"values\":[{\"value\":227,\"timestamp\":\"2021-06-09T08:40:46.237Z\"}]}]","payloadType":"json","x":80,"y":2760,"wires":[["687e698f.e16a98"]]}]
[
   {
       "measurement": "sensor",
       "fields": payload.${$.name: $.values.value}
  }
]                 
1 Like

Yep this is the answer. Perfect. Thank you

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