# Help with how to make a node to do this function

**URL:** <https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381>\
**Category:** General\
**Created:** [6 January 2025 08:59 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381 "2025-01-06T08:59:41Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [6 January 2025 21:38 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/22 "2025-01-06T21:38:18Z")

</div>

Thanks.

Not exactly what I was going for.

No offence. I didn't explain it correctly.

Does the bit I posted help clear up what I'm wanting to do?

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [6 January 2025 21:38 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/23 "2025-01-06T21:38:49Z")

</div>

yep, thinking on it

---

<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:** [6 January 2025 21:49 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/24 "2025-01-06T21:49:59Z")

</div>

This seems to be working all but for the last `function` node, and I have to keep inputting from the initial `inject` node depending if I want it on or off.

```auto
[{"id":"79dfd23d02073a19","type":"function","z":"b25fd092.bcc758","name":"function 186","func":"let x = msg.payload;\nlet y = context.get(\"toggle\") || 0;\nif (x == \"TOGGLE\")\n{\n //node.warn(\"1 \" + y);\n y = ((y + 1) % 2);\n //node.warn(\"2 \" + y)\n context.set(\"toggle\",y)\n if (y == 0)\n {\n node.status({fill: \"green\", text: \"normal\"});\n }\n else\n {\n node.status({fill: \"red\", text: \"flip\"});\n }\n return;\n}\n\nnode.warn(\"Toggle set to \" + y)\n\nif (y == 1)\n{\n //\n if (x = `1,0`)\n {\n x = `0,1`\n msg.payload = x;\n }\n //\n if (x = `0,1`)\n {\n x = `1,0`\n msg.payload = x;\n }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":6490,"wires":[["d87aa2b354720320","ae171e439487547e"]]},{"id":"aa890c59737b287b","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TOGGLE","payloadType":"str","x":460,"y":6440,"wires":[["79dfd23d02073a19"]]},{"id":"7355d12c86790cdc","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0,1","payloadType":"str","x":270,"y":6470,"wires":[["79dfd23d02073a19"]]},{"id":"b2ae2e64bea44080","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1,0","payloadType":"str","x":270,"y":6520,"wires":[["79dfd23d02073a19"]]},{"id":"d87aa2b354720320","type":"debug","z":"b25fd092.bcc758","name":"debug 3463","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":6580,"wires":[]},{"id":"ae171e439487547e","type":"function","z":"b25fd092.bcc758","name":"function 187","func":"let x = msg.payload;\nlet y = context.get(\"toggle\") || 0;\nif (x == \"TOGGLE\")\n{\n //node.warn(\"1 \" + y);\n y = ((y + 1) % 2);\n //node.warn(\"2 \" + y)\n context.set(\"toggle\",y)\n if (y == 0)\n {\n node.status({fill: \"green\", text: \"normal\"});\n }\n else\n {\n node.status({fill: \"red\", text: \"flip\"});\n }\n return;\n}\n\nnode.warn(\"Toggle set to \" + y)\n\nif (y == 1)\n{\n //\n if (x = `1,0`)\n {\n x = `0,1`\n msg.payload = x;\n }\n //\n if (x = `0,1`)\n {\n x = `1,0`\n msg.payload = x;\n }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":6490,"wires":[["87a2abd50a00c54c","2e2c3085569f9064"]]},{"id":"c96ea5a2349e3ffc","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TOGGLE","payloadType":"str","x":680,"y":6440,"wires":[["ae171e439487547e"]]},{"id":"87a2abd50a00c54c","type":"function","z":"b25fd092.bcc758","name":"function 188","func":"let x = msg.payload;\nlet y = context.get(\"toggle\") || 0;\nif (x == \"TOGGLE\")\n{\n //node.warn(\"1 \" + y);\n y = ((y + 1) % 2);\n //node.warn(\"2 \" + y)\n context.set(\"toggle\",y)\n if (y == 0)\n {\n node.status({fill: \"green\", text: \"normal\"});\n }\n else\n {\n node.status({fill: \"red\", text: \"flip\"});\n }\n return;\n}\n\nnode.warn(\"Toggle set to \" + y)\n\nif (y == 1)\n{\n //\n if (x = `1,0`)\n {\n x = `0,1`\n msg.payload = x;\n }\n //\n if (x = `0,1`)\n {\n x = `1,0`\n msg.payload = x;\n }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":6490,"wires":[["8799bfd981b0a666","5d90f192a52f3689"]]},{"id":"2e2c3085569f9064","type":"debug","z":"b25fd092.bcc758","name":"debug 3464","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":6580,"wires":[]},{"id":"ea4d2a8c0b6140a6","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TOGGLE","payloadType":"str","x":890,"y":6440,"wires":[["87a2abd50a00c54c"]]},{"id":"8799bfd981b0a666","type":"function","z":"b25fd092.bcc758","name":"function 189","func":"let x = msg.payload;\nlet y = x.split(',');\n\nmsg.payload = y;\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1090,"y":6490,"wires":[["ca5264fb7de95619"]]},{"id":"5d90f192a52f3689","type":"debug","z":"b25fd092.bcc758","name":"debug 3462","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":6580,"wires":[]},{"id":"ca5264fb7de95619","type":"debug","z":"b25fd092.bcc758","name":"Light is.....","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1280,"y":6490,"wires":[]}]

```

This is the code in the main `function` nodes:

```auto
let x = msg.payload;
let y = context.get("toggle") || 0;
if (x == "TOGGLE")
{
    //node.warn("1 " + y);
    y = ((y + 1) % 2);
    //node.warn("2 " + y)
    context.set("toggle",y)
    if (y == 0)
    {
        node.status({fill: "green", text: "normal"});
    }
    else
    {
        node.status({fill: "red", text: "flip"});
    }
    return;
}

node.warn("Toggle set to " + y)

if (y == 1)
{
    //
    if (x = `1,0`)
    {
        x = `0,1`
        msg.payload = x;
    }
    //
    if (x = `0,1`)
    {
        x = `1,0`
        msg.payload = x;
    }
}
return msg;

```

---

<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:** [6 January 2025 21:55 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/25 "2025-01-06T21:55:32Z")

</div>

Ok, tweaked a bit more.

```auto
[{"id":"ae171e439487547e","type":"function","z":"b25fd092.bcc758","name":"function 187","func":"let x = msg.payload;\nlet y = context.get(\"toggle\") || 0;\nif (x == \"TOGGLE\")\n{\n //node.warn(\"1 \" + y);\n y = ((y + 1) % 2);\n //node.warn(\"2 \" + y)\n context.set(\"toggle\",y)\n if (y == 0)\n {\n node.status({fill: \"green\", text: \"normal\"});\n }\n else\n {\n node.status({fill: \"red\", text: \"flip\"});\n }\n return;\n}\n\n//node.warn(\"Toggle set to \" + y)\n\nif (y == 1)\n{\n //\n if (x = `1,0`)\n {\n x = `0,1`\n msg.payload = x;\n }\n //\n if (x = `0,1`)\n {\n x = `1,0`\n msg.payload = x;\n }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":6490,"wires":[["87a2abd50a00c54c","2e2c3085569f9064"]]},{"id":"c96ea5a2349e3ffc","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TOGGLE","payloadType":"str","x":680,"y":6440,"wires":[["ae171e439487547e"]]},{"id":"79dfd23d02073a19","type":"function","z":"b25fd092.bcc758","name":"function 186","func":"let x = msg.payload;\nlet y = context.get(\"toggle\") || 0;\nif (x == \"TOGGLE\")\n{\n //node.warn(\"1 \" + y);\n y = ((y + 1) % 2);\n //node.warn(\"2 \" + y)\n context.set(\"toggle\",y)\n if (y == 0)\n {\n node.status({fill: \"green\", text: \"normal\"});\n }\n else\n {\n node.status({fill: \"red\", text: \"flip\"});\n }\n return;\n}\n\n//node.warn(\"Toggle set to \" + y)\n\nif (y == 1)\n{\n //\n if (x = `1,0`)\n {\n x = `0,1`\n msg.payload = x;\n }\n //\n if (x = `0,1`)\n {\n x = `1,0`\n msg.payload = x;\n }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":6490,"wires":[["d87aa2b354720320","ae171e439487547e"]]},{"id":"87a2abd50a00c54c","type":"function","z":"b25fd092.bcc758","name":"function 188","func":"let x = msg.payload;\nlet y = context.get(\"toggle\") || 0;\nif (x == \"TOGGLE\")\n{\n //node.warn(\"1 \" + y);\n y = ((y + 1) % 2);\n //node.warn(\"2 \" + y)\n context.set(\"toggle\",y)\n if (y == 0)\n {\n node.status({fill: \"green\", text: \"normal\"});\n }\n else\n {\n node.status({fill: \"red\", text: \"flip\"});\n }\n return;\n}\n\n//node.warn(\"Toggle set to \" + y)\n\nif (y == 1)\n{\n //\n if (x = `1,0`)\n {\n x = `0,1`\n msg.payload = x;\n }\n //\n if (x = `0,1`)\n {\n x = `1,0`\n msg.payload = x;\n }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":6490,"wires":[["8799bfd981b0a666","5d90f192a52f3689"]]},{"id":"2e2c3085569f9064","type":"debug","z":"b25fd092.bcc758","name":"debug 3464","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":6580,"wires":[]},{"id":"aa890c59737b287b","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TOGGLE","payloadType":"str","x":460,"y":6440,"wires":[["79dfd23d02073a19"]]},{"id":"7355d12c86790cdc","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0,1","payloadType":"str","x":270,"y":6470,"wires":[["79dfd23d02073a19"]]},{"id":"b2ae2e64bea44080","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1,0","payloadType":"str","x":270,"y":6520,"wires":[["79dfd23d02073a19"]]},{"id":"d87aa2b354720320","type":"debug","z":"b25fd092.bcc758","name":"debug 3463","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":6580,"wires":[]},{"id":"ea4d2a8c0b6140a6","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TOGGLE","payloadType":"str","x":890,"y":6440,"wires":[["87a2abd50a00c54c"]]},{"id":"8799bfd981b0a666","type":"function","z":"b25fd092.bcc758","name":"function 189","func":"let x = msg.payload;\nlet y = x.split(',');\n\nlet state = y[0];\n\nif (state == 0)\n{\n msg.payload = \"OFF\"\n}\nelse\n{\n msg.payload = \"ON\"\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1090,"y":6490,"wires":[["ca5264fb7de95619"]]},{"id":"5d90f192a52f3689","type":"debug","z":"b25fd092.bcc758","name":"debug 3462","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":6580,"wires":[]},{"id":"ca5264fb7de95619","type":"debug","z":"b25fd092.bcc758","name":"Light is.....","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1280,"y":6490,"wires":[]}]

```

I may need to work on the _INPUT_ side of things but it seems to do what I want.

# THANKS ALL FOR THE HELP!

I won't `SOLVE` it just now.  
Wouldn't mind a second set of eyes to make sure I haven't missed anything obvious.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [6 January 2025 22:24 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/26 "2025-01-06T22:24:36Z")

</div>

I think you may have missed a state or two. here's the logic table

 ![4way](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/183556a23003689d37df02fb6493ac2b2e098372.jpeg)

and here's a quick knock up of a flow

```auto
[{"id":"a12fd86cefad38b2","type":"inject","z":"d5c9800b918b16b1","name":"s1 up","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"s1","payload":"1","payloadType":"num","x":150,"y":60,"wires":[["b8aacbc9fc416766"]]},{"id":"df77685f245585b8","type":"inject","z":"d5c9800b918b16b1","name":"s1 dn","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"s1","payload":"0","payloadType":"num","x":150,"y":120,"wires":[["b8aacbc9fc416766"]]},{"id":"d04ed9d6f12cd219","type":"inject","z":"d5c9800b918b16b1","name":"s2 up","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"s2","payload":"1","payloadType":"num","x":150,"y":300,"wires":[["b8aacbc9fc416766"]]},{"id":"c40df9e2b7f13bce","type":"inject","z":"d5c9800b918b16b1","name":"s2 dn","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"s2","payload":"0","payloadType":"num","x":150,"y":360,"wires":[["b8aacbc9fc416766"]]},{"id":"b8aacbc9fc416766","type":"function","z":"d5c9800b918b16b1","name":"logic","func":"\n\nif (msg.topic == \"s1\") {\n if (msg.payload = 1) {\n let S1 = \"up\";\n context.set(\"S1\", \"up\");\n }\n if (msg.payload = 0) {\n let S1 = \"dn\";\n context.set(\"S1\", \"dn\");\n }\n}\n\nif (msg.topic == \"s2\") {\n if (msg.payload = 1) {\n let S2 = \"up\";\n context.set(\"S2\", \"up\");\n }\n if (msg.payload = 0) {\n let S2 = \"dn\";\n context.set(\"S2\", \"dn\");\n }\n}\n\nif (msg.topic == \"mid\") {\n if (msg.payload = 1) {\n let S3 = \"up\";\n context.set(\"S3\", \"up\");\n }\n if (msg.payload = 0) {\n let S3 = \"dn\";\n context.set(\"S3\", \"dn\");\n }\n}\n\n\nlet S1 = context.get(\"S1\");\nlet S2 = context.get(\"S2\");\nlet Mid = context.get(\"S3\");\n\n//state 1\nif (S1 == 0 && Mid == 0 && S2 == 0) {\n //no output\n}\n//state2\nif (S1 == 0 && Mid == 0 && S2 == 1) {\n // output\n}\n//state 3\nif (S1 == 0 && Mid == 1 && S2 == 1) {\n //no output\n}\n//state 4\nif (S1 == 0 && Mid == 1 && S2 == 0) {\n //output\n}\n//state5\nif (S1 == 1 && Mid == 1 && S2 == 0) {\n //no output\n}\n//state 6\nif (S1 == 1 && Mid == 1 && S2 == 1) {\n //output\n}\n//state 7\nif (S1 == 1 && Mid == 0 && S2 == 1) {\n //no output\n}\n//state 8\nif (S1 == 1 && Mid == 0 && S2 == 0) {\n //output\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":160,"wires":[[]]},{"id":"9923e5787112da02","type":"inject","z":"d5c9800b918b16b1","name":"s mid up","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"mid","payload":"1","payloadType":"num","x":160,"y":180,"wires":[["b8aacbc9fc416766"]]},{"id":"8d2ae70b80fe8a86","type":"inject","z":"d5c9800b918b16b1","name":"s mid dn","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"mid","payload":"0","payloadType":"num","x":160,"y":240,"wires":[["b8aacbc9fc416766"]]}]

```

not tested of course

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 January 2025 22:34 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/27 "2025-01-06T22:34:18Z")

</div>

Hereis the two way and one intermediate switch using switch nodes.

```auto
[{"id":"8286aa615a7ad4f4","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cross","payload":"X","payloadType":"str","x":150,"y":1260,"wires":[["7076f91375c07186"]]},{"id":"7076f91375c07186","type":"join","z":"667cec54c048503c","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":323.99999237060547,"y":1384.000020980835,"wires":[["c20833963e0e4498","10538ca58b76020f"]]},{"id":"aa890c59737b287b","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"cross","payload":"_","payloadType":"str","x":150,"y":1300,"wires":[["7076f91375c07186"]]},{"id":"7355d12c86790cdc","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T1","payload":"true","payloadType":"bool","x":170,"y":1360,"wires":[["7076f91375c07186"]]},{"id":"b2ae2e64bea44080","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T2","payload":"true","payloadType":"bool","x":170,"y":1440,"wires":[["7076f91375c07186"]]},{"id":"d4aab0b81c657f87","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T1","payload":"false","payloadType":"bool","x":170,"y":1400,"wires":[["7076f91375c07186"]]},{"id":"1a68b59de052a1ed","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T2","payload":"false","payloadType":"bool","x":170,"y":1480,"wires":[["7076f91375c07186"]]},{"id":"c20833963e0e4498","type":"debug","z":"667cec54c048503c","name":"debug 2579","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1070,"y":1560,"wires":[]},{"id":"10538ca58b76020f","type":"switch","z":"667cec54c048503c","name":"","property":"payload.cross","propertyType":"msg","rules":[{"t":"eq","v":"X","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":1360,"wires":[["8bd8f11992800d44","623fdb3ebc294641","7e2209b1ae95ed7d","b19cc6454a550d19"],["6fcf95f68691f170","22d99c465d376ce1","4cf7c1608a50074d","875db038d7702bc0"]]},{"id":"9f0a0b623431a5f2","type":"change","z":"667cec54c048503c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":1380,"wires":[["c20833963e0e4498"]]},{"id":"086841fc6e4ab2c1","type":"change","z":"667cec54c048503c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":980,"y":1280,"wires":[["c20833963e0e4498"]]},{"id":"8bd8f11992800d44","type":"switch","z":"667cec54c048503c","name":"T1 false","property":"payload.T1","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":1200,"wires":[["ceafd3557641c2cb"]]},{"id":"623fdb3ebc294641","type":"switch","z":"667cec54c048503c","name":"T1 true","property":"payload.T1","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":1160,"wires":[["b3a88c264d301db7"]]},{"id":"7e2209b1ae95ed7d","type":"switch","z":"667cec54c048503c","name":"T1 true","property":"payload.T1","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":1240,"wires":[["14d14705d992a9b1"]]},{"id":"b19cc6454a550d19","type":"switch","z":"667cec54c048503c","name":"T1 false","property":"payload.T1","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":1280,"wires":[["daa382cbda2b4302"]]},{"id":"6fcf95f68691f170","type":"switch","z":"667cec54c048503c","name":"T1 true","property":"payload.T1","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":1340,"wires":[["8d91545844b4800b"]]},{"id":"22d99c465d376ce1","type":"switch","z":"667cec54c048503c","name":"T1 false","property":"payload.T1","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":1380,"wires":[["6dcf0bc1fed46acc"]]},{"id":"4cf7c1608a50074d","type":"switch","z":"667cec54c048503c","name":"T1 true","property":"payload.T1","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":1420,"wires":[["8267d58154977bfd"]]},{"id":"875db038d7702bc0","type":"switch","z":"667cec54c048503c","name":"T1 false","property":"payload.T1","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":1460,"wires":[["39315566cf158a4e"]]},{"id":"daa382cbda2b4302","type":"switch","z":"667cec54c048503c","name":"Y2 false","property":"payload.T2","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":1280,"wires":[["9f0a0b623431a5f2"]]},{"id":"14d14705d992a9b1","type":"switch","z":"667cec54c048503c","name":"T2 true","property":"payload.T2","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":1240,"wires":[["9f0a0b623431a5f2"]]},{"id":"8d91545844b4800b","type":"switch","z":"667cec54c048503c","name":"T2 false","property":"payload.T2","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":1340,"wires":[["9f0a0b623431a5f2"]]},{"id":"6dcf0bc1fed46acc","type":"switch","z":"667cec54c048503c","name":"T2 true","property":"payload.T2","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":1380,"wires":[["9f0a0b623431a5f2"]]},{"id":"b3a88c264d301db7","type":"switch","z":"667cec54c048503c","name":"T2 false","property":"payload.T2","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":1160,"wires":[["086841fc6e4ab2c1"]]},{"id":"ceafd3557641c2cb","type":"switch","z":"667cec54c048503c","name":"T2 true","property":"payload.T2","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":1200,"wires":[["086841fc6e4ab2c1"]]},{"id":"8267d58154977bfd","type":"switch","z":"667cec54c048503c","name":"T2 true","property":"payload.T2","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":1420,"wires":[["086841fc6e4ab2c1"]]},{"id":"39315566cf158a4e","type":"switch","z":"667cec54c048503c","name":"Y2 false","property":"payload.T2","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":760,"y":1460,"wires":[["086841fc6e4ab2c1"]]}]

```

And in one function node with status showing output and switch states

```auto
[{"id":"8286aa615a7ad4f4","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T2","payload":"X","payloadType":"str","x":150,"y":1260,"wires":[["a8e73a2352109978"]]},{"id":"a8e73a2352109978","type":"function","z":"667cec54c048503c","name":"intermediate","func":"let output = false;\nlet switches = context.get(\"switches\") ?? {};\nif(msg.payload !== switches[msg.topic]){\n switches[msg.topic] = msg.payload;\n context.set(\"switches\", switches)\n if(switches.T2 === \"X\"){\n if(switches.T1 === false && switches.T3 === true){\n node.status({text:\"/X/ ON\",shape:\"ring\", fill:\"green\"})\n output = \"on\";\n }else if(switches.T1 === true && switches.T3 === false){\n node.status({text:\"\\\\X\\\\ ON\",shape:\"ring\", fill:\"green\"})\n output = \"on\";\n }else if(switches.T1 === true && switches.T3 === true){\n node.status({text:\"\\\\X/ OFF\",shape:\"ring\", fill:\"red\"})\n output = \"off\";\n }else if(switches.T1 === false && switches.T3 === false){\n node.status({text:\"/X\\\\ OFF\",shape:\"ring\", fill:\"red\"})\n output = \"off\";\n }else{\n msg = null;\n }\n }else{\n if(switches.T1 === false && switches.T3 === true){\n node.status({text:\"/=/ OFF\",shape:\"ring\", fill:\"red\"})\n output = \"off\";\n }else if(switches.T1 === true && switches.T3 === false){\n node.status({text:\"\\\\=\\\\ OFF\",shape:\"ring\", fill:\"red\"})\n output = \"off\";\n }else if(switches.T1 === true && switches.T3 === true){\n node.status({text:\"\\\\=\\/ ON\",shape:\"ring\", fill:\"green\"})\n output = \"on\";\n }else if(switches.T1 === false && switches.T3 === false){\n node.status({text:\"/=\\\\ ON\",shape:\"ring\", fill:\"green\"})\n output = \"on\";\n }else{\n msg = null;\n }\n \n }\n if(output && msg) msg.payload = output;\n return msg;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":1520,"wires":[["f58c0c4d6691b18b"]]},{"id":"aa890c59737b287b","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T2","payload":"=","payloadType":"str","x":150,"y":1300,"wires":[["a8e73a2352109978"]]},{"id":"7355d12c86790cdc","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T1","payload":"true","payloadType":"bool","x":170,"y":1360,"wires":[["a8e73a2352109978"]]},{"id":"b2ae2e64bea44080","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T3","payload":"true","payloadType":"bool","x":170,"y":1440,"wires":[["a8e73a2352109978"]]},{"id":"d4aab0b81c657f87","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T1","payload":"false","payloadType":"bool","x":170,"y":1400,"wires":[["a8e73a2352109978"]]},{"id":"1a68b59de052a1ed","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"T3","payload":"false","payloadType":"bool","x":170,"y":1480,"wires":[["a8e73a2352109978"]]},{"id":"f58c0c4d6691b18b","type":"debug","z":"667cec54c048503c","name":"debug 2580","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":1600,"wires":[]}]

```

---

<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:** [6 January 2025 22:43 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/28 "2025-01-06T22:43:47Z")

</div>

Um,

Thanks.

(And I know mine doesn't work after more testing)

Again I'll say I am trying to do something NR was not really designed to do.  
It is more curiosity than an actual need for it I am doing this.

I am wanting to make the _flow_ to work the same way as the wires would.  
(Kinda)  
In that rather than using `context` and that sort of stuff, I'm wanting to see the _states_ going through/past the switches (nodes) and be represented in a realistic way.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [6 January 2025 22:48 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/29 "2025-01-06T22:48:13Z")

</div>

Andrew,

I think you need to clarify a bit more your use case for this.

As you are in Aus - a typical two way switch example is used to enable switching a light on or off from two places (i.e. either entry way in a room as an example)

If you ever watch a sparky wire up something like this - you will find there is an additional "control" wire that is wired between the two switches to enable the link between them - essentially this wire is the one that provides the magic and enables the state of the two switches to turn the light on or off - and is why two way switches (in australia) are confusing sometimes as you can turn the light on by actually turning the opposing switch in the different direction (which may very well be to the off position)

This is distinct from a DPDT switch - which has a number of different versions - the key part being that it has Dual Poles that are both switched at the same time. (And which in different countries are called different things and implemented differently)

Craig

---

<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:** [6 January 2025 22:57 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/30 "2025-01-06T22:57:44Z")

</div>

I'm just wanting to build a mental picture of the workings of how you can have _n_ number of switches to toggle a light on/off.

_n_ being \>= 3

I get how 2 switches control the light. Though their _positions_ are confusing if both are on and the light is off. But that's outside the scope of what I'm wanting.

The link shows how the _magic switch_ is wired into the circuit and allows for \>= 3 switches to control te light.

But my monkey brain isn't _seeing_ it. So I am wanting to (without buying switches and actually wiring it up) get a _picture_ of how it works.

So I have `function` nodes which do the _magic_ of this extra/new switch.

Alas thinking is getting harder and harder for me and it is frustrating when I'm stuck with this thought and can't get it to _show_ me how things are.

I've found a couple of problems with what I thought was THE ANSWER, but the logic was wrong and it failed.

So I'm back to trying to understand how to make the node flip the message if the TOGGLE is set.  
that is: `if y == 1` part is problematic where it inverts the incoming message.

(Better still)

```auto
if (y == 1)
{
    //
    if (x = `1,0`)
    {
        x = `0,1`
        msg.payload = x;
        return msg; // line added because it would get to the next line and change it back again.
    }
    //
    if (x = `0,1`)
    {
        x = `1,0`
        msg.payload = x;
        return msg; // line added.
    }
}

```

Doesn't seem to work.

---

<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:** [6 January 2025 23:05 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/31 "2025-01-06T23:05:08Z")

</div>

Just saw a HUGE mistake with the `if (x = `1,0`)` line.  
Should be ```if (x == `1,0`)

ARGH!

---

<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:** [6 January 2025 23:10 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/32 "2025-01-06T23:10:11Z")

</div>

This code:

```auto
[{"id":"79dfd23d02073a19","type":"function","z":"b25fd092.bcc758","name":"function 186","func":"let x = msg.payload;\nlet y = context.get(\"toggle\") || 0;\nif (x == \"TOGGLE\")\n{\n //node.warn(\"1 \" + y);\n y = ((y + 1) % 2);\n //node.warn(\"2 \" + y)\n context.set(\"toggle\",y)\n if (y == 0)\n {\n node.status({fill: \"green\", text: \"normal\"});\n }\n else\n {\n node.status({fill: \"red\", text: \"flip\"});\n }\n return;\n}\n\n//node.warn(\"Toggle set to \" + y)\n\n//node.warn(\"Payload is \" + x)\n\nif (y == 1)\n{\n //\n //node.warn(\"a\")\n if (x == `1,0`)\n {\n //node.warn(\"a\")\n x = `0,1`\n msg.payload = x;\n return msg;\n }\n //\n if (x == `0,1`)\n {\n //node.warn(\"b\")\n x = `1,0`\n msg.payload = x;\n return msg;\n }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":709,"y":6496,"wires":[["ae171e439487547e","32e7de687114a89f"]]},{"id":"aa890c59737b287b","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TOGGLE","payloadType":"str","x":699,"y":6446,"wires":[["79dfd23d02073a19"]]},{"id":"188b0c7758d721af","type":"trigger","z":"b25fd092.bcc758","name":"OFF","op1":"0,1","op2":"0","op1type":"str","op2type":"str","duration":"-1","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":520,"y":6470,"wires":[["79dfd23d02073a19"]]},{"id":"b275c8b2d07f2578","type":"trigger","z":"b25fd092.bcc758","name":"ON","op1":"1,0","op2":"0","op1type":"str","op2type":"str","duration":"-1","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":520,"y":6510,"wires":[["79dfd23d02073a19"]]},{"id":"ae171e439487547e","type":"function","z":"b25fd092.bcc758","name":"function 187","func":"let x = msg.payload;\nlet y = context.get(\"toggle\") || 0;\nif (x == \"TOGGLE\")\n{\n //node.warn(\"1 \" + y);\n y = ((y + 1) % 2);\n //node.warn(\"2 \" + y)\n context.set(\"toggle\",y)\n if (y == 0)\n {\n node.status({fill: \"green\", text: \"normal\"});\n }\n else\n {\n node.status({fill: \"red\", text: \"flip\"});\n }\n return;\n}\n\n//node.warn(\"Toggle set to \" + y)\n\n//node.warn(\"Payload is \" + x)\n\nif (y == 1)\n{\n //\n //node.warn(\"a\")\n if (x == `1,0`)\n {\n //node.warn(\"a\")\n x = `0,1`\n msg.payload = x;\n return msg;\n }\n //\n if (x == `0,1`)\n {\n //node.warn(\"b\")\n x = `1,0`\n msg.payload = x;\n return msg;\n }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":929,"y":6496,"wires":[["87a2abd50a00c54c","070b96c2e0e3ce92"]]},{"id":"32e7de687114a89f","type":"debug","z":"b25fd092.bcc758","name":"SW1","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":690,"y":6560,"wires":[]},{"id":"54d68a866d3e8353","type":"inject","z":"b25fd092.bcc758","name":"OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OFF","payloadType":"str","x":250,"y":6440,"wires":[["15b5283d986df757","188b0c7758d721af"]]},{"id":"512885e1dfc85d57","type":"change","z":"b25fd092.bcc758","name":"STOP","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":6470,"wires":[["188b0c7758d721af"]]},{"id":"893473637099af23","type":"inject","z":"b25fd092.bcc758","name":"ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ON","payloadType":"str","x":250,"y":6540,"wires":[["b275c8b2d07f2578","512885e1dfc85d57"]]},{"id":"15b5283d986df757","type":"change","z":"b25fd092.bcc758","name":"STOP","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":6510,"wires":[["b275c8b2d07f2578"]]},{"id":"c96ea5a2349e3ffc","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TOGGLE","payloadType":"str","x":919,"y":6446,"wires":[["ae171e439487547e"]]},{"id":"87a2abd50a00c54c","type":"function","z":"b25fd092.bcc758","name":"function 188","func":"let x = msg.payload;\nlet y = context.get(\"toggle\") || 0;\nif (x == \"TOGGLE\")\n{\n //node.warn(\"1 \" + y);\n y = ((y + 1) % 2);\n //node.warn(\"2 \" + y)\n context.set(\"toggle\",y)\n if (y == 0)\n {\n node.status({fill: \"green\", text: \"normal\"});\n }\n else\n {\n node.status({fill: \"red\", text: \"flip\"});\n }\n return;\n}\n\n//node.warn(\"Toggle set to \" + y)\n\n//node.warn(\"Payload is \" + x)\n\nif (y == 1)\n{\n //\n //node.warn(\"a\")\n if (x == `1,0`)\n {\n //node.warn(\"a\")\n x = `0,1`\n msg.payload = x;\n return msg;\n }\n //\n if (x == `0,1`)\n {\n //node.warn(\"b\")\n x = `1,0`\n msg.payload = x;\n return msg;\n }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1139,"y":6496,"wires":[["8799bfd981b0a666","d5b233c2e3220144"]]},{"id":"070b96c2e0e3ce92","type":"debug","z":"b25fd092.bcc758","name":"SW2","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":910,"y":6560,"wires":[]},{"id":"fa341aa89c533db6","type":"inject","z":"b25fd092.bcc758","name":"Reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":6490,"wires":[["512885e1dfc85d57","15b5283d986df757"]]},{"id":"ea4d2a8c0b6140a6","type":"inject","z":"b25fd092.bcc758","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TOGGLE","payloadType":"str","x":1129,"y":6446,"wires":[["87a2abd50a00c54c"]]},{"id":"8799bfd981b0a666","type":"function","z":"b25fd092.bcc758","name":"function 189","func":"let x = msg.payload;\nlet y = x.split(',');\n\nlet state = y[0];\n\nif (state == 0)\n{\n msg.payload = \"OFF\"\n}\nelse\n{\n msg.payload = \"ON\"\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1329,"y":6496,"wires":[["415745113788cc72"]]},{"id":"d5b233c2e3220144","type":"debug","z":"b25fd092.bcc758","name":"SW3","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1120,"y":6560,"wires":[]},{"id":"415745113788cc72","type":"rbe","z":"b25fd092.bcc758","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":1480,"y":6496,"wires":[["ca5264fb7de95619"]]},{"id":"ca5264fb7de95619","type":"debug","z":"b25fd092.bcc758","name":"Light is.....","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1330,"y":6550,"wires":[]}]

```

I tweaked it and the first _SWITCH_ keeps sending it's status.  
(reset is to just stop all things)  
Now it seems to be doing what I wanted.

For the sake of the main `function` node code:

```auto
let x = msg.payload;
let y = context.get("toggle") || 0;
if (x == "TOGGLE")
{
    y = ((y + 1) % 2);
    context.set("toggle",y)
    if (y == 0)
    {
        node.status({fill: "green", text: "normal"});
    }
    else
    {
        node.status({fill: "red", text: "flip"});
    }
    return;
}

if (y == 1)
{
    //
    if (x == `1,0`) // changed from (x =  
    {
        x = `0,1`
        msg.payload = x;
        return msg; // added to stop it being caught in the next check.
    }
    //
    if (x == `0,1`) // as above.
    {
        x = `1,0`
        msg.payload = x;
        return msg; // is this really needed?
    }
}
return msg;

```

This seems to have fixed the problem.

Wanting someone to check it please.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [7 January 2025 06:31 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/33 "2025-01-07T06:31:10Z")

</div>

> [@Trying\_to\_learn](#):
>
> `if (x ==`1,0`) `

You are using backticks here, not single quotes:

```auto
` vs '

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [7 January 2025 09:46 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/34 "2025-01-07T09:46:38Z")

</div>

Woo - this thread is getting complicated.

Can I take you back to your original wiring diagram...

 ![2_way_switching](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/a/3a24dcd22d14f68e3a6fed82db6fcd81da0e08b4.png)  
I would describe the wiring as a **2-way switching circuit** NOT a 3-way.

A **2-way switching circuit** is a type of electrical wiring setup commonly used in homes to control a single light (or another electrical load) from two different locations. This setup is practical for areas like staircases, hallways, or large rooms where you may want to turn the light on or off from either end.

### Application in a Home:

- **Staircases** : Control a light at the top and bottom of the stairs.
- **Hallways** : Control a light from either end of a long hallway.
- **Bedrooms** : Control bedside lights from the door and near the bed.

To work out how the circuit operates you need to start at the 'Ungrounded Conductor' (at the left of the diagram) and see if there is a path to the 'Lamp'. If the 'Hot' and 'Switch Leg' switches are BOTH off - then there will be a path along the top 'Traveller' wire to the 'Lamp'. If the two switches are BOTH on - then there will be a path along the bottom 'Traveller' wire to the 'Lamp'. If the two switches are in any other combination then a path **does not exist** and the **lamp will not light.**

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 January 2025 09:54 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/35 "2025-01-07T09:54:02Z")

</div>

I still think that Node-RED is a poor tool to model this. BUT, if I were going to do it, this seems custom made for a _state machine_. We have a number of state machine Node's available.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 January 2025 12:22 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/36 "2025-01-07T12:22:42Z")

</div>

Here it is modeled with 3 types off switches.  
It must have 1 start, 1 end and 0 to unlimited intermediate between.  
The positions of each switch is shown in the status.

```auto
[{"id":"7bcc382034a861ad","type":"function","z":"667cec54c048503c","name":"start ","func":"let toggle = context.get(\"state\") ?? false;\nlet text;\nif(msg.toggle){\n toggle = !toggle; \n context.set(\"state\", toggle)\n text = toggle ? \"\\\\\" : \"/\";\n msg = null;\n}else{\n if(toggle === true){\n msg.position = 1;\n text = \"\\\\\";\n }else{\n msg.position = 0;\n text = \"/\";\n }\n}\nnode.status({text});\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":210,"y":1640,"wires":[["a535b7ff4e68edb1"]]},{"id":"1050e27646d41e0a","type":"trigger","z":"667cec54c048503c","name":"","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"-1000","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":260,"y":1720,"wires":[["7bcc382034a861ad"]]},{"id":"2a582a99b25c9b07","type":"inject","z":"667cec54c048503c","name":"toggle","props":[{"p":"toggle","v":"true","vt":"bool"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":1580,"wires":[["7bcc382034a861ad"]]},{"id":"a535b7ff4e68edb1","type":"function","z":"667cec54c048503c","name":"intermediate","func":"let toggle = context.get(\"state\") ?? false;\nlet text;\nif(msg.toggle){\n toggle = !toggle; \n context.set(\"state\", toggle);\n text = toggle ? \"X\" : \"=\";\n msg = null;\n}else{\n const output = [];\n if(msg.position === 0){\n if(toggle === true){\n msg.position = 1;\n text = \"X\";\n }else{\n msg.position = 0;\n text = \"=\";\n }\n }else{\n if(toggle === true){\n msg.position = 0;\n text = \"X\";\n }else{\n msg.position = 1;\n text = \"=\";\n }\n }\n}\nnode.status({text});\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":1640,"wires":[["aaf1fe796777107c"]]},{"id":"634cba0152b6c1fb","type":"inject","z":"667cec54c048503c","name":"msb on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"power","payloadType":"str","x":90,"y":1700,"wires":[["1050e27646d41e0a"]]},{"id":"3609de5055af4b01","type":"inject","z":"667cec54c048503c","name":"msb off","props":[{"p":"reset","v":"true","vt":"bool"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":90,"y":1760,"wires":[["1050e27646d41e0a"]]},{"id":"18aebe385a8fb24c","type":"inject","z":"667cec54c048503c","name":"toggle","props":[{"p":"toggle","v":"true","vt":"bool"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":390,"y":1580,"wires":[["a535b7ff4e68edb1"]]},{"id":"aaf1fe796777107c","type":"function","z":"667cec54c048503c","name":"intermediate","func":"let toggle = context.get(\"state\") ?? false;\nlet text;\nif(msg.toggle){\n toggle = !toggle; \n context.set(\"state\", toggle);\n text = toggle ? \"X\" : \"=\";\n msg = null;\n}else{\n const output = [];\n if(msg.position === 0){\n if(toggle === true){\n msg.position = 1;\n text = \"X\";\n }else{\n msg.position = 0;\n text = \"=\";\n }\n }else{\n if(toggle === true){\n msg.position = 0;\n text = \"X\";\n }else{\n msg.position = 1;\n text = \"=\";\n }\n }\n}\nnode.status({text});\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":1640,"wires":[["e60fe3f7e7f645d5"]]},{"id":"7385d7a2ef60db58","type":"inject","z":"667cec54c048503c","name":"toggle","props":[{"p":"toggle","v":"true","vt":"bool"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":570,"y":1580,"wires":[["aaf1fe796777107c"]]},{"id":"e60fe3f7e7f645d5","type":"function","z":"667cec54c048503c","name":"end","func":"let toggle = context.get(\"state\") ?? false;\nlet text;\nif(msg.toggle){\n toggle = !toggle; \n context.set(\"state\", toggle)\n text = toggle ? \"/\" : \"\\\\\";\n msg = null;\n}else{\n if(msg.position === 0){\n if(toggle === true){\n msg = null\n text = \"/\";\n }else{\n text = \"\\\\\";\n }\n }else{\n if(toggle === true){\n text = \"/\";\n }else{\n msg = null;\n text = \"\\\\\";\n }\n }\n}\nnode.status({text});\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":1640,"wires":[["12bd75f4a26a4aed"]]},{"id":"a1b190bf64bde31e","type":"inject","z":"667cec54c048503c","name":"toggle","props":[{"p":"toggle","v":"true","vt":"bool"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":750,"y":1580,"wires":[["e60fe3f7e7f645d5"]]},{"id":"12bd75f4a26a4aed","type":"trigger","z":"667cec54c048503c","name":"Light","op1":"","op2":"off","op1type":"pay","op2type":"str","duration":"1500","extend":true,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":750,"y":1700,"wires":[["d7de512b49d9429e"]]},{"id":"d7de512b49d9429e","type":"debug","z":"667cec54c048503c","name":"debug 2581","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":1740,"wires":[]}]

```

---

<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:** [7 January 2025 20:43 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/37 "2025-01-07T20:43:36Z")

</div>

Thanks.

Fixed/replaced. (_Though it did seem to be working with the wrong one_)

---

<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:** [7 January 2025 20:47 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/38 "2025-01-07T20:47:05Z")

</div>

Yes, but just a bit further down it shows `3 way switching` - of which I am interested.

For reasons unknown I couldn't get my head around how it was working.  
I fear a big piece of paper and two different coloured pens would have been a lot easier.  
But I am what I am and was curious.

The latest flow I posted helps me understand the _trick_ in how that works.  
I somehow couldn't _see_ the trick the DPDT switches were doing and _how_ it worked in the bigger picture.

Now I can.

---

<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:** [7 January 2025 20:52 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/39 "2025-01-07T20:52:48Z")

</div>

Thanks, but I'm confused.

DEPLOYING it and looking I get this:

 ![Screenshot from 2025-01-08 07-49-16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30f1ba9f9de18c7894edaaa27338379f39f5e472.png)

The `debug` window scrolling away _happily_.

But then if I press the second inject node, it all just stops.

 ![Screenshot from 2025-01-08 07-49-49](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48d09e47b3daabcea63b96d0ad44fe7845ae83f4.png)

- as I have a squillion tabs on this machine of NR stuff, the debug window has this horrible habit of wiping itself.

I'll look at what you did but just saying what I'm seeing.

## Update:

Ok.

The FIRST `function` node... The one on the extreme left:

```auto
let toggle = context.get("state") ?? false;
let text;
if(msg.toggle){
    toggle = !toggle;    
    context.set("state", toggle)
    text = toggle ? "\\" : "/";
    msg = null;
}else{
    if(toggle === true){
        msg.position = 1;
        text = "\\";
    }else{
        msg.position = 0;
        text = "/";
    }
}
node.status({text});
return msg;

```

~~That isn't going to work as it is receiving a different `payload` than the other ones.  
(Hey, I made a lot of mistakes too. No worries.)~~

~~I'll edit it and see if I can get it working as you expected.~~

Ok, I'm wrong - again.

More digging my end.

Am I supposed to see a `ON` message or only the `OFF` messages?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 January 2025 21:10 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/40 "2025-01-07T21:10:02Z")

</div>

Turn on mcb and a message is sent every second.(simulates electricity turned on. and the light trigger represents the electricity reaching the light.)

Press any toggle and the switch changes state and the new state is displayed, the light no longer receive power and the light trigger stops signalling.  
Press any toggle and the switch changes state and the light trigger receives power again, the light trigger starts flashing again.

This is a full representation of the twoway and intermediate loop in wiring method you showed in your first post.

electricity flowing to light blue dot

 ![chrome_screenshot_7 Jan 2025 21_22_42 GMT](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22c6d44249f86c72efd0732ea2088b7b75ddc4c7.png)  
Press third toggle, electricity to light stops, no blue dot.  
 ![chrome_screenshot_7 Jan 2025 21_23_38 GMT](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a2e1eb717925a38d4c2fa6d29bf13822fbad8143.png)  
Press second toggle, electricty flowing again, blue dot.  
 ![chrome_screenshot_7 Jan 2025 21_24_05 GMT](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b71b5c9b411f59455a2dd08e48cedb03c8ab0290.png)

---

<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:** [7 January 2025 21:25 UTC](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381/41 "2025-01-07T21:25:02Z")

</div>

If I press the `mcb on` button, (everything else default) I get `OFF` messages.

 ![Screenshot from 2025-01-08 08-11-42](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/109f18ff2939cab2fb70c56b5f3b9dd0edeacf4d.png)

Putting that back to normal and clicking the next one,

 ![Screenshot from 2025-01-08 08-15-30](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e6484b76835621d868c1d46a1c0a68bb1d5d57c.png)

Putting that back to normal and clicking on the third `inject` (button) the messages start then stop.

 ![Screenshot from 2025-01-08 08-17-16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c66603032bc108049b2925627032cd4c247b196.png)

For clarity:  
The messages start when things are at their default position.  
Then stop as soon as any one of them is altered.

Although now, I don't really know which is their default positions and which are their toggled one.

But, I am only seeing `OFF` messages.

As I have kind of worked out / seen what I wanted with the flow I posted, I think it may be better I not confuse myself with what is happening here and why.  
No offence.

I also fear you didn't understand my I put the `reset` in my flow.  
I'm anal. I don't like things running when not needed. So I put the reset there to stop the flow _running_ when I didn't need it.

Thinking a bit more on your flow:  
Ok, so you are sending a POWER message, and then - depending on the switches (nodes) - I get an `on` or `off` message.  
Fair enough. Don't know if I need the `power` one, but....

All I am getting is/are the `off` and `power` messages.

NOT seen an `on` message yet.

# AH-HA!

Ok, so every 1000ms you are sending a `power` message to _clock_ things.

So what's the go with the `trigger` node at the end:

 ![Screenshot from 2025-01-08 08-31-10](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13cf0e9a24e1973d466dcf72d0eabece2cd7ff53.png)

That is confusing to me.

[Previous page](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381.md?page=1)

[Next page](https://discourse.nodered.org/t/help-with-how-to-make-a-node-to-do-this-function/94381.md?page=3)
