How to set the `initial values` in a function node?

Sorry folks, but I can't understand it.

I did a quick search but am not wording it the right way.

This is an example of what I am wanting to do.

[{"id":"5869e9f6.3f1a3","type":"function","z":"675e227d.d158b4","name":"","func":"msg1 = {};\n\n\nlet x = context.get(\"STATE\") || 0;\n\nx = (x + 1) % 2;\ncontext.set(\"STATE\",x);\n\nif (x === 0)\n{\n    //\n    //  Set things for state 0\n    //\n    msg.payload = context.get(\"PayloadA\");\n    msg.topic = context.get(\"Topic\");\n    msg1.colour = context.get(\"ABGC\");\n    msg1.txt = context.get(\"Atxt\");\n    msg1.fontclr = context.get(\"AFC\");\n}\nelse if (x === 1)\n{\n    //\n    //  Set things for state 1\n    //\n    msg.payload = context.get(\"PayloadB\");\n    msg.topic = context.get(\"Topic\");\n    msg1.colour = context.get(\"BBGC\");\n    msg1.txt = context.get(\"Btxt\");\n    msg1.fontclr = context.get(\"BFC\");\n}\n\n\n\n\n\nreturn msg;","outputs":2,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nlet topic = \"CONTROL\";\nlet PayloadA = \"ON\";\nlet ABGC = \"blue\";\nlet Atxt = \"On\";\nlet AFC = \"black\";\n","finalize":"","libs":[],"x":420,"y":2510,"wires":[["2d39686e.02fac8"],[]]}]

Look in the on start tab.

Yeah, I am missing half of them, but if I am shown how to do the first half, I hope I can then do the second half.

Ok, progress made, but still not there.

[{"id":"5869e9f6.3f1a3","type":"function","z":"675e227d.d158b4","name":"","func":"msg1 = {};\n\n\nlet x = context.get(\"STATE\") || 0;\n\nx = (x + 1) % 2;\ncontext.set(\"STATE\",x);\n\nif (x === 0)\n{\n    //\n    //  Set things for state 0\n    //\n    msg.payload = context.get(\"PayloadA\");\n    msg.topic = context.get(\"Topic\");\n    msg1.colour = context.get(\"ABGC\");\n    msg1.txt = context.get(\"Atxt\");\n    msg1.fontclr = context.get(\"AFC\");\n}\nelse if (x === 1)\n{\n    //\n    //  Set things for state 1\n    //\n    msg.payload = context.get(\"PayloadB\");\n    msg.topic = context.get(\"Topic\");\n    msg1.colour = context.get(\"BBGC\");\n    msg1.txt = context.get(\"Btxt\");\n    msg1.fontclr = context.get(\"BFC\");\n}\n\n\n\n\n\nreturn msg;","outputs":2,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\ncontext.set(\"topic\",\"CONTROL\");\ncontext.set(\"PayloadA\",\"ON\");\ncontext.set(\"AGBC\",\"blue\");\ncontext.set(\"Atxt\",\"On\");\ncontexet.set(\"AFC\",\"black\");\n\ncontext.set(\"PayloadB\",\"OFF\");\ncontext.set(\"BGBC\",\"yellow\");\ncontext.set(\"Btxt\",\"Off\");\ncontexet.set(\"BFC\",\"white\");\n","finalize":"","libs":[],"x":420,"y":2510,"wires":[["2d39686e.02fac8"],[]]}]

I am not seeing the second half being set.

Ok, bad spelling in the context.set line... Yeah?

Try this

[{"id":"5869e9f6.3f1a3","type":"function","z":"1e44d3d73f4ab3a0","name":"","func":"let msg1 = {};\n\n\nlet x = context.get(\"STATE\") || 0;\n\nx = (x + 1) % 2;\ncontext.set(\"STATE\",x);\n\nif (x === 1)\n{\n    //\n    //  Set things for state 0\n    //\n    msg.payload = context.get(\"PayloadA\");\n    msg.topic = context.get(\"Topic\");\n    msg.colour = context.get(\"AGBC\");\n    msg.txt = context.get(\"Atxt\");\n    msg.fontclr = context.get(\"AFC\");\n}\nelse if (x === 0)\n{\n    //\n    //  Set things for state 1\n    //\n    msg1.payload = context.get(\"PayloadB\");\n    msg1.topic = context.get(\"Topic\");\n    msg1.colour = context.get(\"BGBC\");\n    msg1.txt = context.get(\"Btxt\");\n    msg1.fontclr = context.get(\"BFC\");\n}\n\n\n\n\n\nreturn msg;","outputs":2,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\ncontext.set(\"Topic\",\"CONTROL\");\ncontext.set(\"PayloadA\",\"ON\");\ncontext.set(\"AGBC\",\"blue\");\ncontext.set(\"Atxt\",\"On\");\ncontext.set(\"AFC\",\"black\");\n\ncontext.set(\"PayloadB\",\"OFF\");\ncontext.set(\"BGBC\",\"yellow\");\ncontext.set(\"Btxt\",\"Off\");\ncontext.set(\"BFC\",\"white\");","finalize":"","libs":[],"x":420,"y":240,"wires":[[],[]]}]

It looks like there were some misspellings in your variables and contexts

1 Like

Yeah, thanks.

I'm a bad speller.

I changed them and they work.

It is just (also) I haven't ever used it and it is full on me trying to get it working.

Thanks.
I didn't delete the post only because I posted it, deleted it and had to restore it and then when I got it half worked out, I was suspicious there was another problem in there.

I get so ahead of myself that I am very guilty of misspellings too. Sometimes I just need a fresh set of eyes to look at something.

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