0
votes

My array payload is like: msg.payload[index].temp. I cut the array in single parts to publish the single parts of the array via MQTT.

So I got msg.payload.temp and msg.parts.index.

msg:{
       payload:{
                    address: "e4:32:a3:3a:99:97";
                    temp: "32"
                }
         parts: {
                   type: "array";
                   index: "16";
                   count: "17";
                }
}

Is it possible to put the number of the index into the name msg.payload.temp? So I got something like msg.payload.temp_6. I tried to combine strings together like:

var temp = "msg.payload.temp_" + msg.parts.index.toString();
temp = payload.temp;
return msg;

Do I need a conversation from string to json so Node-Red detect the property?

1
What have you already tried? - hardillb
Tried var temp = "msg.payload.temp-"+msg.parts.index.toString() but doesnt work - Marcel
Edit the question to add details (show all the code you used in the function node), and explain what you mean by "doesn't work" - hardillb
I want to add the index number into the property names of my payload. The property names are always the same but the index numbers are always changing because the size of the array which I split before the function is rising with timeWhen I try to combine the index number "msg.parts.index" with "msg.payload.temp" to a string node-red doesnt accept the property. Maybe I need a conversation from string to json? - Marcel
EDIT the question! - hardillb

1 Answers

1
votes

You can add a property to msg.payload.

let index = 6;
msg.payload["temp"+index] = 'Whatever';

This will be available as

console.log(msg.payload.temp6); // Whatever