18 binary switches

Good evening, I have 18 binary switches (Boolean) that can be false or true.
Based on the combination of them all I want to have different numeric outputs.
So when switch 1 is true and switch 2 is false and switch 3 is true I need to have a numeric output eg 7
When switch 1 is true and switch 2 is false and switch 3 is false it needs to be 5 eg.
So depending on the input states the output always needs to be the same number.
How could this be achieved?
Thanks a lot!

Do you mean dashboard switches or physical switches?

Either way, one solution would be to hold the is value in context 0 if off, X if on (where X is the bit value - SW1=1, sw2=2, sw3=4, sw4=8 etc)

Then with each switch change, retrieve the values from context & add them up.

See working with context

The alternative (which is my personal preference) is to use a Join node. Look in the node red cookbook for the page Joining Streams.

Looks like something that might work. But how would that look like? Any chance you could create an example with 3 or 4 switches?
They’re btw physical switches that send their statuses to mqtt.
Much appreciated

Normally I would insist you give it a go & I would help out when you are stuck but I think the code might be a bit much if you are very new to node-red/coding (apologies if this is not the case).

So the approach I took was that if your topics from your 18 mqtt items are garage/binswitch1/1, garage/binswitch1/2, garage/binswitch1/3 ... garage/binswitch1/18 the code will store the current value in flow context under garage/binswitch1. Similarly, if your topics from your 18 mqtt items are kitchen/sw1/1, kitchen/sw1/2, kitchen/sw1/3 ... kitchen/sw1/18 the code will store the current value in flow context under kitchen/sw1

This means it is completely generic and reusable.

image

Import this demo flow...

[{"id":"ec72da90.ce7808","type":"inject","z":"ac69cd26.4f506","name":"1 off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"switches/1","payload":"0","payloadType":"num","x":170,"y":780,"wires":[["1f6c3303.89544d"]]},{"id":"e17a639a.c6e5","type":"debug","z":"ac69cd26.4f506","name":"sw binary val","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":1160,"wires":[]},{"id":"9136b2d.e77125","type":"inject","z":"ac69cd26.4f506","name":"1 on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"switches/1","payload":"1","payloadType":"num","x":170,"y":820,"wires":[["1f6c3303.89544d"]]},{"id":"a2353771.e520c8","type":"inject","z":"ac69cd26.4f506","name":"2 off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"switches/2","payload":"0","payloadType":"num","x":170,"y":880,"wires":[["1f6c3303.89544d"]]},{"id":"304e4ea3.ddf472","type":"inject","z":"ac69cd26.4f506","name":"2 on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"switches/2","payload":"2","payloadType":"num","x":170,"y":920,"wires":[["1f6c3303.89544d"]]},{"id":"5897cb96.815f14","type":"inject","z":"ac69cd26.4f506","name":"3 off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"switches/3","payload":"0","payloadType":"num","x":170,"y":980,"wires":[["1f6c3303.89544d"]]},{"id":"fa714125.576f9","type":"inject","z":"ac69cd26.4f506","name":"3 on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"switches/3","payload":"4","payloadType":"num","x":170,"y":1020,"wires":[["1f6c3303.89544d"]]},{"id":"1f6c3303.89544d","type":"link out","z":"ac69cd26.4f506","name":"to mqtt (faking it)","links":["231f6d6b.7327e2"],"x":390,"y":900,"wires":[],"l":true},{"id":"231f6d6b.7327e2","type":"link in","z":"ac69cd26.4f506","name":"switches/# (mqtt in)","links":["1f6c3303.89544d"],"x":190,"y":1160,"wires":[["5d07f85.0a6a308"]],"l":true},{"id":"5d07f85.0a6a308","type":"function","z":"ac69cd26.4f506","name":"","func":"var parts = msg.topic.split(\"/\");\nvar sw = parseInt(parts.pop());//get sw number from topic e.g. topic/subtopic/4 is switch 4 (bit 3)\nvar contextName = parts.join(\"/\") || \"switchValue\"; //create a storage name from topic e.g. topic/subtopic\nvar switchValue = flow.get(contextName) || 0;//retrieve current switch value\nvar bit = sw - 1;//get bit number from\nvar mask = 1 << bit; \nif(msg.payload === true || msg.payload > 0) {\n    switchValue |= mask;//set\n} else {\n    switchValue &= ~mask;\n}    \n\nflow.set(contextName, switchValue);\n\n//return it\nmsg.topic = contextName;\nmsg.payload = switchValue;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":1160,"wires":[["e17a639a.c6e5"]]},{"id":"50888026.1bd01","type":"comment","z":"ac69cd26.4f506","name":"The code to keep","info":"","x":180,"y":1120,"wires":[]},{"id":"db6de1d.215452","type":"comment","z":"ac69cd26.4f506","name":"Fake MQTT sw messages","info":"","x":370,"y":800,"wires":[]},{"id":"b08387e3.89a3e8","type":"comment","z":"ac69cd26.4f506","name":"Delete these after testing","info":"","x":370,"y":840,"wires":[]}]
1 Like

Brilliant!! You're amazing, thanks so much!

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