Help with how to make a node to do this function

From this link:
Three and four-way switching – Basic Lighting for Electricians: Level 1.

I'm wanting to make a node that (ok, 2 nodes) to emulate the switches in the diagrams.

SPST and DPDT.

No dashboard needed.
inject nodes to control the the node.

A supply isn't really needed, but I want to better understand how it works.

Thought I'd try with N.R. But maybe not the best tool for the job.

If my admitedly rusty schoolboy brain is working right, I THINK this is, in computer terms, an OR process? If either switch is on (1), then messages should flow, if either is off (0), then messages should not flow.

And no, Node-RED isn't a particularly good tool to actually model this. You can model the logic easily enough but you cannot model the physical circuit.

Maybe try something like https://www.circuitlab.com or https://www.circuit-diagram.org which have the added benefit of actually modelling the electrical flow.

1 Like

I think you'll find the functionality is that of an Exclusive NOR gate (XNOR).


The truth table shows if both switches are 'off' or both are 'on' then the output is True - and the light goes on. Another way of describing it is... if both inputs have the same state, output is True.
In practice it means if BOTH switches are in the same state, the circuit is completed and the light goes on. If the switches are in DIFFERENT states, the circuit is broken and light is extinguished.

I haven't looked for a while but a quick check shows a number of boolean nodes in the pallete manager. Maybe there is a quick install that has that logic in it?

Thanks all, but....

The examples posted as replies only have ONE output and TWO inputs.

Node-red doesn't allow two inputs.

The one output is also a problem - for me - because:

Even the first switch has two outputs. They are complimentary.

I can't see how to use the nodes suggested.
I had drawn up a picture but made a mess of it and so didn't post it as it would have only complicated the situation.

Thanks.

I'll look at them soon.

They do seem better for what I am wanting to do.

The spst is certainly straightforward, on/off, the dpdt may be a little more convoluted but it is actually two switches housed together. Usually center off then one direction goes out one terminal the other direction goes out the other terminal.i don't see multiple inputs if you break it down. In the real world things can get messy as there are momentary switches and make then break options. Maybe a little more on what you are looking to do or is this simply educational?

My original thought was to cheat and use function nodes and write a bit of JS code to emulate the state.

Some status lines to help visualise what's going on.

But, I seem to be lacking the idea of how to do that.

EG:
(starting with the first switch)
It would send either 0,1 or 1,0 (as would all actually - well, ok, maybe not the last one)

Then that message would be parsed by the next node and depending on how it is set would either echo the received message or flip it.

Yes, that would require an extra node - but I did mention that - to set each node's state.
Simple inject node with a payload detected by the node and used to control how the messages are handled.

I've just got up. I may give it a go now and post what I've done.
Inspired by what people have posted here.

Sorry but I feel I'm not seeing your whole problem. SPST simple on or off. DPDT swtich in one direction signal out one way, switch in other direction signal out other direction, a core switch node , first logic = 0 out first output, second logic =1 out second output,

Yes, that is good for the output/s.

But to parse that at the next node is..... confusing/difficult. For me at this time.

I have this working so far. But it is a problem with the %1 bit.
(Help)

let x = msg.payload;
let y;
if (x == "TOGGLE")
{
    y = context.get("toggle") || 0;
    y = ((y + 1) % 1);
    node.warn(y)
    context.set("toggle,y")
    return;
}
if (y == 1)
{
    //
    if (x = `1,0`)
    {
        x = `0,1`
        msg.payload = x;
    }
    //
    if (x = `0,1`)
    {
        x = `1,1`
        msg.payload = x;
    }
}
return msg;

So really what I'm hearing is you really don't want DPDT you are looking to emulate the 3 way switch logic.

I think a major problem in Node-red simulating the wiring of a two way switch is that sometimes current flows through the bulb from left to right and sometimes it flows right to left (and of course if it's off, neither)

Node-red wires are unidirectional.

So here is a (sort of) Single Pole Single Throw switch wired as it would be for a two way switch.
It has one input wire (live) and two output wires.


If the switch is up then the top output is live. If it's down, the bottom is live.
(There is a neutral wire involved somewhere too and I might have live and neutral confused)

But I can't see how to simulate the lightbulb which might get its live from one side, from the other or from neither.

1 Like

@gerry
No, I just am having problems with passing the state from one node to the next.

@jbudd
Yes, that is a bit of a trick there.

I would put another node after all the function nodes and it would parse only the first part of the message.

All,
WRT the %1 line. Ok, it needs to be %2 - sorry. I'll cop that.
But for some reason it isn't working.

The two end ones are SPDT swtiches, the middle one is an intermediate switch or crossover, can be made with a DPDT contact with a couple of link wires. Do you want to understand how the wiring diagram works or create it in node-red?

More just to understand how it works. NR is the only tool I have and so am limited to how I can build the example/s of things taking my interest.

totally untested but I think this is where you are going

[{"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":220,"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":280,"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\nlet S1 = context.get(\"S1\");\nlet S2 = context.get(\"S2\");\n\nif (S1 == 1 && S2 == 1){\n    //both switches up \n    //logic good - logic one on output\n}\n\nif (S1 == 0 && S2 == 0) {\n    //both switches dn \n    //logic good - logic one on output\n}\n\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":160,"wires":[[]]}]

The two end ones are up or down, The middle is straight though on both wires or crossover. Draw the possible permutations out. With the 3 switches there are 8 possible drawings. 4 will connect left to right and 4 will break and not connect.

If you draw them out and follow them you can see how each movement of a switch affects the connection through, allowing electricery to pass or not.

So you are wanting a four way?

This is what I have made ITMT.

[{"id":"79dfd23d02073a19","type":"function","z":"b25fd092.bcc758","name":"function 186","func":"let x = msg.payload;\nlet y;\nif (x == \"TOGGLE\")\n{\n    y = context.get(\"toggle\") || 0;\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\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,1`\n        msg.payload = x;\n    }\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":6490,"wires":[["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"]]}]

I'll look at what you posted now @gerry

OH!

I only edited the first function node.

The last two have OLD code in them.
Just noticed.