Input control of Dashboard switch

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

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

I'd set a flow variable with 'switch' and test it in the '???' 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

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

@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.

@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?

[{"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":[]}]
1 Like

The two switch nodes can be combined so you just need the join and a switch, by configuring the switch like this
image
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.

2 Likes

Thank you for clarifying.

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