Foreach topic and payload

I have array,

for (var i = 0; i < numbers.length; i++) {
  numbers[i] *= 2;
query.push(array[i].query;
temp.push(array[i].push;
}

and I want to send msg.topic, msg.payload, msg.something like this

msg.query.forEach(query => node.send({topic: query}))
msg.temp.forEach(temp=>node.send({payload: temp}))

How to do that

Why don't you tell us what's in your array?

And tell us exactly what you want to send?

Otherwise it's all guesswork.
You have given us your guess at the code, we have to guess what the data looks like.

This is my flow

[{"id":"a8b7f1f2.cd6ca","type":"modbus-flex-connector","z":"205b40d7.bc47a8","name":"","maxReconnectsPerMinute":4,"emptyQueue":false,"showStatusActivities":true,"showErrors":false,"server":"3f55d524.d93c72","x":670,"y":480,"wires":[["38cbf3bc.9ad9d4","306fb5f5.4304fa"]]},{"id":"38cbf3bc.9ad9d4","type":"debug","z":"205b40d7.bc47a8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":850,"y":380,"wires":[]},{"id":"ea1493b6.7819b","type":"modbus-flex-sequencer","z":"205b40d7.bc47a8","name":"sequencer 1","sequences":[{"name":"1","unitid":"1","fc":"FC4","address":"10009","quantity":"1"}],"server":"3f55d524.d93c72","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":1130,"y":440,"wires":[["60595176.16f1d8"],[]]},{"id":"e20cc429.d0ebf","type":"function","z":"205b40d7.bc47a8","name":"f1","func":"var _unitId = msg.unitId;\n\nmsg.sequences = [\n    {\n        name: \"Foo\",\n        unitid: _unitId,\n        fc: 4,\n        address: 10009,\n        quantity: 1\n    }\n    ]\n\nreturn msg;","outputs":1,"noerr":0,"x":970,"y":440,"wires":[["ea1493b6.7819b","fc14e47a.1f9368"]]},{"id":"60595176.16f1d8","type":"debug","z":"205b40d7.bc47a8","name":"output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1090,"y":520,"wires":[]},{"id":"306fb5f5.4304fa","type":"delay","z":"205b40d7.bc47a8","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":840,"y":440,"wires":[["e20cc429.d0ebf"]]},{"id":"fc14e47a.1f9368","type":"debug","z":"205b40d7.bc47a8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1080,"y":380,"wires":[]},{"id":"e2d77eca.b789e","type":"inject","z":"205b40d7.bc47a8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":460,"wires":[["7a7de6e0.750358"]]},{"id":"7a7de6e0.750358","type":"function","z":"205b40d7.bc47a8","name":"f1","func":"var array =[{\"delta\":1,\"nazwa\":\"ED_9373\",\"host\":\"10.0.0.202\",\"modbus_id\":8},{\"delta\":2,\"nazwa\":\"ED_9376\",\"host\":\"10.0.0.132\",\"modbus_id\":2},{\"delta\":3,\"nazwa\":\"ED_9377\",\"host\":\"10.0.0.201\",\"modbus_id\":4},{\"delta\":4,\"nazwa\":\"ED_9375\",\"host\":\"10.0.0.203\",\"modbus_id\":9},{\"delta\":5,\"nazwa\":\"ED_9379\",\"host\":\"10.0.0.204\",\"modbus_id\":5},{\"delta\":6,\"nazwa\":\"ED_9374\",\"host\":\"10.0.0.203\",\"modbus_id\":3},{\"delta\":8,\"nazwa\":\"ED_9380\",\"host\":\"10.0.0.202\",\"modbus_id\":7},{\"delta\":9,\"nazwa\":\"ED_9381\",\"host\":\"10.0.0.203\",\"modbus_id\":6},{\"delta\":7,\"nazwa\":\"ED_9378\",\"host\":\"10.0.0.131\",\"modbus_id\":10}];\nvar host = '';\nvar unitId = 1;\nvar query = [];\nvar host_array = [];\nvar unitId_array = [];\n\n\nvar i=0;\nfor (i=0; i<array.length;i++)\nif (array[i]) {\n    host = array[i].host;\n    unitId = array[i].modbus_id;\n query.push({    'connectorType': 'TCP',\n    'tcpHost': host,\n    'tcpPort': '502',\n    'unitId': unitId\n     \n })  \n host_array.push(host);\n unitId_array.push(unitId);\n    \n}\n\nmsg.host = host_array;\nmsg.unitId = unitId_array;\n\n\nquery.forEach(query => node.send({payload: query}));\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":480,"wires":[["32ebc461.d791fc","9a8b464d.122a98"]]},{"id":"32ebc461.d791fc","type":"debug","z":"205b40d7.bc47a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":450,"y":400,"wires":[]},{"id":"9a8b464d.122a98","type":"delay","z":"205b40d7.bc47a8","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"10","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":440,"wires":[["a8b7f1f2.cd6ca"]]},{"id":"3f55d524.d93c72","type":"modbus-client","z":"","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"10.0.0.203","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"","unit_id":0,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":false,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true}]

my array var array =[{"delta":1,"nazwa":"ED_9373","host":"10.0.0.202","modbus_id":8},{"delta":2,"nazwa":"ED_9376","host":"10.0.0.132","modbus_id":2},{"delta":3,"nazwa":"ED_9377","host":"10.0.0.201","modbus_id":4},{"delta":4,"nazwa":"ED_9375","host":"10.0.0.203","modbus_id":9},{"delta":5,"nazwa":"ED_9379","host":"10.0.0.204","modbus_id":5},{"delta":6,"nazwa":"ED_9374","host":"10.0.0.203","modbus_id":3},{"delta":8,"nazwa":"ED_9380","host":"10.0.0.202","modbus_id":7},{"delta":9,"nazwa":"ED_9381","host":"10.0.0.203","modbus_id":6},{"delta":7,"nazwa":"ED_9378","host":"10.0.0.131","modbus_id":10}];

From function f1 I want to send payload like this `{ 'connectorType': 'TCP',
'tcpHost': host,
'tcpPort': '502',
'unitId': unitId

}`
to modbus Flex Connector, and to f2 I want to send modbus_id from my array.

I thing there is solution
It is possible using one output?

[{"id":"76bb7a6436a00103","type":"function","z":"5a11616887901db9","name":"","func":"var array =[{\"delta\":1,\"nazwa\":\"ED_9373\",\"host\":\"10.0.0.202\",\"modbus_id\":8},{\"delta\":2,\"nazwa\":\"ED_9376\",\"host\":\"10.0.0.132\",\"modbus_id\":2},{\"delta\":3,\"nazwa\":\"ED_9377\",\"host\":\"10.0.0.201\",\"modbus_id\":4},{\"delta\":4,\"nazwa\":\"ED_9375\",\"host\":\"10.0.0.203\",\"modbus_id\":9},{\"delta\":5,\"nazwa\":\"ED_9379\",\"host\":\"10.0.0.204\",\"modbus_id\":5},{\"delta\":6,\"nazwa\":\"ED_9374\",\"host\":\"10.0.0.203\",\"modbus_id\":3},{\"delta\":8,\"nazwa\":\"ED_9380\",\"host\":\"10.0.0.202\",\"modbus_id\":7},{\"delta\":9,\"nazwa\":\"ED_9381\",\"host\":\"10.0.0.203\",\"modbus_id\":6},{\"delta\":7,\"nazwa\":\"ED_9378\",\"host\":\"10.0.0.131\",\"modbus_id\":10}];\nmsg1 = {a:2};\nmsg2 = {b:2};\nmsg3 = {c:2};\n\n\nvar host = '';\nvar unitId = 1;\nvar query = [];\nvar host_array = [];\nvar unitId_array = [];\n\n\nvar i=0;\nfor (i=0; i<array.length;i++)\nif (array[i]) {\n    host = array[i].host;\n    unitId = array[i].modbus_id;\n query.push({    'connectorType': 'TCP',\n    'tcpHost': host,\n    'tcpPort': '502',\n    'unitId': unitId\n     \n })  \n host_array.push(host);\n unitId_array.push(unitId);\n    \n}\n\nmsg.host = host_array;\nmsg.unitId = unitId_array;\n\n\nquery.forEach(query => node.send({msg1: query}));\nhost_array.forEach(host_array => node.send({msg2: host_array}));\nunitId_array.forEach(unitId_array => node.send({msg3: unitId_array}));\n\n\n\n\n\n","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":200,"wires":[["345a0b3cfa112a3b"],["179f67d021bd2ec8"],["ffbb0444a4b15bfb"]]},{"id":"9abf350082ac563a","type":"inject","z":"5a11616887901db9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":200,"wires":[["76bb7a6436a00103"]]},{"id":"345a0b3cfa112a3b","type":"debug","z":"5a11616887901db9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":430,"y":160,"wires":[]},{"id":"179f67d021bd2ec8","type":"debug","z":"5a11616887901db9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":430,"y":220,"wires":[]},{"id":"ffbb0444a4b15bfb","type":"debug","z":"5a11616887901db9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":430,"y":280,"wires":[]}]

If I understand correctly try this

[{"id":"7a7de6e0.750358","type":"function","z":"30941801.e74b08","name":"f1","func":"let array = msg.payload;\nfor (let i=0; i<array.length;i++){\n    node.send({\n        \"payload\":{   \n            'connectorType': 'TCP',\n            'tcpHost': array[i].host,\n            'tcpPort': '502',\n            'unitId': array[i].modbus_id\n        },\n        'host':array[i].host,\n        'unitId': array[i].modbus_id,\n        'topic': 'a_topic'\n        });\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":220,"wires":[["e91b31fa.f5e628"]]},{"id":"e91b31fa.f5e628","type":"debug","z":"30941801.e74b08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":220,"wires":[]},{"id":"6a52daab.ce364c","type":"inject","z":"30941801.e74b08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"delta\":1,\"nazwa\":\"ED_9373\",\"host\":\"10.0.0.202\",\"modbus_id\":8},{\"delta\":2,\"nazwa\":\"ED_9376\",\"host\":\"10.0.0.132\",\"modbus_id\":2},{\"delta\":3,\"nazwa\":\"ED_9377\",\"host\":\"10.0.0.201\",\"modbus_id\":4},{\"delta\":4,\"nazwa\":\"ED_9375\",\"host\":\"10.0.0.203\",\"modbus_id\":9},{\"delta\":5,\"nazwa\":\"ED_9379\",\"host\":\"10.0.0.204\",\"modbus_id\":5},{\"delta\":6,\"nazwa\":\"ED_9374\",\"host\":\"10.0.0.203\",\"modbus_id\":3},{\"delta\":8,\"nazwa\":\"ED_9380\",\"host\":\"10.0.0.202\",\"modbus_id\":7},{\"delta\":9,\"nazwa\":\"ED_9381\",\"host\":\"10.0.0.203\",\"modbus_id\":6},{\"delta\":7,\"nazwa\":\"ED_9378\",\"host\":\"10.0.0.131\",\"modbus_id\":10}]","payloadType":"json","x":130,"y":180,"wires":[["7a7de6e0.750358"]]}]
let array = msg.payload;
for (let i=0; i<array.length;i++){
    node.send({
        "payload":{   
            'connectorType': 'TCP',
            'tcpHost': array[i].host,
            'tcpPort': '502',
            'unitId': array[i].modbus_id
        },
        'host':array[i].host,
        'unitId': array[i].modbus_id,
        'topic': 'a_topic'
        });
}
return null;
1 Like

Perfect!!!
Thank you!

One suggestion .. since you are the one creating that array why dont you add from the begining the information needed (msg.sequences) that the modbus-flex-sequencer will later need.

Also its better to remove the second delay you have in your flow.. i dont think its needed since Modbus Config has its own queue of modbus commands.

image

Added a modbus-queue-info node to monitor how many commands are queued.

Example Flow (untested) :

[{"id":"a8b7f1f2.cd6ca","type":"modbus-flex-connector","z":"5847b7aa62131d37","name":"","maxReconnectsPerMinute":4,"emptyQueue":false,"showStatusActivities":true,"showErrors":false,"server":"3f55d524.d93c72","x":630,"y":1460,"wires":[["38cbf3bc.9ad9d4","ea1493b6.7819b"]]},{"id":"38cbf3bc.9ad9d4","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":1400,"wires":[]},{"id":"ea1493b6.7819b","type":"modbus-flex-sequencer","z":"5847b7aa62131d37","name":"sequencer 1","sequences":[{"name":"1","unitid":"1","fc":"FC4","address":"10009","quantity":"1"}],"server":"3f55d524.d93c72","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":910,"y":1460,"wires":[["60595176.16f1d8"],[]]},{"id":"60595176.16f1d8","type":"debug","z":"5847b7aa62131d37","name":"output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1090,"y":1460,"wires":[]},{"id":"e2d77eca.b789e","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":115,"y":1460,"wires":[["7a7de6e0.750358"]],"l":false},{"id":"7a7de6e0.750358","type":"function","z":"5847b7aa62131d37","name":"f1","func":"var devices = [\n   {\n      \"delta\": 1, \"nazwa\": \"ED_9373\", \"connectorType\": \"TCP\", \"tcpHost\": \"10.0.0.202\", \"unitId\": 8, \"sequences\": [\n         { \"name\": \"Foo\", \"unitid\": 8, \"fc\": 4, \"address\": 10009, \"quantity\": 1 },\n         { \"name\": \"Bar\", \"unitid\": 8, \"fc\": 4, \"address\": 10010, \"quantity\": 1 }\n      ]\n   },\n   {\n      \"delta\": 2, \"nazwa\": \"ED_9376\", \"connectorType\": \"TCP\", \"tcpHost\": \"10.0.0.132\", \"unitId\": 2, \"sequences\": [\n         { \"name\": \"Foo\", \"unitid\": 8, \"fc\": 4, \"address\": 10009, \"quantity\": 1 },\n         { \"name\": \"Bar\", \"unitid\": 8, \"fc\": 4, \"address\": 10010, \"quantity\": 1 }\n      ]\n   },\n   {\n      \"delta\": 3, \"nazwa\": \"ED_9377\", \"connectorType\": \"TCP\", \"tcpHost\": \"10.0.0.201\", \"unitId\": 4, \"sequences\": [\n         { \"name\": \"Foo\", \"unitid\": 8, \"fc\": 4, \"address\": 10009, \"quantity\": 1 },\n         { \"name\": \"Bar\", \"unitid\": 8, \"fc\": 4, \"address\": 10010, \"quantity\": 1 }\n      ]\n   },\n   {\n      \"delta\": 4, \"nazwa\": \"ED_9375\", \"connectorType\": \"TCP\", \"tcpHost\": \"10.0.0.203\", \"unitId\": 9, \"sequences\": [\n         { \"name\": \"Foo\", \"unitid\": 8, \"fc\": 4, \"address\": 10009, \"quantity\": 1 },\n         { \"name\": \"Bar\", \"unitid\": 8, \"fc\": 4, \"address\": 10010, \"quantity\": 1 }\n      ]\n   },\n   {\n      \"delta\": 5, \"nazwa\": \"ED_9379\", \"connectorType\": \"TCP\", \"tcpHost\": \"10.0.0.204\", \"unitId\": 5, \"sequences\": [\n         { \"name\": \"Foo\", \"unitid\": 8, \"fc\": 4, \"address\": 10009, \"quantity\": 1 },\n         { \"name\": \"Bar\", \"unitid\": 8, \"fc\": 4, \"address\": 10010, \"quantity\": 1 }\n      ]\n   },\n   {\n      \"delta\": 6, \"nazwa\": \"ED_9374\", \"connectorType\": \"TCP\", \"tcpHost\": \"10.0.0.203\", \"unitId\": 3, \"sequences\": [\n         { \"name\": \"Foo\", \"unitid\": 8, \"fc\": 4, \"address\": 10009, \"quantity\": 1 },\n         { \"name\": \"Bar\", \"unitid\": 8, \"fc\": 4, \"address\": 10010, \"quantity\": 1 }\n      ]\n   },\n   {\n      \"delta\": 8, \"nazwa\": \"ED_9380\", \"connectorType\": \"TCP\", \"tcpHost\": \"10.0.0.202\", \"unitId\": 7, \"sequences\": [\n         { \"name\": \"Foo\", \"unitid\": 8, \"fc\": 4, \"address\": 10009, \"quantity\": 1 },\n         { \"name\": \"Bar\", \"unitid\": 8, \"fc\": 4, \"address\": 10010, \"quantity\": 1 }\n      ]\n   },\n   {\n      \"delta\": 9, \"nazwa\": \"ED_9381\", \"connectorType\": \"TCP\", \"tcpHost\": \"10.0.0.203\", \"unitId\": 6, \"sequences\": [\n         { \"name\": \"Foo\", \"unitid\": 8, \"fc\": 4, \"address\": 10009, \"quantity\": 1 },\n         { \"name\": \"Bar\", \"unitid\": 8, \"fc\": 4, \"address\": 10010, \"quantity\": 1 }\n      ]\n   },\n   {\n      \"delta\": 7, \"nazwa\": \"ED_9378\", \"connectorType\": \"TCP\", \"tcpHost\": \"10.0.0.131\", \"unitId\": 10, \"sequences\": [\n         { \"name\": \"Foo\", \"unitid\": 8, \"fc\": 4, \"address\": 10009, \"quantity\": 1 },\n         { \"name\": \"Bar\", \"unitid\": 8, \"fc\": 4, \"address\": 10010, \"quantity\": 1 }\n      ]\n   }];\n\n\n\n//msg.payload = { 'connectorType': 'TCP', 'tcpHost': '127.0.0.1', 'tcpPort': '10502', 'unitId': 2 } return msg\n\n\ndevices.forEach(dev => {\n\n   node.send({\n      \"topic\": dev.nazwa,\n      \"payload\": {\n         \"connectorType\" : dev.connectorType,\n         \"tcpHost\": dev.tcpHost,\n         \"tcpPort\": \"502\",\n         \"unitId\": dev.unitId\n      },\n      \"sequences\": dev.sequences\n   });\n\n})\n\n\n\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":210,"y":1460,"wires":[["32ebc461.d791fc","9a8b464d.122a98"]]},{"id":"32ebc461.d791fc","type":"debug","z":"5847b7aa62131d37","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":310,"y":1400,"wires":[]},{"id":"9a8b464d.122a98","type":"delay","z":"5847b7aa62131d37","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"10","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"outputs":1,"x":400,"y":1460,"wires":[["a8b7f1f2.cd6ca"]]},{"id":"bf8eea292ceaa8d3","type":"modbus-queue-info","z":"5847b7aa62131d37","name":"","topic":"","unitid":1,"queueReadIntervalTime":1000,"lowLowLevel":25,"lowLevel":75,"highLevel":150,"highHighLevel":300,"server":"3f55d524.d93c72","errorOnHighLevel":false,"showStatusActivities":true,"updateOnAllQueueChanges":false,"updateOnAllUnitQueues":false,"x":1000,"y":1560,"wires":[[]]},{"id":"3f55d524.d93c72","type":"modbus-client","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"10.0.0.203","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"","unit_id":"0","commandDelay":"100","clientTimeout":"1000","reconnectOnTimeout":false,"reconnectTimeout":"2000","parallelUnitIdsAllowed":false}]

[EDIT]
Correction .. the second delay between Connector and Sequencer is needed after all, because the Modbus Flex Connector takes its time to actually change the modbus config settings.
For sure you cannot have both set at 10sec because by the time sequencer does its read a new message will arrive at connector messing up the settings .. and all hell will brake lose.
You have to play with the timing of the 1st and 2nd delay. the 2nd has to be less

I thinking how to instead of delay use status.

i thought about it but this would make it even more complicated

the only way i can think that this could work is to save the array of dynamic Modbus configurations in Context and when the Status changes to "connected" array.pop() a config from the array and then send it to Sequencer.

One problem is .. lets say you have 10-20 devices and the 3rd one is offline for some reason ...
What happens then ? the whole logic breaks since we are supposed to be waiting for a connected status ?

It would be interesting to see if you manage to find some solution.

Personally i came to terms that if i had many devices i would do a separate configuration for each one,
to have better control over its errors, disconnects, replies (that may need to be passed from buffer-parser).

I have a lot of devices, but I want to configure in database and read from node-red.
I think that best method is use outside library modbus in node-red

const modbus = require('modbus-tcp-ip')
const device = modbus(ipAddress,port,unitId)

but I haven't added libraries yet