# Count Messages - Reset externally

**URL:** <https://discourse.nodered.org/t/count-messages-reset-externally/49560>\
**Category:** General\
**Created:** [10 August 2021 08:40 UTC](https://discourse.nodered.org/t/count-messages-reset-externally/49560 "2021-08-10T08:40:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [10 August 2021 08:40 UTC](https://discourse.nodered.org/t/count-messages-reset-externally/49560/1 "2021-08-10T08:40:42Z")

</div>

> [@Count the messages](https://discourse.nodered.org/t/count-the-messages/47412/12):
>
> 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 …

@Steve-Mcl

Hi, is there a way to reset the function code externally, for example, by clicking inject 0, the count should reset

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 August 2021 09:44 UTC](https://discourse.nodered.org/t/count-messages-reset-externally/49560/2 "2021-08-10T09:44:29Z")

</div>

> [@smanjunath211](#):
>
> is there a way to reset the function code externally, for example, by clicking inject 0, the count should reset

If you mean can you put a button on a node-red dashboard to do the reset, the answer is YES.

If you mean a physical button, the answer is still YES, you just need to connect it up with the right hardware and use the correct node to read the incoming data.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [10 August 2021 10:06 UTC](https://discourse.nodered.org/t/count-messages-reset-externally/49560/3 "2021-08-10T10:06:39Z")

</div>

Sorry for not being clear. i want to understand the changes i have to do in function code to read the 'reset' command, here in the example by pressing the inject node with a ' 0 '

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 August 2021 10:24 UTC](https://discourse.nodered.org/t/count-messages-reset-externally/49560/4 "2021-08-10T10:24:00Z")

</div>

Add a new `inject` node setting msg.payload to 'reset' then add some code to the start of the `function` node to

1. test msg.payload for 'reset' - if true
2. store 0 in the context variable
3. maybe use node.status to display a msg undetr the node
4. do a 'return;' to exit the function and not send anything down the wire.

---

<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:** [9 October 2021 10:24 UTC](https://discourse.nodered.org/t/count-messages-reset-externally/49560/5 "2021-10-09T10:24:41Z")

</div>

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