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;
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.
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.
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.
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?
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.