# Input control of Dashboard switch

**URL:** <https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844>\
**Category:** Dashboard\
**Created:** [29 March 2020 17:17 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844 "2020-03-29T17:17:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![raphaelgl](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@raphaelgl](https://discourse.nodered.org/u/raphaelgl)\
**Post date:** [29 March 2020 17:17 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844/1 "2020-03-29T17:17:18Z")

</div>

Hi everyone,  
i would like to build e simple Switch which "switch" a constant payload to output1 or output2.

pseudocode:

> if(switch.state == true)  
> output1= testvalue;  
> else  
> output2=testvalue;

![Unbenannt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/46b6cb51a5b9a135a92b962b63b71cc216d24655.png)

Finally: testvalue is a string from Serialstream, output1 and 2 a string inject  
Thanks in advance for your support!  
Raphael

---

<div class="post-metadata">

**Author:** ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)\
**Post date:** [29 March 2020 17:41 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844/2 "2020-03-29T17:41:26Z")

</div>

I'd set a flow variable with 'switch' and test it in the '???' node.

---

<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:** [29 March 2020 17:45 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844/3 "2020-03-29T17:45:41Z")

</div>

> [@bogorad](#):
>
> Looks like you need a gate node

I don't think that allows switching between two outputs does it?

In order to do this the first problem is that you need both the switch state and the input testvalue together in one node, so you can decide what to do.  
To solve that part of the problem have a look at the cookbook example on joining streams using the Join node in key/value pair mode.  
[https://cookbook.nodered.org/basic/join-streams](https://cookbook.nodered.org/basic/join-streams)

---

<div class="post-metadata">

**Author:** ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)\
**Post date:** [29 March 2020 17:48 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844/4 "2020-03-29T17:48:08Z")

</div>

> [@Colin](#):
>
> I don't think that allows switching between two outputs does it?

You are absolutely correct, I've been revising my reply while you were typing 🙂

---

<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:** [29 March 2020 19:24 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844/5 "2020-03-29T19:24:14Z")

</div>

@raphaelgl you could either use flow context as suggested by @bogorad or a Join node. Using the context is easier but you have to be careful using context for such things as if you do it too much it becomes more and more difficult to follow what is happening. Personally I virtually never use the context for such things but many think I am too zealous in my avoidance of this technique.

---

<div class="post-metadata">

**Author:** ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)\
**Post date:** [31 March 2020 22:02 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844/6 "2020-03-31T22:02:54Z")

</div>

@Colin just our of curiosity, did you mean something like this:

- join (manual,payload,key/value,topic)
- switch (ignore topic of the on/off buttons)
- check if gate open

-- or there's an easier/more elegant solution?

```auto
[{"id":"d297ad2a.f0cc3","type":"join","z":"cf8f0132.408f8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":390,"y":280,"wires":[["a6ece895.a32c88"]]},{"id":"e3a9c651.4a8ad8","type":"inject","z":"cf8f0132.408f8","name":"","topic":"gate","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":160,"y":280,"wires":[["d297ad2a.f0cc3"]]},{"id":"a081bf2.0be0b4","type":"debug","z":"cf8f0132.408f8","name":"open","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":260,"wires":[]},{"id":"35ccc47e.fc964c","type":"inject","z":"cf8f0132.408f8","name":"","topic":"value","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":240,"wires":[["d297ad2a.f0cc3"]]},{"id":"8598d735.02d028","type":"inject","z":"cf8f0132.408f8","name":"","topic":"gate","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":320,"wires":[["d297ad2a.f0cc3"]]},{"id":"a6ece895.a32c88","type":"switch","z":"cf8f0132.408f8","name":"topic=value","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"value","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":530,"y":280,"wires":[["4e2b1ccf.dbc6e4"]]},{"id":"4e2b1ccf.dbc6e4","type":"switch","z":"cf8f0132.408f8","name":"gate","property":"payload.gate","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":670,"y":280,"wires":[["a081bf2.0be0b4"],["4c54cf9c.75277"]]},{"id":"4c54cf9c.75277","type":"debug","z":"cf8f0132.408f8","name":"closed","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":300,"wires":[]}]

```

---

<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:** [1 April 2020 08:35 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844/7 "2020-04-01T08:35:10Z")

</div>

> [@bogorad](#):
>
> did you mean something like this

The two switch nodes can be combined so you just need the join and a switch, by configuring the switch like this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cd13777350cf24c5c916b7b8dcc9a894ae3ebe8e.png)  
If the topic is "value" then the JSONata expression evaluates to `true` or `false` so the message goes to o/p 1 or 2 as appropriate, but if the topic is not "value" then the expression evaluates to 0 so neither of the following tests is satisfied and no message is sent.

---

<div class="post-metadata">

**Author:** ![bogorad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bogorad/32/18900_2.png) [@bogorad](https://discourse.nodered.org/u/bogorad)\
**Post date:** [1 April 2020 09:40 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844/8 "2020-04-01T09:40:49Z")

</div>

Thank you for clarifying.

---

<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:** [15 April 2020 09:40 UTC](https://discourse.nodered.org/t/input-control-of-dashboard-switch/23844/9 "2020-04-15T09:40:49Z")

</div>

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