How do I recall a context.flow variable and set to a node (ex: "switch")

How do I recall a context.flow variable to initial a node "switch"?
Because when I call a tab, the switch will stay on latest status. I tried to store it in flow variable, but most of time it's undefined due to not changing the "switch". I use if (!flow.variable) to find it is "undefined" but how to initial a value to the "switch"?

Have you read thru the documentation? https://nodered.org/docs/user-guide/context

When having an issue, I find it best do copy the problem flow to a new tab and then remove anything that isn't directly relatd to the issue. Then I share that flow showing the issue. When sharing a flow, make sure to follow the directions in the thread ' How to share code or flow json'

2 Likes

First make the flow variable persistent (see the docs linked to by @zenofmud), then you can use an Inject node to inject the value on startup so the switch will be initialised.

1 Like

Here is my sample flow:

[{"id":"5678f065.787c4","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"3cf86145.c3e65e","type":"ui_switch","z":"5678f065.787c4","name":"OnOff","label":"On/Off","tooltip":"","group":"30fd970c.abb5a8","order":5,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"1","onvalueType":"num","onicon":"","oncolor":"","offvalue":"0","offvalueType":"num","officon":"","offcolor":"","x":130,"y":80,"wires":[["63952612.c76fc8"]]},{"id":"63952612.c76fc8","type":"function","z":"5678f065.787c4","name":"Store SchedulePortSwitch","func":"context.flow.PortSwitch = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":80,"wires":[[]]},{"id":"e0569f7e.97986","type":"function","z":"5678f065.787c4","name":"Submit Schedule Select Value","func":"if (!context.flow.PortSwitch)\n{\n    msg.payload = \"Something undefined sw:\" +context.flow.PortSwitch;\n    return [msg,null];\n}\n//if (context.flow.ScheduleStaNo!==null && context.flow.SchedulePortNo!==null && context.flow.PortSwitch!==null && context.flow.ScheduleDate!==null && context.flow.ScheduleHour!==null && msg.payload === \"submit\")\nelse{\n    msg.payload_PortSwitch = context.flow.PortSwitch;\n    return [null,msg];\n}","outputs":2,"noerr":0,"x":230,"y":140,"wires":[["8eda8cdf.a2de1"],["8eda8cdf.a2de1"]]},{"id":"8eda8cdf.a2de1","type":"debug","z":"5678f065.787c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":540,"y":140,"wires":[]},{"id":"30fd970c.abb5a8","type":"ui_group","z":"","name":"Station1","tab":"1013cb34.33f755","disp":true,"width":"8","collapse":false},{"id":"1013cb34.33f755","type":"ui_tab","z":"","name":"Schedule","icon":"date_range","disabled":false,"hidden":false}]

Function nodes don't do anything unless they receive a message on the input. so your function that tests it will never do anything.

Hi Colin, Thanks for your help. That's why I'm wondering if there is anything I can do. Otherwise, users come to the page see the "switch" and do nothing because "off" status is what she/he wants, but the node is "undefined"....

As I said earlier, you can use an Inject node to inject the value at startup.
In fact for something like this I would actually use MQTT with retained topics, but if you are not already using MQTT then using persistent context may be simpler, but is does mean you have to do the inject on startup.

Thank you so much. Since I thought Inject node can do manually or repeat only, finally I realize Inject can be set to Inject "at least once after t seconds, then...."
Thanks for your help.