I’m new here, so please excuse any inexact terminology.
Input to a node with 3 outputs consists of an array: [34, 35, 36]
matchVar = 35
Function iterates over the array. Where a match is found, the corresponding msg.payload is set to {"On": true}. Where it does not match msg.payload becomes {"On": false}
I've got this far, but I am not constructing the output correctly:
var matchVar = 35; //test value
var outmsg = {};
var inputArray = msg.payload; // [34, 35, 36]
var outputArray = [];
var i;
for (i = 0; i < inputArray.length; i++) {
if (inputArray[i] == switchedOn) {
outmsg.payload={"On": "true"};
}else{
outmsg.payload={"On": "false"};
}
outputArray.push(outmsg.payload);
}
return outputArray;
I (think I) know that the output should look like: [ msg1, msg2, msg3] where msg1.payload is {"On": "true"}, msg2.payload is {"On": "false"} and msg3.payload is {"On": "true"}, but I am getting tangled up.
So what you need to do is build an output array too.
So you have the three checks.
I am not a programmer. But from your code:
for (i = 0; i < inputArray.length; i++) {
if (inputArray[i] == switchedOn) {
outmsg.payload={"On": "true"};
}else{
outmsg.payload={"On": "false"};
}
outputArray.push(outmsg.payload);
}
Make it something like:
for (i = 0; i < inputArray.length; i++) {
if (inputArray[i] == switchedOn) {
outmsg[i].payload={"On": "true"}; // look at what I did here.
}else{
outmsg[i].payload={"On": "false"}; // and here.
}
outputArray.push(outmsg.payload);
}
You are using the same message and thus overwriting the previous payload.
to feed multiple outputs, a complete message (including payload) is required, you just push the payload
So something like this should work (not tested):
for (i = 0; i < inputArray.length; i++) {
let outmsg = {}; // create new message object for each output
if (inputArray[i] == switchedOn) {
outmsg.payload={"On": "true"};
}else{
outmsg.payload={"On": "false"};
}
outputArray.push(outmsg); // output the complete message object
}
Alternatively, you could reuse the input message and clone it with RED.util.cloneMessage(msg) each time. That way you can preserve existing properties.