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?
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?
yep, thinking on it
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.
[{"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:
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;
Ok, tweaked a bit more.
[{"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.
I won't SOLVE
it just now.
Wouldn't mind a second set of eyes to make sure I haven't missed anything obvious.
I think you may have missed a state or two. here's the logic table
and here's a quick knock up of a flow
[{"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
Hereis the two way and one intermediate switch using switch nodes.
[{"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
[{"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":[]}]
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.
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
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)
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.
Just saw a HUGE mistake with the if (x = `1,0`)
line.
Should be ```if (x == 1,0
)
ARGH!
This code:
[{"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:
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.
You are using backticks here, not single quotes:
` vs '
Woo - this thread is getting complicated.
Can I take you back to your original wiring diagram...
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.
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.
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.
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.
[{"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":[]}]
Thanks.
Fixed/replaced. (Though it did seem to be working with the wrong one)
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.
Thanks, but I'm confused.
DEPLOYING it and looking I get this:
The debug
window scrolling away happily.
But then if I press the second inject node, it all just stops.
I'll look at what you did but just saying what I'm seeing.
Ok.
The FIRST function
node... The one on the extreme left:
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?
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
If I press the mcb on
button, (everything else default) I get OFF
messages.
Putting that back to normal and clicking the next one,
Putting that back to normal and clicking on the third inject
(button) the messages start then stop.
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.
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:
That is confusing to me.