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

**URL:** <https://discourse.nodered.org/t/how-to-set-the-initial-values-in-a-function-node/53201>\
**Category:** General\
**Created:** [4 November 2021 08:53 UTC](https://discourse.nodered.org/t/how-to-set-the-initial-values-in-a-function-node/53201 "2021-11-04T08:53:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 November 2021 08:53 UTC](https://discourse.nodered.org/t/how-to-set-the-initial-values-in-a-function-node/53201/1 "2021-11-04T08:53:45Z")

</div>

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.

```auto
[{"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.

```auto
[{"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?

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [4 November 2021 09:09 UTC](https://discourse.nodered.org/t/how-to-set-the-initial-values-in-a-function-node/53201/2 "2021-11-04T09:09:31Z")

</div>

Try this

```auto
[{"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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 November 2021 09:10 UTC](https://discourse.nodered.org/t/how-to-set-the-initial-values-in-a-function-node/53201/3 "2021-11-04T09:10:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [4 November 2021 09:13 UTC](https://discourse.nodered.org/t/how-to-set-the-initial-values-in-a-function-node/53201/4 "2021-11-04T09:13:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 November 2021 09:14 UTC](https://discourse.nodered.org/t/how-to-set-the-initial-values-in-a-function-node/53201/5 "2021-11-18T09:14:00Z")

</div>

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