# Issue with updating variables in function node

**URL:** https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529
**Category:** General
**Created:** [2 March 2020 22:20 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529 "2020-03-02T22:20:25Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [2 March 2020 22:20 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/1 "2020-03-02T22:20:25Z")

</div>

Hi everyone  
This is my first venture into node-red and wondered if someone could maybe help with an issue I don't seem to resolve after hours of trying.

Thank you in advance

I have a function that simply receives a input of a string of either a "0" or "1" (newinput)

my else if statement then follows the critera and if true is supposed to change a variable tempinput but even though I know the event is being triggered the variable does not change

Many thanks  
Andrew

// set tempinput - to be used as a internal changing variable  
var tempinput

//set the value of tempinput to 1 to start with to stop undefined  
if (tempinput === undefined){  
tempinput = "1";}

// declare a variable to me used for sending debug messages  
var text

// Assign the incoming character to variable new input  
// This value is either a 1 or 0 String formatting  
var newinput = msg.payload

// if newinput + tempinput === "01"  
if (newinput === "0" && tempinput === "1") {

//Set debug output text

text = "New Trigger " + newinput + "" + tempinput;

// update the variable of tempinput to "0"

tempinput = "0";

// if newinput + tempinput === "10"

} else if (newinput === "1" && tempinput === "0") {

// update the variable of tempinput to "1"

tempinput = "1";

//Set debug output text

text = "Reset" + newinput + "" + tempinput;

} else {

//Set debug output text

text = "Not Important - " + newinput + "" + tempinput;

}

//set out message

var newMsg = {payload: text + " " + tempinput};

//send out message

return newMsg;

---

<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: [2 March 2020 22:32 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/2 "2020-03-02T22:32:38Z")

</div>

It might be easier to see if you posted your flow for us to look at. Please follow the guidelines to make sure you post it correctly. Basically use the double ticks. Or the icon looks like \</\>.  
Thanks

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [2 March 2020 22:33 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/3 "2020-03-02T22:33:27Z")

</div>

Many thanks I will do that

Regards  
Andrew

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [2 March 2020 22:40 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/4 "2020-03-02T22:40:07Z")

</div>

```auto
`// set tempinput - to be used as a internal changing variable
var tempinput

//set the value of tempinput to 1 to start with to stop undefined
if (tempinput === undefined){
   tempinput = "1";}

// declare a variable to me used for sending debug messages
var text

// Assign the incoming character to variable new input
// This value is either a 1 or 0 String formatting
var newinput = msg.payload

 // if newinput + tempinput === "01"
if (newinput === "0" && tempinput === "1") {
  
   //Set debug output text
  
  text = "New Trigger " + newinput + "" + tempinput;
  
   
   // update the variable of tempinput to "0"
   
   tempinput = "0";
 
    
    
// if newinput + tempinput === "10"
    
    
} else if (newinput === "1" && tempinput === "0") {
  
  
   // update the variable of tempinput to "1"
  
  tempinput = "1";
  
  
   //Set debug output text
   
   text = "Reset" + newinput + "" + tempinput;
  

} else { 
 

 //Set debug output text

 text = "Not Important - " + newinput + "" + tempinput;

    
}

//set out message

 var newMsg = {payload: text + " " + tempinput}; 

    
//send out message

  return newMsg;`

```

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [2 March 2020 22:48 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/5 "2020-03-02T22:48:09Z")

</div>

```auto
`[{"id":"3f625393.594e9c","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"9bf09702.4e79d8","type":"inject","z":"3f625393.594e9c","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":180,"wires":[["87fea3ef.adc7c"]]},{"id":"87fea3ef.adc7c","type":"function","z":"3f625393.594e9c","name":"","func":"// set tempinput - to be used as a internal changing variable\nvar tempinput\n\n//set the value of tempinput to 1 to start with to stop undefined\nif (tempinput === undefined){\n tempinput = \"1\";}\n\n// declare a variable to me used for sending debug messages\nvar text\n\n// Assign the incoming character to variable new input\n// This value is either a 1 or 0 String formatting\nvar newinput = msg.payload\n\n // if newinput + tempinput === \"01\"\nif (newinput === \"0\" && tempinput === \"1\") {\n \n //Set debug output text\n \n text = \"New Trigger \" + newinput + \"\" + tempinput;\n \n \n // update the variable of tempinput to \"0\"\n \n tempinput = \"0\";\n \n \n \n// if newinput + tempinput === \"10\"\n \n \n} else if (newinput === \"1\" && tempinput === \"0\") {\n \n \n // update the variable of tempinput to \"1\"\n \n tempinput = \"1\";\n \n \n //Set debug output text\n \n text = \"Reset\" + newinput + \"\" + tempinput;\n \n\n} else { \n \n\n //Set debug output text\n\n text = \"Not Important - \" + newinput + \"\" + tempinput;\n\n \n}\n\n//set out message\n\n var newMsg = {payload: text + \" \" + tempinput}; \n\n \n//send out message\n\n return newMsg;","outputs":1,"noerr":0,"x":490,"y":180,"wires":[["44df4ab2.5e01a4"]]},{"id":"44df4ab2.5e01a4","type":"debug","z":"3f625393.594e9c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":180,"wires":[]},{"id":"4d44451d.7e573c","type":"inject","z":"3f625393.594e9c","name":"","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":260,"wires":[["87fea3ef.adc7c"]]}]`

```

---

<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: [2 March 2020 22:54 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/6 "2020-03-02T22:54:25Z")

</div>

The flow you posted is a single debug node. You sure that is what you wanted to post?

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [2 March 2020 23:01 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/7 "2020-03-02T23:01:15Z")

</div>

Hi Gerry  
All a bit new to this  
I have hopefully edited the post with the correct flow

Many thanks

---

<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: [2 March 2020 23:06 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/8 "2020-03-02T23:06:20Z")

</div>

Can you explain the goal of your flow ? What is the expected output ?

---

<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: [2 March 2020 23:08 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/9 "2020-03-02T23:08:02Z")

</div>

To add to Bakman, when you press the 1 what do you want to happen, when you press 0 what do you want to happen, and what do you see happening when you press the triggers?

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [2 March 2020 23:17 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/10 "2020-03-02T23:17:50Z")

</div>

When the flow start the input into the If else statements is as follows

Pressing inject(1) will create newinput + tempinput = to "11"

This is ignored by the logic

////////////////////////////////////////////////

Pressing inject(0) will create newinput + tempinput = to "01"

This will go on when I get it working to write to a database  
and needs to change the variable tempinput to "0" (This doesnt happen)

///////////////////////////////////////////////////////////////////////////////////////////////

Pressing insert (0) again will create newinput + tempinput = to "00"  
We do nothing

////////////////////////////////////////////////

Pressing insert (1) again will create newinput + tempinput = to "10"

This will go on when I get it working to write to a database  
and needs to change the variable tempinput to "1" (This doesnt happen)

And so we return ("11")

Not sure this helps but thank you

---

<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: [2 March 2020 23:23 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/11 "2020-03-02T23:23:10Z")

</div>

> Not sure this helps but thank you

I was hoping for a practical example, but ok, this is not how node-red works.

When you send a message to the function node, it handles it and produces an output, that was the lifecycle of the message and no information is kept.

If you want to keep track of the state, you will need to create context variables, either [node, flow or global context variables](https://nodered.org/docs/user-guide/writing-functions#storing-data).

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [2 March 2020 23:28 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/12 "2020-03-02T23:28:24Z")

</div>

Hi Bakman  
Many thanks for your help  
This flow is part of a complex 16 channel I/O board that gives out a Hexidecimal string which i then process and convert to a Binary string

The purpose of this little test flow is to get the binary char and make a decision as to whether I write to a database or not

---

<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: [2 March 2020 23:32 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/13 "2020-03-02T23:32:04Z")

</div>

I think you should take a look at [bitwise operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators) for those kind of things instead. I am not knowledgeable with that stuff, maybe @Colin could lead you on the right track.

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [2 March 2020 23:38 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/14 "2020-03-02T23:38:35Z")

</div>

Cheers bakman

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [3 March 2020 00:28 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/15 "2020-03-03T00:28:37Z")

</div>

Hi Again  
Just to let you know I implemented a context variable and the thing is working like a dream, a bit less hair but a bit more knowledgeable I guess

Thank you again

---

<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: [3 March 2020 00:47 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/16 "2020-03-03T00:47:09Z")

</div>

FYI in the future, if you want to imbed some debuggng in your `function` node you can use node.warn(). for example say you have

```auto
var x = msg.payload
if (x == 'hello') {msg.payload = 'world'} 

```

and your are sure that msg.payload has 'hello' in it you could use

```auto
var x = msg.payload
node.warn('x=' + x)
if (x == 'hello') {msg.payload = 'world'} 

```

and you will see in the debug panel what x contains.

---

<div class="post-metadata">

### Author: ![fogmajor](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fogmajor](https://discourse.nodered.org/u/fogmajor)
#### Post date: [3 March 2020 00:59 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/17 "2020-03-03T00:59:40Z")

</div>

Thank you for that, it will make life much easier

Regards  
Andrew

---

<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: [17 March 2020 01:05 UTC](https://discourse.nodered.org/t/issue-with-updating-variables-in-function-node/22529/18 "2020-03-17T01:05:02Z")

</div>

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