# Help with contexts please

**URL:** https://discourse.nodered.org/t/help-with-contexts-please/23602
**Category:** General
**Created:** [25 March 2020 08:23 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602 "2020-03-25T08:23:35Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)
#### Post date: [25 March 2020 08:23 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/1 "2020-03-25T08:23:35Z")

</div>

Hi there, I'm new to Node-Red, I'm having fun ! 😊 $

Please advice : I'm trying to create a programmable and resettable timer. I'm using the node-red-contrib-loop-processing nodes.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/54a09413697f024cf4856e2fb6329272a1168be4.png)

The counter works, but if I use the inject node a second time (to restart the counter) I see that there is actually a second counter running. I understand that it is related to a second \_msgid and that I should create a global or flow variable but I don't understand how.

Could someone put me on the right track ?

Thanks

Matthieu

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [25 March 2020 08:26 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/2 "2020-03-25T08:26:42Z")

</div>

can you post your flow code please?

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

that would be great  
Johannes

---

<div class="post-metadata">

### Author: ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)
#### Post date: [25 March 2020 08:29 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/3 "2020-03-25T08:29:40Z")

</div>

Sure, thanks !

Here you are :

```auto
[{"id":"bab1d11c.c9df7","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"27bad9ee.78950e","type":"inject","z":"bab1d11c.c9df7","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":260,"wires":[["e69d0cba.2a1168"]]},{"id":"36c3bd3f.70bf42","type":"delay","z":"bab1d11c.c9df7","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":620,"y":340,"wires":[["e69d0cba.2a1168"]]},{"id":"b7650339.95175","type":"ui_text","z":"bab1d11c.c9df7","group":"232a7e45.e895ea","order":1,"width":0,"height":0,"name":"","label":"Counter = ","format":"{{msg.payload}}","layout":"row-spread","x":740,"y":200,"wires":[]},{"id":"4444d80f.c7693","type":"debug","z":"bab1d11c.c9df7","name":"DONE","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":730,"y":260,"wires":[]},{"id":"e69d0cba.2a1168","type":"while-loop","z":"bab1d11c.c9df7","name":"","condi":"msg.payload == 0","x":510,"y":260,"wires":[["2735fb45.ca497c","b7650339.95175"],["4444d80f.c7693"]]},{"id":"2735fb45.ca497c","type":"change","z":"bab1d11c.c9df7","name":"decrement payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload - 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":340,"wires":[["36c3bd3f.70bf42"]]},{"id":"232a7e45.e895ea","type":"ui_group","name":"Group 1","tab":"6efc8e67.a67ff8","order":1,"disp":true,"width":6},{"id":"6efc8e67.a67ff8","type":"ui_tab","name":"Tab 2","icon":"dashboard","order":1}]

```

---

<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: [25 March 2020 08:58 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/4 "2020-03-25T08:58:14Z")

</div>

Try this, just after the `inject` node, add a `change` node and set global.counter to 0

---

<div class="post-metadata">

### Author: ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)
#### Post date: [25 March 2020 09:10 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/5 "2020-03-25T09:10:25Z")

</div>

Thanks ! I guess you mean setting `global.counter` to `msg.payload` ?

I tried this :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ace0e63f68d15547df19ebd33d262f643d189be6.png)

```auto
[{"id":"bab1d11c.c9df7","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"27bad9ee.78950e","type":"inject","z":"bab1d11c.c9df7","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":70,"y":240,"wires":[["c51871cc.9de9a"]]},{"id":"36c3bd3f.70bf42","type":"delay","z":"bab1d11c.c9df7","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":620,"y":340,"wires":[["e69d0cba.2a1168"]]},{"id":"b7650339.95175","type":"ui_text","z":"bab1d11c.c9df7","group":"232a7e45.e895ea","order":1,"width":0,"height":0,"name":"","label":"Counter = ","format":"{{msg.payload}}","layout":"row-spread","x":880,"y":180,"wires":[]},{"id":"4444d80f.c7693","type":"debug","z":"bab1d11c.c9df7","name":"DONE","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":730,"y":260,"wires":[]},{"id":"e69d0cba.2a1168","type":"while-loop","z":"bab1d11c.c9df7","name":"","condi":"global.counter == 0","x":510,"y":260,"wires":[["2735fb45.ca497c","dfb489e0.84369"],["4444d80f.c7693"]]},{"id":"2735fb45.ca497c","type":"change","z":"bab1d11c.c9df7","name":"decrement counter","rules":[{"t":"set","p":"counter","pt":"global","to":"gobal.counter - 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":340,"wires":[["36c3bd3f.70bf42"]]},{"id":"c51871cc.9de9a","type":"change","z":"bab1d11c.c9df7","name":"msg.payload --> global.counter","rules":[{"t":"set","p":"counter","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":240,"wires":[["e69d0cba.2a1168"]]},{"id":"dfb489e0.84369","type":"change","z":"bab1d11c.c9df7","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"counter","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":180,"wires":[["b7650339.95175","acde071e.1280d8"]]},{"id":"acde071e.1280d8","type":"debug","z":"bab1d11c.c9df7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":900,"y":140,"wires":[]},{"id":"232a7e45.e895ea","type":"ui_group","name":"Group 1","tab":"6efc8e67.a67ff8","order":1,"disp":true,"width":6},{"id":"6efc8e67.a67ff8","type":"ui_tab","name":"Tab 2","icon":"dashboard","order":1}]

```

But it looks like global.counter is not even created.

---

<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: [25 March 2020 09:12 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/6 "2020-03-25T09:12:11Z")

</div>

Nope, set global.counter to 0  
 ![Screen Shot 2020-03-25 at 5.11.27 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c4345a07f8b7f8952c0b4b770451775543b8cac.png)  
this should reset the counter used the loop node

---

<div class="post-metadata">

### Author: ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)
#### Post date: [25 March 2020 09:13 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/7 "2020-03-25T09:13:04Z")

</div>

Thanks @zenofmud ! It works ! How did you know that this variable was used by the while loop node ?

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [25 March 2020 09:54 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/8 "2020-03-25T09:54:19Z")

</div>

Saw it to late that @zenofmud already answered your question . But have a look at a different approach using a function node i wrote in the mean time anyway:

 ![Screenshot 2020-03-25 at 10.53.35](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f496fc7b3074fa906c4306ae81e5e007055f5d1.png)

```auto
[{"id":"e55319fd.75bac","type":"inject","z":"f954564f.03e718","name":"","topic":"start","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":780,"wires":[["f07ada4b.c33cb8"]]},{"id":"c1fd7048.fbcb48","type":"delay","z":"f954564f.03e718","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":840,"wires":[["f07ada4b.c33cb8"]]},{"id":"f07ada4b.c33cb8","type":"function","z":"f954564f.03e718","name":"decrement","func":"const previouspayload = context.get(\"previous_payload\");\nif(msg.topic == \"start\" && msg.payload != previouspayload){\n msg.topic = \"counting\";\n context.set(\"startvalue\", msg.payload);\n context.set(\"previous_payload\", msg.payload);\n context.set(\"newcount\", true);\n return [msg, msg];\n} else if(msg.topic == \"counting\" && msg.payload > 1){\n const startvalue = context.get(\"startvalue\");\n const newcount = context.get(\"newcount\");\n let countvalue = previouspayload;\n if(newcount && msg.payload != countvalue) return null;\n context.set(\"newcount\", false);\n msg.payload = countvalue - 1;\n context.set(\"previous_payload\", msg.payload);\n return [msg, msg];\n} else if(msg.payload == 1){\n msg.payload = \"finished\";\n return [msg, null];\n}","outputs":2,"noerr":0,"x":510,"y":780,"wires":[["c3bf3c62.ac62a"],["c1fd7048.fbcb48"]]},{"id":"c3bf3c62.ac62a","type":"debug","z":"f954564f.03e718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":780,"wires":[]},{"id":"ccff5a19.ae50c8","type":"inject","z":"f954564f.03e718","name":"","topic":"start","payload":"11","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":840,"wires":[["f07ada4b.c33cb8"]]},{"id":"ee085092.8ec72","type":"inject","z":"f954564f.03e718","name":"","topic":"start","payload":"16","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":720,"wires":[["f07ada4b.c33cb8"]]}]

```

you can throw any number with a msg.topic of start at this and it will countdown. It will start a new countdown if it receives a new number with a start topic and only ever does one count at a time.  
Best regards Johannes

---

<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: [25 March 2020 09:56 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/9 "2020-03-25T09:56:16Z")

</div>

Actually my solution doesn't work. The author of the node should impliment some way to `break` out of the node (I added an RFE to the GitHub of the node)

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [25 March 2020 10:03 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/10 "2020-03-25T10:03:49Z")

</div>

I added a stop function to mine

 ![Screenshot 2020-03-25 at 11.01.02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f1346c3febb1cbe4fee90344bd5d4503eb4e7a1.png)

```auto
[{"id":"e55319fd.75bac","type":"inject","z":"f954564f.03e718","name":"","topic":"start","payload":"22","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":570,"y":700,"wires":[["f07ada4b.c33cb8"]]},{"id":"c1fd7048.fbcb48","type":"delay","z":"f954564f.03e718","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":860,"y":760,"wires":[["f07ada4b.c33cb8"]]},{"id":"f07ada4b.c33cb8","type":"function","z":"f954564f.03e718","name":"decrement","func":"const previouspayload = context.get(\"previous_payload\");\nconst stop = context.get(\"stop\") || false;\nif(msg.payload == \"stop\"){\n context.set(\"stop\", true);\n return [msg, null];\n} else if(msg.topic == \"start\" && msg.payload != previouspayload){\n msg.topic = \"counting\";\n context.set(\"startvalue\", parseFloat(msg.payload));\n context.set(\"previous_payload\", parseFloat(msg.payload));\n context.set(\"newcount\", true);\n context.set(\"stop\", false);\n return [msg, msg];\n} else if(msg.topic == \"counting\" && msg.payload > 1 && !stop){\n const startvalue = context.get(\"startvalue\");\n const newcount = context.get(\"newcount\");\n let countvalue = previouspayload;\n if(newcount && msg.payload != countvalue) return null;\n context.set(\"newcount\", false);\n msg.payload = countvalue - 1;\n context.set(\"previous_payload\", msg.payload);\n return [msg, msg];\n} else if(msg.payload == 1 && !stop){\n msg.payload = \"finished\";\n return [msg, null];\n}","outputs":2,"noerr":0,"x":850,"y":700,"wires":[["c3bf3c62.ac62a"],["c1fd7048.fbcb48"]]},{"id":"c3bf3c62.ac62a","type":"debug","z":"f954564f.03e718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1110,"y":700,"wires":[]},{"id":"ccff5a19.ae50c8","type":"inject","z":"f954564f.03e718","name":"","topic":"start","payload":"11","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":570,"y":760,"wires":[["f07ada4b.c33cb8"]]},{"id":"ee085092.8ec72","type":"inject","z":"f954564f.03e718","name":"","topic":"start","payload":"16","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":570,"y":640,"wires":[["f07ada4b.c33cb8"]]},{"id":"9b97d08b.29d83","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"stop","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":570,"y":820,"wires":[["f07ada4b.c33cb8"]]}]

```

if you send a msg.payload of "stop" the running counter will be stopped

---

<div class="post-metadata">

### Author: ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)
#### Post date: [25 March 2020 10:17 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/11 "2020-03-25T10:17:35Z")

</div>

Thanks so much both of you ! I'll use your function node @JGKK 😊 I like to avoid function nodes for readability but in this case this looks the way to go.

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [25 March 2020 10:24 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/12 "2020-03-25T10:24:45Z")

</div>

Here is one with ui:

 ![Screenshot 2020-03-25 at 11.23.04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/cea92d4af294bbddaf46da521d105d4cb790d2d9.png)  
 ![Screenshot 2020-03-25 at 11.24.24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81c806d2ab0ebf64f031da03e4b40f682549b55a.png)

```auto
[{"id":"c1fd7048.fbcb48","type":"delay","z":"f954564f.03e718","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":460,"y":760,"wires":[["f07ada4b.c33cb8"]]},{"id":"f07ada4b.c33cb8","type":"function","z":"f954564f.03e718","name":"decrement","func":"const previouspayload = context.get(\"previous_payload\");\nconst stop = context.get(\"stop\") || false;\nif(msg.payload == \"stop\"){\n context.set(\"stop\", true);\n msg.payload = \"stopped\";\n return [msg, null];\n} else if(msg.topic == \"start\" && msg.payload != previouspayload){\n msg.topic = \"counting\";\n context.set(\"previous_payload\", parseFloat(msg.payload));\n context.set(\"newcount\", true);\n context.set(\"stop\", false);\n return [msg, msg];\n} else if(msg.topic == \"counting\" && msg.payload > 1 && !stop){\n const newcount = context.get(\"newcount\");\n let countvalue = previouspayload;\n if(newcount && msg.payload != countvalue) return null;\n context.set(\"newcount\", false);\n msg.payload = countvalue - 1;\n context.set(\"previous_payload\", msg.payload);\n return [msg, msg];\n} else if(msg.payload == 1 && !stop){\n msg.payload = \"finished\";\n return [msg, null];\n}","outputs":2,"noerr":0,"x":450,"y":700,"wires":[["757c3701.47806"],["c1fd7048.fbcb48"]]},{"id":"78a83b75.26d8c4","type":"ui_numeric","z":"f954564f.03e718","name":"","label":"set start count","tooltip":"","group":"d619b4d0.780aa8","order":1,"width":0,"height":0,"wrap":false,"passthru":false,"topic":"","format":"{{msg.payload}}","min":0,"max":"100","step":1,"x":160,"y":580,"wires":[["463c0cad.ce804c"]]},{"id":"c99a569d.8a0998","type":"ui_button","z":"f954564f.03e718","name":"","group":"d619b4d0.780aa8","order":2,"width":0,"height":0,"passthru":false,"label":"start countdown","tooltip":"","color":"","bgcolor":"","icon":"","payload":"startcount","payloadType":"flow","topic":"start","x":160,"y":700,"wires":[["f07ada4b.c33cb8","1ac5dad4.5007ed"]]},{"id":"463c0cad.ce804c","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"set","p":"startcount","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":580,"wires":[[]]},{"id":"757c3701.47806","type":"ui_text","z":"f954564f.03e718","group":"d619b4d0.780aa8","order":3,"width":0,"height":0,"name":"","label":"Countdown","format":"{{msg.payload}}","layout":"row-spread","x":630,"y":700,"wires":[]},{"id":"1ac5dad4.5007ed","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":640,"wires":[["78a83b75.26d8c4"]]},{"id":"cdc32fdf.291988","type":"ui_button","z":"f954564f.03e718","name":"","group":"d619b4d0.780aa8","order":4,"width":0,"height":0,"passthru":false,"label":"Stop countdown","tooltip":"","color":"","bgcolor":"","icon":"","payload":"stop","payloadType":"str","topic":"","x":170,"y":780,"wires":[["f07ada4b.c33cb8"]]},{"id":"d619b4d0.780aa8","type":"ui_group","z":"","name":"Countdown","tab":"b2566dcf.0415","order":2,"disp":true,"width":"6","collapse":false},{"id":"b2566dcf.0415","type":"ui_tab","z":"","name":"Countdown","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

```

---

<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: [8 April 2020 10:24 UTC](https://discourse.nodered.org/t/help-with-contexts-please/23602/13 "2020-04-08T10:24:47Z")

</div>

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