// La trame complète est reçue dans 'msg'
var Fields = [];
var Tags = [];
// Enlever les codes début et fin de trame
var lines = msg.payload.toString().replace("\u0002\n","").replace("\r\u0003","");
// Récupérer chaque ligne une à une
lines = lines.split("\r\n");
for (var line in lines)
{
// on dépile nos 3 valeurs
var myline = lines[line].toString().replace(" "," s").split(" ");
var check = myline.pop();
var value = myline.pop();
var label = String(myline.pop());
if (["BBRHCJB","BBRHPJB","BBRHCJW","BBRHPJW","BBRHCJR","BBRHPJR","IINST","IMAX","PAPP"].indexOf(label)>=0)
{
Fields.push({[label] : Number(value)});
//Fields.push({["\""+label+"\""] : Number(value)});
}
else if (["PTEC","DEMAIN"].indexOf(label)>=0)
{
Tags.push({[label] : value});
//Tags.push({["\""+label+"\""] : value});
//Tags.push({["\""+label+"\""] : ["\""+value+"\""]});
}
}
msg.payload = [Fields,Tags];
return msg;```
My debug message seems OK:
"
25/11/2023 13:18:19[noeud: debug 7](http://192.168.0.37:1880/#)
msg.payload : array[2]
array[2]
0: array[9]
0: object
1: object
BBRHPJB: 2126343
2: object
BBRHCJW: 112634
3: object
BBRHPJW: 141164
4: object
BBRHCJR: 0
5: object
BBRHPJR: 0
6: object
IINST: 7
7: object
IMAX: 90
8: object
PAPP: 1530
1: array[2]
0: object
PTEC: "HPJB"
1: object
DEMAIN: "----"
"
But I can't make it to have it in the same record in influxdb :
> SELECT * from test
name: test
time BBRHCJB BBRHPJB DEMAIN PTEC
---- ------- ------- ------ ----
1700914793504145056 1703479 2126374
1700914793504145056 ---- HPJB
As one can see, influxdb stores the fields first, then the tags.....
Any idea ??????????????????
Thx in advance.
You appear to have an array of two arrays instead of an array of two objects.
So element 0 should be an object (not an array), something like
{
BBRHPJB: 2126343,
BBRHCJW: 112634
}
and so on. The same for the tags.
In your function, instead of
var Fields = [];
var Tags = [];
you need to create empty objects
var Fields = {};
var Tags = {};
then instead of Fields.push({[label] : Number(value)});
something like Fields[label] = Number(value)});
and similarly for the tags.
Before feeding into influxdb look at it in a debug node and make sure it looks correct.
> SELECT * from test
name: test
time BBRHCJB BBRHCJR BBRHCJW BBRHPJB BBRHPJR BBRHPJW DEMAIN IINST IMAX PAPP PTEC
---- ------- ------- ------- ------- ------- ------- ------ ----- ---- ---- ----
1700919587984269609 1703479 0 112634 2128636 0 141164 ---- 5 90 1200 HPJB
thank you so much, I've been stuck for 3 days on testing so many possibilities.
And such a quick answer
By the way, I've been searching a lot on the net but I find that not so easy to have clear explainations and examples
Would you recommend some really pedagogic links or tutos ??
I'm sure there must be a subject on the forum but I'm a newby on forums too. I tryed to but couldn't figure out
I don't think you will find many detailed tutorials on formatting data for influx in node-red. Assuming that you had read the help text then the fundamental problem I think was that you did not realise the difference between an array and an object. Have you watched the node-red intro videos? Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point.
I looked at the info tab of the node, and many sites to understand better, but I didn't meet, so far, the method you suggested to fill in the array with objects (that's why I was loosing myself with push : I was misunderstanding and tyring to push objects in an array this way :
Fields.push({[label] : value})
but I was stuck with placing a space beetwin fields an tags ... bla bla,
plus :
Yes indeed my declaration was wrong.
The node info is not so clear about that :
" 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.
If msg.payload is an object containing multiple properties, the fields will be written to the measurement."
I was wanting the second case while doing the first.
Your proposal, which works great is, if I understand well, an array of objects, not a global object like said in the node info....
U're right It's not so clear to me yet, but it will
Since you need tags you can't use that format (a simple object) you have to use the next one down, which is an array of two objects. The first one being the fields and the second one being the tags.