I’m a fair programmer, but new to node red. I may be going about this the wrong way, so if there’s a better way that skips over what I’m trying to do, please let me know. What I’m trying to do: I am polling my 5 JKBMS batteries over bluetooth. So I would like to archive things like cell max / min and delta in influx. I’d like to consolidate the function nodes that format the data to JSON and pass on to influx. What I did was go into the “poll state” node and set the msg.topic to XXXX. msg.payload is of course the value from the BMS such as 3.402v. Now I want to take that JSON message and form another JSON message that gets passed to an influx out node.
So in the fiction block that forms the JSON going to influx, I have code that assembles the msg.payload array but the key seems hard coded. I’m looking for a way to have the value of TOPIC inserted into this key rather than the literal characters TOPIC. I put $'s around it to flag it so you’d catch it when you see it in the code below. For the sake of my example, msg.topic will contain XXXXX and I’m trying to make the JSON msg.payload end up with XXXX: 3.402
It would be simpler using a change node rather than a function node - assuming you don’t have other code in there as well (obviously voltage is coming from the js somewhere). Then you can set it to use JSONata and set msg.payload to:
Thank you for the prompt reply… now I’m on to the next two problems… the code you suggested works, but my voltage is a string not a number and I can’t figure out how to make it a “float”.
var voltage = parseFloat(msg.payload);
var topic= msg.topic;
msg.payload[0][topic] = parseFloat(voltage);
msg.payload[1] = [{
tag1: “Battery1”,
tag2: “DC”,
}];
return msg;
Also, trying to build element [1] of my array with tag1:battery and tag2:DC isn’t working… what the heck am I screwing up? That element doesn’t show up in the debug output of that JSON message.
In the order you’ve done it you would first have to set up the payload as an array, and then set up the first element as an object:
msg.payload = [];
msg.payload[0] = {};
This would probably fix both problems. And, you’ve currently got the second element of the payload array as another array, but your initial code had it as an object. So I think in total it should be:
var voltage = parseFloat(msg.payload);
var topic= msg.topic;
msg.payload = [];
msg.payload[0] = {};
msg.payload[0][topic] = parseFloat(voltage);
msg.payload[1] = {
tag1: "Battery1",
tag2: "DC"
};
return msg;
If you want you could simplify this to:
var voltage = parseFloat(msg.payload);
var topic= msg.topic;
msg.payload = [
{}.
{
tag1: "Battery1",
tag2: "DC"
}
];
msg.payload[0][topic] = parseFloat(voltage);
return msg;
Note also that in the forum you need to surround code with three backticks (```) for it to format correctly.
In the words of Forest Gump, you are my new best good friend. Thank you so much for revealing the idiosyncrasies of this formatting. BOOM. it works (one minor syntax error). Also, thanks for telling me how to format code posted in the forum. I owe you a coffee some day.