# A global context variable is getting changed and I can't find how/why

**URL:** https://discourse.nodered.org/t/a-global-context-variable-is-getting-changed-and-i-cant-find-how-why/32426
**Category:** General
**Created:** [4 September 2020 01:57 UTC](https://discourse.nodered.org/t/a-global-context-variable-is-getting-changed-and-i-cant-find-how-why/32426 "2020-09-04T01:57:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [4 September 2020 01:57 UTC](https://discourse.nodered.org/t/a-global-context-variable-is-getting-changed-and-i-cant-find-how-why/32426/1 "2020-09-04T01:57:30Z")

</div>

External node needed:

`node-red-contrib-fan`  
`node-red-contrib-counter`

It is handy to split flow into groups. Yes, I know the latest version allows that in the edit screen.  
I'm not up to speed with it and so use the `fan` node.

This is being typed _on the fly_. I don't know how to structure it in an easy way.  
You will see things in the flow which may make you ask: "Why?" Please read this all and I hope it will all make sense.

Scenario:  
I am building (another) clock.  
RasPi (headless) and s 20x4 LCD.

I know that is making my life difficult, but I seem to enjoy trying hard things.  
And it isn't that I want to drag anyone else into this, but I'm stuck.

I haven't even got the buttons connected yet.

At the top I build the menu structure and save it to _global.context_.

Forget about the next bit about setting the global context (two `inject` nodes and a `change` node)

Down a bit further is an `inject` node: **Menu**  
This calls up the menu on the display. Doesn't matter if you don't have one.

Going to the right you get to `change` node. (Start menu at line?) This is because I can only show 4 lines at a time.  
If there is more than 4, the menu is grouped into blocks of 4.  
That then goes into a `function` node that spits out 4 lines of text that are formatted with position data at the end. (`@x,y`)

That output is sent into the `counter` node that adds a `msg.count` to the messages. This is used to control the `y` position of the message. (See `template` node)

That then gets it's `topic` set and sent to the display.

Then, it loops back to the left to a `switch` node. When all 4 lines are shown, it goes to a `trigger` node. (Basically a delay, but indulge me doing it this way)

That then goes into another `function` node.  
That prints `>` at a position.  
Part of that is got from `global.LINE`. I'll get to that very shortly, as that is where the problems happen.

Just under this bit of code you will see two `inject` nodes. `Up` and `Down`.  
The `switch` nodes check that the MENU is being displayed.  
The next node is a `change` node. This sets `msg.payload` to `global.LINE`.  
Next is a `switch` node checking if `0` or `4` have been exceeded. (Lines on the display.)  
Pressing the `Down` 4 times the `switch` node sends the output to output #1.  
That goes down to the `+4` `function` node below.  
That adjusts the `global_index` to it's value +4 and saves it.

Then the .... _wires_ loop back to the left (and up) to re-do the menu.

It works to here.  
And you can press the `Down` button to move to the other 4 items.  
Yes, 3 of them are _dummies_.

# So the problem?

(Yes, I also get that the `up` and `down` seem back to front, but down moves the cursor down the screen - up in menu items.)

So: Page 2, the cursor is blinking away happily on the top line. (`0`)  
If you press the `Up` button, it goes back to the previous screen.  
But.... The cursor needs to be at the bottom.

Now it all starts to fall apart!

Just a bit further down, you see an `inject` and `function` node. The `inject` node pulses every 0.5 seconds.

That shows me `global.LINE`.

On the first menu page, as you press the `Down` button, you will see this increment.  
When it gets to the new page, it is back to `0`. Fair enough.

So on page 2, top line: You press the `Up` button.  
The display is wiped, the original 4 lines are printed and the cursor is supposed to be at `0,3`.

This is done by `function` node called `>` you saw before when the display had been given 4 lines.

`global.LINE` is changed to `3` if you look in the `-4` `function` node.  
You probably won't see it as it is reset to `0` and I don't know why.

There are 4 nodes on the flow (and nowhere else on any other flow on the machine) that adjust the `global.LINE` value.

They are the `-1`, `+1`, `+4` and `-4`.  
Yes, there are the other two at the bottom.  
One is simply showing you (`node.status`) on the `function` node connected to the repeating `inject` node, and the other one `SET` used to set `global.LINE` to `3` for testing.

# How do I see this is actually happening?

Towards the top you will see a lovely `inject` node called: **LOOK AT ME in the docs**.  
Just below it is a `link - in` node.

(If you want)  
Press the `Down` and `Up` buttons and see what is happening and how the node at the bottom is showing you the `LINE` value.  
Go to the second page and press the `Up` button..

Now: delete the `link` on that `link - in` node below the `inject` node.  
Press the `Down` button `4` times.

Look that `LINE` is showing.  
Then press the `Up` button.

You will see "Line value set to 3" clearly in the `debug` window.  
(That is to set the cursor on line .... 3)  
Then a "Starting 1" message. That is from the `function` node printing out the lines.  
The you see..... "Y is set to 0"  
And `Y` was just shown as set to `3`.

How does it get wiped?

Go to the second page of the _Menu_ and delete the afore mentioned `link`.  
Press the `Up` button.  
You can see `LINE` is still `3`.

Now go to the `inject` node "LOOK AT ME in the docs" and press it.  
`LINE` gets changed to `0`, and I don't know why.

```auto
[{"id":"df3a8af4.5db058","type":"comment","z":"51326072.289f98","name":"Menus *","info":"Build the menu and store it in global context.","x":230,"y":570,"wires":[]},{"id":"5ba016aa.9913a8","type":"inject","z":"51326072.289f98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":660,"wires":[["339ed550.edc292","8536c3d.4319f4"]]},{"id":"339ed550.edc292","type":"function","z":"51326072.289f98","name":"","func":"msg.menu = {\n 1:\"> Show clock/time\",\n 2:\"> Show alarms\",\n 3:\"> Edit time/date\",\n 4:\"> Edit alarm/s\",\n 5:\"> Alarm run time\",\n 6:\"> menu 6\",\n 7:\"> menu 7\",\n 8:\"> menu 8\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":660,"wires":[["57451be6.c4378c","965a4efd.751c18"]]},{"id":"57451be6.c4378c","type":"debug","z":"51326072.289f98","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":510,"y":660,"wires":[]},{"id":"965a4efd.751c18","type":"change","z":"51326072.289f98","name":"Global set","rules":[{"t":"set","p":"menu","pt":"global","to":"menu","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":700,"wires":[[]]},{"id":"cef8e62f.ce34","type":"link in","z":"51326072.289f98","name":"Menu","links":["e97ee73c.f15b9"],"x":225,"y":1060,"wires":[["fb8f4a36.d133a8"]]},{"id":"737bc492.63dee4","type":"change","z":"51326072.289f98","name":"CLS","rules":[{"t":"set","p":"payload","pt":"msg","to":"CLS","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":880,"y":1010,"wires":[["bc929d43.947888","5db4e9b3.848618","1a874592.53e5b2"]]},{"id":"bc929d43.947888","type":"link out","z":"51326072.289f98","name":"LCD Display CTL commands","links":["1e4ce1b3.80123e"],"x":1400,"y":1010,"wires":[],"l":true},{"id":"779bdb9.a9a22a4","type":"change","z":"51326072.289f98","name":"Start menu at line?","rules":[{"t":"set","p":"payload","pt":"msg","to":"menu_index","tot":"global"},{"t":"set","p":"menu","pt":"msg","to":"menu","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":1060,"wires":[["4492436a.0bba2c","7146361e.b95538"]]},{"id":"8536c3d.4319f4","type":"change","z":"51326072.289f98","name":"","rules":[{"t":"set","p":"menu_index","pt":"global","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":620,"wires":[[]]},{"id":"2834ad20.8140e2","type":"inject","z":"51326072.289f98","name":"Menu","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":1110,"wires":[["fb8f4a36.d133a8"]]},{"id":"79066ec1.f3c33","type":"debug","z":"51326072.289f98","name":"Message","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1420,"y":1210,"wires":[]},{"id":"4492436a.0bba2c","type":"function","z":"51326072.289f98","name":"Display 4 lines on screen","func":"var starting = parseInt(msg.payload) + 1;\nvar ending = starting + 4;\n\nnode.warn(\"Starting \" + starting)\n//node.warn(\"Ending \" + ending)\n//node.warn(msg.menu);\n\nif (starting < ending)\n{\n //\n pop();\n}\nreturn;\n\nfunction pop() {\n// node.warn(\"Loop\" + starting);\n// node.warn(\"Ending \" + ending);\n node.send({payload: msg.menu[starting]});\n starting +=1;\n if (starting < ending){\n pop();\n }\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1170,"y":1060,"wires":[["67e331bf.2fb5f8"]]},{"id":"7146361e.b95538","type":"debug","z":"51326072.289f98","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":880,"y":1110,"wires":[]},{"id":"bc9bd74f.b7b978","type":"inject","z":"51326072.289f98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":240,"y":770,"wires":[["c43ead33.c62478"]]},{"id":"61087487.918824","type":"inject","z":"51326072.289f98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":240,"y":800,"wires":[["c43ead33.c62478"]]},{"id":"c43ead33.c62478","type":"change","z":"51326072.289f98","name":"","rules":[{"t":"set","p":"menu_index","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":780,"wires":[["19a36f72.7c1c89"]]},{"id":"67e331bf.2fb5f8","type":"counter","z":"51326072.289f98","name":"","init":"-1","step":"1","lower":"","upper":"","mode":"increment","outputs":"1","x":1180,"y":1110,"wires":[["c2e1550c.c19d9"]]},{"id":"c2e1550c.c19d9","type":"template","z":"51326072.289f98","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{{payload}}}@0,{{{count}}}","output":"str","x":890,"y":1170,"wires":[["adac1b1.f2ae7e8"]]},{"id":"b93cb59b.e0c17","type":"link out","z":"51326072.289f98","name":"Message to LCD display","links":["402996d9.ab9ce"],"x":1660,"y":1170,"wires":[],"l":true},{"id":"5db4e9b3.848618","type":"debug","z":"51326072.289f98","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":960,"wires":[]},{"id":"b3239fab.cff44","type":"change","z":"51326072.289f98","name":"Reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":1110,"wires":[["67e331bf.2fb5f8"]]},{"id":"2f8423db.8f0644","type":"delay","z":"51326072.289f98","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1060,"y":1190,"wires":[["adac1b1.f2ae7e8"]]},{"id":"adac1b1.f2ae7e8","type":"fan","z":"51326072.289f98","name":"","x":1260,"y":1170,"wires":[["79066ec1.f3c33","c5b0f5e9.6feee","92c97542.7768f","bef2867.88ad778","df0e2ee3.af4ac8"]]},{"id":"c5b0f5e9.6feee","type":"debug","z":"51326072.289f98","name":"COUNT","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"count","targetType":"msg","statusVal":"","statusType":"auto","x":1420,"y":1250,"wires":[]},{"id":"19a36f72.7c1c89","type":"debug","z":"51326072.289f98","name":"STARTING AT","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":660,"y":780,"wires":[]},{"id":"df0e2ee3.af4ac8","type":"switch","z":"51326072.289f98","name":"Menu displayed","property":"count","propertyType":"msg","rules":[{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":500,"y":1240,"wires":[["25d98da4.8c2442","b41cb7e5.7df81"]]},{"id":"92c97542.7768f","type":"change","z":"51326072.289f98","name":"text","rules":[{"t":"set","p":"topic","pt":"msg","to":"text","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1410,"y":1170,"wires":[["b93cb59b.e0c17"]]},{"id":"c7b890b7.6d73d8","type":"change","z":"51326072.289f98","name":">","rules":[{"t":"set","p":"payload","pt":"msg","to":">@0,0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":1290,"wires":[["3c76d420.a1947c","92bb1bdc.5bbea"]]},{"id":"3c76d420.a1947c","type":"debug","z":"51326072.289f98","name":"Message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1030,"y":1290,"wires":[]},{"id":"25d98da4.8c2442","type":"debug","z":"51326072.289f98","name":"COUNT","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":540,"y":1290,"wires":[]},{"id":"b41cb7e5.7df81","type":"trigger","z":"51326072.289f98","name":"D","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"2500","extend":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":660,"y":1240,"wires":[["f5ba0671.28e6"]]},{"id":"975f857.2480c78","type":"change","z":"51326072.289f98","name":"Blink ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"BLK-ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1190,"y":1240,"wires":[["bc929d43.947888"]]},{"id":"87e0389e.935588","type":"change","z":"51326072.289f98","name":"Blink ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"BLK-ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":830,"wires":[[]]},{"id":"9b7af149.60fc58","type":"change","z":"51326072.289f98","name":"Blink OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"BLK-OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1180,"y":980,"wires":[["bc929d43.947888"]]},{"id":"35f294bd.361dbc","type":"inject","z":"51326072.289f98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1100,"y":930,"wires":[["9b7af149.60fc58"]]},{"id":"bef2867.88ad778","type":"change","z":"51326072.289f98","name":"Set What screen (menu)","rules":[{"t":"set","p":"SCREEN","pt":"global","to":"menu","tot":"str"},{"t":"set","p":"LINE","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1520,"y":1090,"wires":[[]]},{"id":"f24a5a0c.272178","type":"fan","z":"51326072.289f98","name":"","x":740,"y":1060,"wires":[["779bdb9.a9a22a4","b3239fab.cff44","737bc492.63dee4"]]},{"id":"fb8f4a36.d133a8","type":"fan","z":"51326072.289f98","name":"","x":310,"y":1060,"wires":[["f24a5a0c.272178","34fc7879.6b6c48"]]},{"id":"1b61102f.015f5","type":"comment","z":"51326072.289f98","name":"Check what screen is active *","info":"Needs to check what screen is active.\nLot of work needed here.\nBut for now not needed while testing menu.","x":530,"y":1040,"wires":[]},{"id":"1ff7c2cc.b3575d","type":"link in","z":"51326072.289f98","name":"Up","links":["1158824c.4f2fc6"],"x":525,"y":1390,"wires":[["482e8079.99e45"]]},{"id":"e79d0955.758548","type":"link in","z":"51326072.289f98","name":"Down","links":["c91ebda0.328b8"],"x":525,"y":1440,"wires":[["b7517494.8e4a68"]]},{"id":"482e8079.99e45","type":"switch","z":"51326072.289f98","name":"Menu?","property":"SCREEN","propertyType":"global","rules":[{"t":"eq","v":"menu","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":620,"y":1390,"wires":[["2ec7b704.c2cbb"]]},{"id":"b7517494.8e4a68","type":"switch","z":"51326072.289f98","name":"Menu?","property":"SCREEN","propertyType":"global","rules":[{"t":"eq","v":"menu","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":620,"y":1440,"wires":[["4e4994fd.d1df64"]]},{"id":"2ec7b704.c2cbb","type":"change","z":"51326072.289f98","name":"get global.LINE","rules":[{"t":"set","p":"payload","pt":"msg","to":"LINE","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":1390,"wires":[["2566ec3e.48963c","f3fe43f3.8f0d58"]]},{"id":"4e4994fd.d1df64","type":"change","z":"51326072.289f98","name":"get global.LINE","rules":[{"t":"set","p":"payload","pt":"msg","to":"LINE","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":1440,"wires":[["45726900.fc2e28","84902981.d2faf"]]},{"id":"8d867ec4.eac3d","type":"function","z":"51326072.289f98","name":"+ 1","func":"var x = msg.payload + 1;\nglobal.set(\"LINE\",x);\n\nmsg.payload = \">@0,\" + x;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1070,"y":1440,"wires":[["92c97542.7768f","f0c6b22a.9767a"]]},{"id":"2566ec3e.48963c","type":"switch","z":"51326072.289f98","name":"0?","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"0","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":920,"y":1390,"wires":[["ab687dda.5b0bb8","264335ed.e72fe2","cd23ad9a.74d988"],["a5e40b9.35c4c78","948c1604.8a333"]]},{"id":"45726900.fc2e28","type":"switch","z":"51326072.289f98","name":"4?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"3","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":920,"y":1440,"wires":[["86f2a8b6.c88db8","cd23ad9a.74d988"],["8d867ec4.eac3d","a5e40b9.35c4c78"]]},{"id":"d26ed2d3.877588","type":"inject","z":"51326072.289f98","name":"Up","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":470,"y":1340,"wires":[["482e8079.99e45"]]},{"id":"c168f079.740828","type":"inject","z":"51326072.289f98","name":"Down","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":470,"y":1490,"wires":[["b7517494.8e4a68"]]},{"id":"f0c6b22a.9767a","type":"debug","z":"51326072.289f98","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1310,"y":1370,"wires":[]},{"id":"948c1604.8a333","type":"function","z":"51326072.289f98","name":"- 1","func":"var x = msg.payload - 1;\nglobal.set(\"LINE\",x);\n\nmsg.payload = \">@0,\" + x;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1070,"y":1390,"wires":[["92c97542.7768f"]]},{"id":"1a874592.53e5b2","type":"delay","z":"51326072.289f98","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1030,"y":980,"wires":[["9b7af149.60fc58"]]},{"id":"3531e39e.b6c4fc","type":"link in","z":"51326072.289f98","name":"","links":["652a67dd.7a84d"],"x":785,"y":1010,"wires":[["737bc492.63dee4"]]},{"id":"34fc7879.6b6c48","type":"change","z":"51326072.289f98","name":"Hide clock","rules":[{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":970,"wires":[["df73a991.eaeb6"]]},{"id":"df73a991.eaeb6","type":"link out","z":"51326072.289f98","name":"0","links":["e02268d.ffa8e18"],"x":585,"y":970,"wires":[]},{"id":"264335ed.e72fe2","type":"function","z":"51326072.289f98","name":"-4","func":"var x = global.get(\"menu_index\") || 0;\nx = x - 4;\nglobal.set(\"menu_index\",x);\nglobal.set(\"LINE\",3);\nnode.warn(\"Line value set to 3\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1070,"y":1500,"wires":[["944a0c7b.1ad3a"]]},{"id":"86f2a8b6.c88db8","type":"function","z":"51326072.289f98","name":"+4","func":"var x = global.get(\"menu_index\") || 0;\nx = x + 4;\nglobal.set(\"menu_index\",x);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1070,"y":1540,"wires":[["944a0c7b.1ad3a"]]},{"id":"15cd23e2.87af14","type":"link out","z":"51326072.289f98","name":"LCD Display CTL commands","links":["1e4ce1b3.80123e"],"x":1470,"y":1520,"wires":[],"l":true},{"id":"80256071.364cf8","type":"change","z":"51326072.289f98","name":"CLS","rules":[{"t":"set","p":"payload","pt":"msg","to":"CLS","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1260,"y":1520,"wires":[["944a0c7b.1ad3a","567090a7.7ca2c"]]},{"id":"944a0c7b.1ad3a","type":"link out","z":"51326072.289f98","name":"","links":["29ae477b.d12f88"],"x":1315,"y":1420,"wires":[]},{"id":"29ae477b.d12f88","type":"link in","z":"51326072.289f98","name":"","links":["944a0c7b.1ad3a"],"x":595,"y":1130,"wires":[["f24a5a0c.272178"]]},{"id":"567090a7.7ca2c","type":"debug","z":"51326072.289f98","name":"LOAD NEW MENU PAGE","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1410,"y":1460,"wires":[]},{"id":"84902981.d2faf","type":"debug","z":"51326072.289f98","name":"MAX value?","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":1500,"wires":[]},{"id":"f3fe43f3.8f0d58","type":"debug","z":"51326072.289f98","name":"MIN value?","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":1340,"wires":[]},{"id":"f5ba0671.28e6","type":"function","z":"51326072.289f98","name":">","func":"var y = global.get(\"LINE\");\n\nnode.warn(\"Y is set to \" + y);\n\nnode.status({text:y});\n\n\nmsg.payload = \">@0,\"+y;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":830,"y":1240,"wires":[["92bb1bdc.5bbea","3c76d420.a1947c"]]},{"id":"92bb1bdc.5bbea","type":"fan","z":"51326072.289f98","name":"","x":1030,"y":1240,"wires":[["975f857.2480c78","bef2867.88ad778","92c97542.7768f"]]},{"id":"3b22a226.b465e6","type":"inject","z":"51326072.289f98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":670,"y":1310,"wires":[["f5ba0671.28e6"]]},{"id":"ab687dda.5b0bb8","type":"delay","z":"51326072.289f98","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":780,"y":1550,"wires":[[]]},{"id":"a5e40b9.35c4c78","type":"trigger","z":"51326072.289f98","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1080,"y":1340,"wires":[[]]},{"id":"cc1a9f81.ce7b2","type":"function","z":"51326072.289f98","name":"LINE value","func":"var y = global.get(\"LINE\");\n\nnode.status({text:y});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":1620,"wires":[[]]},{"id":"9c54a1.11a0fb6","type":"inject","z":"51326072.289f98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"0.5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":420,"y":1620,"wires":[["cc1a9f81.ce7b2"]]},{"id":"d66068ed.d166f","type":"inject","z":"51326072.289f98","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":940,"y":1660,"wires":[["94843779.f6146"]]},{"id":"94843779.f6146","type":"function","z":"51326072.289f98","name":"SET","func":"\nglobal.set(\"LINE\",3);\nnode.warn(\"Line value set to 3\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":940,"y":1700,"wires":[[]]},{"id":"cd23ad9a.74d988","type":"trigger","z":"51326072.289f98","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1080,"y":1580,"wires":[[]]},{"id":"1df632f6.f1a3b5","type":"comment","z":"51326072.289f98","name":"Don't worry about this.","info":"","x":440,"y":740,"wires":[]},{"id":"7b5790dc.a6dad8","type":"inject","z":"51326072.289f98","name":"LOOK AT ME in the docs","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":500,"y":1100,"wires":[["f24a5a0c.272178"]]},{"id":"e84ff176.3b5268","type":"comment","z":"51326072.289f98","name":"Show global.LINE value","info":"","x":570,"y":1590,"wires":[]}]

```

I know. It is a mess. I am developing it. Arduino code is different to Node Red, and the logic is different as all parts of Node-Red are active at all times.

That is why I have `global.SCREEN`, `LINE` and `menu_index`.

Is what I wrote clear enough?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [4 September 2020 04:09 UTC](https://discourse.nodered.org/t/a-global-context-variable-is-getting-changed-and-i-cant-find-how-why/32426/2 "2020-09-04T04:09:47Z")

</div>

> [@Trying\_to\_learn](#):
>
> I know. It is a mess.

😬

I only took a rough look. You are setting the global LINE and SCREEN as text and as numbers (non-) interchangeably.

eg. LINE is set as "0" (string) and then you use a switch node that checks "contains" "0" and are going to subtract/add `1` to/from it, that will fail. First verify all the change/switch nodes and align them all to `number`.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [4 September 2020 07:17 UTC](https://discourse.nodered.org/t/a-global-context-variable-is-getting-changed-and-i-cant-find-how-why/32426/3 "2020-09-04T07:17:01Z")

</div>

Yeah, ok. I'll cop that.

But look towards the bottom where the `function` node is showing the value for `LINE`.

Press `Down` to get to page 2 of the menu.

Delete the `link` I mentioned.

Click the `Up` button and see `LINE` change to 3.

But then as soon as you press _the_ `inject` node you will see it is set back to `0`.

Press down 3 (4?) times to get the debug as shown.

Then delete the link as shown in blue.

 ![Screenshot from 2020-09-04 17-25-59](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1b0c73b064dc33fbcc86dd599fd041de94337b73.png)

Look at the node at the bottom in picture below then  
press the UP button to get this.

Then press the "LOOK AT ME" `inject` node at the top and watch `LINE` go to `0`

 ![Screenshot from 2020-09-04 17-26-54](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/ad08e33baac364ff7437bbb685f2986b7dcc957f.png)

---

<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: [4 September 2020 12:34 UTC](https://discourse.nodered.org/t/a-global-context-variable-is-getting-changed-and-i-cant-find-how-why/32426/4 "2020-09-04T12:34:55Z")

</div>

The very fact that you are in the situation where you don't know how a global is getting set suggests to me that you shouldn't be using a global for this. Use messages instead to get the value where you need it.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [4 September 2020 13:49 UTC](https://discourse.nodered.org/t/a-global-context-variable-is-getting-changed-and-i-cant-find-how-why/32426/5 "2020-09-04T13:49:10Z")

</div>

If you want to know where a global is referenced, you can surely simply search for it? The search will return all nodes that reference it including function nodes.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [4 September 2020 20:43 UTC](https://discourse.nodered.org/t/a-global-context-variable-is-getting-changed-and-i-cant-find-how-why/32426/6 "2020-09-04T20:43:53Z")

</div>

Messages?

As in `msg.payload`?

Err, yeah, and if you though this is a mess......

There are 4 (6 if you include the extra 2 at the bottom of the flow used for debugging) that set/use that.

I have put `trigger` nodes on their inputs to indicate any signals going into them.

**nothing** unexpected.

So this is why I am asking.

---

<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: [3 November 2020 20:43 UTC](https://discourse.nodered.org/t/a-global-context-variable-is-getting-changed-and-i-cant-find-how-why/32426/7 "2020-11-03T20:43:53Z")

</div>

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