I have managed to implement most things I needed ,just got stuck on one:
if(msg.payload == 0){
var i=0;
//node.warn(flow.get(individual_items))
var items = flow.get('individual_items')
var number = flow.get('counter_temp')
node.warn("COUNTER 0 DETECTED")
node.warn(items)
node.warn(number)
for(i;i<number;i++)
msg.topic[i] = items[i]
node.warn(msg.topic[i])
}
return msg
[{"id":"6bdf19ef.77f2a8","type":"debug","z":"df177cb8.27882","name":"status","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":1880,"wires":},{"id":"88eb8b89.498378","type":"function","z":"df177cb8.27882","name":"-1 from counter when status=DONE","func":"var temp_var;\nif (msg.payload == "DONE"){\n msg.topic = "global/counter"\n flow.set('counter',flow.get('counter')-1)\n msg.payload = flow.get('counter')\n node.warn("COUNTER DECREMENTED")\n return msg\n}\nelse {\n return msg\nnode.warn("STATUS NOT DONE")\n}\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1020,"y":1820,"wires":[["cef1a3c2.340f5","aa9503a7.c2c02"]]},{"id":"aa9503a7.c2c02","type":"debug","z":"df177cb8.27882","name":"decremented counter","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1360,"y":1820,"wires":},{"id":"72f7e508.9a6dfc","type":"mqtt in","z":"df177cb8.27882","name":"","topic":"+/status","qos":"2","datatype":"auto","broker":"2727c5a5.a4fb6a","x":770,"y":1820,"wires":[["88eb8b89.498378"]]},{"id":"cef1a3c2.340f5","type":"function","z":"df177cb8.27882","name":"function to make msg.payload to flow.counter","func":"if(msg.payload == 0){\n var i=0;\n //node.warn(flow.get(individual_items))\n var items = flow.get('individual_items')\n var number = flow.get('counter_temp')\n node.warn("COUNTER 0 DETECTED")\n node.warn(items)\n node.warn(number)\n for(i;i<number;i++)\n msg.topic[i] = items[i]\n node.warn(msg.topic[i])\n}\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1110,"y":1720,"wires":[]},{"id":"9be853bd.5da3c","type":"debug","z":"df177cb8.27882","name":"GOOD JOB","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","statusVal":"","statusType":"auto","x":1410,"y":1720,"wires":},{"id":"2727c5a5.a4fb6a","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]
In the function, I am checking if the payload is 0 ( thats the counter value that I decrement).
When the value reaches 0, I want to set the msg.topic to the Individual item array as following:
msg.topic[0] = Individual_items[0]
msg.topic[1] = Individual_items[1] .....

I Used node.warn to print what are the individual_items and I sucessfully get an debug message that Individual_items=[device1,device2] which is correct.
However, it wont let me set the msg.topic[0] = individual_items[0] like I have tried