# 18 binary switches

**URL:** <https://discourse.nodered.org/t/18-binary-switches/35840>\
**Category:** General\
**Created:** [12 November 2020 23:23 UTC](https://discourse.nodered.org/t/18-binary-switches/35840 "2020-11-12T23:23:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobiasn2005](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobiasn2005/32/31900_2.png) [@Tobiasn2005](https://discourse.nodered.org/u/Tobiasn2005)\
**Post date:** [12 November 2020 23:23 UTC](https://discourse.nodered.org/t/18-binary-switches/35840/1 "2020-11-12T23:23:31Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 November 2020 23:30 UTC](https://discourse.nodered.org/t/18-binary-switches/35840/2 "2020-11-12T23:30:48Z")

</div>

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](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 November 2020 07:25 UTC](https://discourse.nodered.org/t/18-binary-switches/35840/4 "2020-11-13T07:25:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tobiasn2005](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobiasn2005/32/31900_2.png) [@Tobiasn2005](https://discourse.nodered.org/u/Tobiasn2005)\
**Post date:** [13 November 2020 07:47 UTC](https://discourse.nodered.org/t/18-binary-switches/35840/5 "2020-11-13T07:47:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 November 2020 08:45 UTC](https://discourse.nodered.org/t/18-binary-switches/35840/6 "2020-11-13T08:45:52Z")

</div>

> [@Tobiasn2005](#):
>
> Any chance you could create an example with 3 or 4 switches

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/2/02ae04c261a3f9d62aea94573c61ed2b0cf28982.png)

Import this demo flow...

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

```

---

<div class="post-metadata">

**Author:** ![Tobiasn2005](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobiasn2005/32/31900_2.png) [@Tobiasn2005](https://discourse.nodered.org/u/Tobiasn2005)\
**Post date:** [13 November 2020 09:17 UTC](https://discourse.nodered.org/t/18-binary-switches/35840/7 "2020-11-13T09:17:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 November 2020 09:17 UTC](https://discourse.nodered.org/t/18-binary-switches/35840/8 "2020-11-27T09:17:05Z")

</div>

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