# Reset counter values

**URL:** <https://discourse.nodered.org/t/reset-counter-values/46782>\
**Category:** General\
**Created:** [6 June 2021 16:51 UTC](https://discourse.nodered.org/t/reset-counter-values/46782 "2021-06-06T16:51:48Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [6 June 2021 16:51 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/1 "2021-06-06T16:51:49Z")

</div>

Hello,

In this flow, there are randomly generated numbers between 20 and 60 in function "Data Generation". Functions' output is connected to a trigger node which configuration is shown bellow. Trigger's output is connected to a function "Fire Off" (Α Mister from this forum helped me a lot with this part). After that there is a debug node which shows the message. What I am trying to accomplish is: generate a random number between 20 and 60, then decrease this number by 1 every 1 second and when the value becomes 20 the next value I want to be the random generated number between 20 and 60. The problem is that flow runs only once and that's why I am trying to insert something like a reset to the value. Are there any ideas on how to make it happen?

Thanks for your time, appreciate your help!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/5914612e005229def118b2b299529f619581e68b.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04bc4c792bc20fe81c2009e63b3d120213a0280d.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9a76faf6d5032be1b3d7ab24e9eced922d9c6402.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/18c9f96dc4dd5b28ac3fefa98049219ad661904a.png)

Finished Flow

```auto
[{"id":"ff6cdaba.0c2748","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"1e5e334b.eee44d","type":"function","z":"ff6cdaba.0c2748","name":"Data Generation","func":"flow.set(\"counter\", Math.round(Math.random()*(60 - 20) + 20))//Math.floor(Math.random()*41)+20)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":280,"wires":[["4b796f69.bc785"]]},{"id":"31b6afa1.d6b6e","type":"inject","z":"ff6cdaba.0c2748","name":"Sensor","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":530,"y":240,"wires":[["1e5e334b.eee44d"]]},{"id":"ded18de2.933c5","type":"function","z":"ff6cdaba.0c2748","name":"Fire Off","func":"var counter = flow.get('counter')\nmsg.payload = counter;\nif(counter==20)\n{\ncounter=Math.floor(Math.round(Math.random()*(60 - 20) + 20));\n}\nelse\n{\ncounter--;\n}\nflow.set(\"counter\",counter);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":360,"wires":[["212b659d.77354a"]]},{"id":"212b659d.77354a","type":"debug","z":"ff6cdaba.0c2748","name":"Off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":400,"wires":[]},{"id":"4b796f69.bc785","type":"trigger","z":"ff6cdaba.0c2748","name":"","op1":"","op2":"","op1type":"pay","op2type":"payl","duration":"-500","extend":true,"overrideDelay":false,"units":"ms","reset":"20","bytopic":"all","topic":"topic","outputs":1,"x":640,"y":320,"wires":[["ded18de2.933c5"]]}]

```

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [6 June 2021 17:22 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/2 "2021-06-06T17:22:23Z")

</div>

If you would post your whole flow that might help. But... your title says reset value but in your description you say you want to clear the value. That confuses me, what exactly do you want it to do? Set to zero , to me that is clearing it or do you want to set it back to 60 or whatever the random number was?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 June 2021 17:22 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/3 "2021-06-06T17:22:32Z")

</div>

you mean something like this

```auto
[{"id":"5f6dc1a8.610bb8","type":"inject","z":"c74669a0.6a34f8","name":"start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":3280,"wires":[["13c80fc2.f0da18"]]},{"id":"13c80fc2.f0da18","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"$floor($random()*41)+20","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":3320,"wires":[["6257365b.69ed28"]]},{"id":"6257365b.69ed28","type":"trigger","z":"c74669a0.6a34f8","name":"","op1":"count","op2":"20","op1type":"flow","op2type":"num","duration":"-1","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":540,"y":3320,"wires":[["51c55056.111468"]]},{"id":"51c55056.111468","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"count","tot":"flow"},{"t":"set","p":"count","pt":"flow","to":"$flowContext(\"count\") = 20 ? ($floor($random()*41)+20) : $flowContext(\"count\") - 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":3320,"wires":[["2ada69b7.748bbe"]]},{"id":"fd2ebb80.2090b8","type":"inject","z":"c74669a0.6a34f8","name":"stop","props":[{"p":"reset","v":"true","vt":"bool"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":140,"y":3360,"wires":[["6257365b.69ed28"]]},{"id":"2ada69b7.748bbe","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":970,"y":3320,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [6 June 2021 17:28 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/4 "2021-06-06T17:28:08Z")

</div>

Excuse me for my wrong formulation. What I want to do is the following: when the value becomes 20 the next value I want to be the random generated number between 20 and 60. I have updated the text. Really thanks for time !

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [6 June 2021 17:29 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/5 "2021-06-06T17:29:58Z")

</div>

Looks like @E1cid has got your answer then

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [6 June 2021 17:33 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/6 "2021-06-06T17:33:19Z")

</div>

Thanks for your reply! I mean something like this but is it possible to be implemented using function node instead of change?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 June 2021 17:36 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/7 "2021-06-06T17:36:59Z")

</div>

Sure  
In first function node create a random number and save to context  
in trigger use that context stored var  
in second function read context var , copy to payload, check if value is 20, if so regenerate random number, if not - 1, then save back to context var

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 June 2021 17:51 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/9 "2021-06-06T17:51:43Z")

</div>

> [@ChrisTs99](#):
>
> mplement the following in the first function  
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d99f4a1334f549783898dff1e838c7f5b3610f90.png)

That should be flow.set() not context.get(), as context is only availablein the node.

and use flow in second function

also remove the quotes around the Math.random as you are making a string

In second function if counter == 20 the you need to generate another random.

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [6 June 2021 17:51 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/10 "2021-06-06T17:51:45Z")

</div>

Excuse me again, I am new and I hardly understand. I implement the following in the first function  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d99f4a1334f549783898dff1e838c7f5b3610f90.png)  
and the second one  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/93f1d5eff1afc041baede0c4ba7e22f33bd9314a.png)  
What should I check again, because its not working.  
Thanks again for your time !

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [6 June 2021 17:56 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/11 "2021-06-06T17:56:19Z")

</div>

I see, there is an error when I replace context.get to flow.set  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/6/b6efc9da1463bcb93d4c79a79d15850b3aefea86.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f08d27490f856599825a2f3f083abdf9de9ce880.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 June 2021 17:57 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/12 "2021-06-06T17:57:29Z")

</div>

flow.set takes two arguments the name of the var and the value, you have only set one.  
You also still have quotes around your Math.Random()

You do not need to do counter = flow.set.

just  
flow.set("counter" , Math.floor(Math.random()\*41)+20)

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [6 June 2021 18:12 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/16 "2021-06-06T18:12:23Z")

</div>

Excuse me again, I deleted some replies because they were not formulated correctly. I managed to fix the first function.. can you please explain me how the second function should work once more ? Here is what I have implemented  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/18392942795614a81725a26f849421fc814b787d.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 June 2021 18:14 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/17 "2021-06-06T18:14:56Z")

</div>

the first flow.set should be flow.get  
remove the second msg.payload = counter;

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [6 June 2021 18:57 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/19 "2021-06-06T18:57:32Z")

</div>

Thanks a lot for your help ! I have one more question. Can you explain me what is the difference between use of ".msg" and ."flow"? I searched on google but still can't understand. Really appreciate it !

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 June 2021 19:02 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/20 "2021-06-06T19:02:14Z")

</div>

flow and global are context storage variables, these are stored and can be retrieved from other messages passing down the flow.

msg is the message passing down the flow, and is not available to other messages passing down the flow.

[Working with context : Node-RED](https://nodered.org/docs/user-guide/context#:~:text=What%20is%20context%3F,This%20is%20called%20'context').

[edit p.s. post your finished flow so others can see the end result and learn and use it.]

---

<div class="post-metadata">

**Author:** ![ChrisTs99](https://avatars.discourse-cdn.com/v4/letter/c/779978/32.png) [@ChrisTs99](https://discourse.nodered.org/u/ChrisTs99)\
**Post date:** [6 June 2021 20:20 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/21 "2021-06-06T20:20:47Z")

</div>

Hello again!

I posted the right code, and I have one more question... I tried using "context.get" and "context.set" but what I get returned is a "Not A Number" statement. So is "context" not working because it doesn't link the nodes that is attached? On the other hand, is "flow.get/set" working because it links every node that is attached?

Thanks for your time, appreciate your help buddy !

---

<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:** [20 June 2021 20:21 UTC](https://discourse.nodered.org/t/reset-counter-values/46782/22 "2021-06-20T20:21:19Z")

</div>

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