Displaying data from context object

Hi.

I have a function node and would like to find the value stored in a context object. In this case either 1 or 0. Nothing shows up in the context side panel - I am guessing because I have not stored the value into a variable?
The node works fine and just allows the flow to pass if a switch is set to on.
So the value is being remembered by the function but I am unable to see what it is.

the function node is as follows:

if (msg.topic == "state") {
	context.mystate = msg.payload.nvalue;
	return null;
}
if (context.mystate == 1)
  return msg;
else
  return null;

You might want to read https://nodered.org/docs/user-guide/writing-functions#storing-data

What happens if the topic is not "state"?

Thanks
I had found that page but it does not really address my issue as the code I use does not use var,get or send.
If the topic is not "state" then the second part of the function applies i.e. the message is only passed when required.
The function works as expected. I was experimenting with storing persistent values across restarts and noticed I am unable to retrieve the current value of the function node for testing.

Have you set the contextStorage property in settings.js?

As for the function add some debug statements in it so you can see what is happening. use

    node.warn("debug-01");

or

    node.warn("context.mystate="+context.mystate);

Brilliant. That has given me what I needed in the debug panel. Now makes sense now when I read the user guide page again.

Yep, I have seen you need to make a change to make variables persistent. I had tried earlier today with retaining mqtt messages - this worked but i ended up in a feedback loop.

Next thing to test then is see if context.mystate is retained if I make the change in settings.js.

Thanks very much for your help @zenofmud

1 Like

Have uncommented the necessary lines in settings.js but context.mystate is not retained.
I will try to store using a variable and test that.

Just realised that I have to inject something into my function node in order for the node.warn to send output to debug. What I was hoping for was a way to read it before anything happened - anyway if I put it into a flow or global variable I will be able to check from elsewhere.

That can usually be fixed by the use of an RBE in the loop, or if you are using ui nodes then by deselecting the pass through checkbox.