Issue with updating variables in function node

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;

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

Many thanks I will do that

Regards
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;`
`[{"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"]]}]`

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

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

Many thanks

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

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?

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

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.

1 Like

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

I think you should take a look at bitwise operators for those kind of things instead. I am not knowledgeable with that stuff, maybe @Colin could lead you on the right track.

Cheers bakman

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

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

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

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

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.

Thank you for that, it will make life much easier

Regards
Andrew

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