Count the messages

One way could be to add a 2nd output...

vBuG0Z3t1c

[{"id":"226f9f18a6afc487","type":"function","z":"553814a2.1248ec","name":"","func":"\nlet msgCount = context.get(\"msgCount\") || 0;//get remembered count\nlet sendMessage = false;\nlet countMessage = \"\";\nif (msg.payload === 1) {\n    msgCount++; //increment count\n}\n\ncountMessage = \"Count of payload 1: \" + msgCount;\n\nif (msgCount >= 16)  {\n    msgCount = 0;//reset counter\n    sendMessage = true;//instruct code to send the msg\n    msg.payload = \"1\"; //set payload to a string of \"1\" as you requested\n}\n\ncontext.set(\"msgCount\", msgCount); //remember count\nnode.status(countMessage);\nlet msgWithCount = { payload: countMessage };\n\nif(sendMessage) {\n    return [msg, msgWithCount];\n} else {\n    return [null, msgWithCount];\n}\n\n\n\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1760,"y":80,"wires":[["5999900b2df185a4"],["e6140c609f34d805"]]},{"id":"5999900b2df185a4","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1930,"y":40,"wires":[]},{"id":"0f206903f17e01f9","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":1590,"y":60,"wires":[["226f9f18a6afc487"]]},{"id":"08c8d12fc86a3f32","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":1590,"y":100,"wires":[["226f9f18a6afc487"]]},{"id":"e6140c609f34d805","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1930,"y":120,"wires":[]}]
2 Likes