Count the messages

Hello there,

I need to count the messages (i.e. "0" or "1" but I need to consider only when msg.payload = 1), and when the message counter will equal to 16 then the system will automatically take other action. I had used "for loop" but it is not working. Does anyone have any suggestions.

Why not post your flow, so we can see why it is not working...

First of all thanks for your kindness, In "function node" I wrote the following code lines

for (var i=1; i<17; i++){
    node.send({payload:i})
}
return msg;

But I thing it is a time counter and it did not fulfill my requirement.

I am confused. You ask how to count messages, but the function you posted is sending messages, not counting them.

Hi @usmanEmder

You are not 100% clear.

  • Should this counter reset if a message with msg.payload == 0 arrives?
    • e.g. if you get 5 msg.payload == 1 the counter would reach 5, however if the next message was msg.payload == 0 should the counter reset to zero?
  • Should this counter reset when count >= 16 or keep counting up?
  • Should this counter be able to be reset (by some other message) before it reaches 16 / after it reaches 16?
  • Where you say "automatically take other action" do you mean you want the flow to go 1 way while count is < 16 and take a different path when the value is >= 16?

Yes, actually... I thought that it will work but it is a time counter instead of msg.payload count.

Yes, after counting 16 messages (count only when msg.payload=1, while possibility of message is 0, or 1) the counter will generate msg.payload "1" to take further action. And the counter will also restart.

Like this?

b7bdBAio5i

[{"id":"315086cb1d9bff21","type":"function","z":"4b3f21a3.ba434","name":"","func":"\nlet msgCount = context.get(\"msgCount\") || 0;//get remembered count\nlet sendMessage = false;\n\nif (msg.payload === 1) {\n    msgCount++; //increment count\n    node.status(\"Count of payload 1: \" + msgCount);\n    if (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}\n\ncontext.set(\"msgCount\", msgCount); //remember count\n\nif(sendMessage) {\n    return msg;//send msg to next node\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1220,"y":680,"wires":[["9bd4724dbaf29a4e"]]},{"id":"9bd4724dbaf29a4e","type":"debug","z":"4b3f21a3.ba434","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1400,"y":680,"wires":[]},{"id":"57670cb0da43a95c","type":"inject","z":"4b3f21a3.ba434","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":1050,"y":660,"wires":[["315086cb1d9bff21"]]},{"id":"7c06fe830b7a1ebf","type":"inject","z":"4b3f21a3.ba434","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":1050,"y":700,"wires":[["315086cb1d9bff21"]]}]

To only count messages with payload of 1 insert a Switch node set to only pass the messages you want to count. Note that you keep changing between strings ("1") and numbers (1) make sure you know which you mean. For the counting and reset then perhaps node-red-contrib-counter (node) - Node-RED will help.

Thanks, It is working. How can I show the "count of payload" in dashboard?
sug

Thanks for your kindness.

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

Thanks again :ok_hand:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.