# Count the messages

**URL:** <https://discourse.nodered.org/t/count-the-messages/47412>\
**Category:** General\
**Created:** [20 June 2021 21:31 UTC](https://discourse.nodered.org/t/count-the-messages/47412 "2021-06-20T21:31:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [20 June 2021 21:31 UTC](https://discourse.nodered.org/t/count-the-messages/47412/1 "2021-06-20T21:31:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [20 June 2021 21:59 UTC](https://discourse.nodered.org/t/count-the-messages/47412/2 "2021-06-20T21:59:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [20 June 2021 22:32 UTC](https://discourse.nodered.org/t/count-the-messages/47412/3 "2021-06-20T22:32:09Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 June 2021 08:06 UTC](https://discourse.nodered.org/t/count-the-messages/47412/4 "2021-06-21T08:06:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 June 2021 09:09 UTC](https://discourse.nodered.org/t/count-the-messages/47412/5 "2021-06-21T09:09:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [21 June 2021 09:27 UTC](https://discourse.nodered.org/t/count-the-messages/47412/6 "2021-06-21T09:27:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [21 June 2021 09:36 UTC](https://discourse.nodered.org/t/count-the-messages/47412/7 "2021-06-21T09:36:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 June 2021 09:44 UTC](https://discourse.nodered.org/t/count-the-messages/47412/8 "2021-06-21T09:44:56Z")

</div>

Like this?

![b7bdBAio5i](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1bf1905598cbba83f7218f2559ee68a165ab6336.gif)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 June 2021 09:45 UTC](https://discourse.nodered.org/t/count-the-messages/47412/9 "2021-06-21T09:45:25Z")

</div>

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](https://flows.nodered.org/node/node-red-contrib-counter) will help.

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [21 June 2021 10:41 UTC](https://discourse.nodered.org/t/count-the-messages/47412/10 "2021-06-21T10:41:39Z")

</div>

Thanks, It is working. How can I show the "count of payload" in dashboard?  
 ![sug](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/2407702eae766d817fa0ad05ac902fdb1067d88f.jpeg)

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [21 June 2021 10:42 UTC](https://discourse.nodered.org/t/count-the-messages/47412/11 "2021-06-21T10:42:35Z")

</div>

Thanks for your kindness.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 June 2021 10:48 UTC](https://discourse.nodered.org/t/count-the-messages/47412/12 "2021-06-21T10:48:44Z")

</div>

> [@usmanEmder](#):
>
> How can I show the "count of payload" in dashboard?

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

![vBuG0Z3t1c](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13143fc6875200bbd4641dc8141d06eb08844ee2.gif)

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [21 June 2021 10:58 UTC](https://discourse.nodered.org/t/count-the-messages/47412/13 "2021-06-21T10:58:48Z")

</div>

Thanks again 👌

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 July 2021 10:59 UTC](https://discourse.nodered.org/t/count-the-messages/47412/14 "2021-07-05T10:59:26Z")

</div>

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