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.

there official format about context write 'context.set(mystate,msg.payload.nvalue);'
Is these two formats are equal?Or maybe your is more clear and easy?

This is first time I use context .

context.set() is the correct way to do it. The other way was the old way, which does still work. The get() and set() method allow the specification of which context store to use.

Thank you for answer.
write function :

Thank you for your answer.

var messageSQL;
context.startFlag = context.startFlag || false; // initialize variables
context.timeStamp = context.timeStamp || 0;
context.orderID = context.orderID || '';
context.customerCode = context.customerCode||'';
var mainFlowMeter = context.mainFlowMeter || -111.111;
var assistantFlowMeter = context.assistantFlowMeter || -111.111;


if (msg.topic == '16#主统计总标志') {
    if (msg.payload==true ){
    context.startFlag=msg.payload;
    }
    // context.timeStamp = new Date.toISOString();
}

......


if (context.startFlag == true) {
    context.startFlag=false;
   // console.log(context.startFlag);


    messageSQL = { topic: `insert into flowmeter_allcount (\`OrderID\`,\`CustomerCode\`,\`Dev_id\`,\`Main_FlowMeter_AllCount\`,\`Assistant_FlowMeter_AllCount\`) values ('${context.orderID}','${context.customerCode}',16,${context.mainFlowMeter},${context.assistantFlowMeter})`};

    return messageSQL;

}

return;

these codes had been test by myself.there had omitted some codes .And function had work well .
Next time I will test context.get or set function.