# Correct use of switch

**URL:** <https://discourse.nodered.org/t/correct-use-of-switch/35212>\
**Category:** General\
**Created:** [2 November 2020 14:12 UTC](https://discourse.nodered.org/t/correct-use-of-switch/35212 "2020-11-02T14:12:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [2 November 2020 14:12 UTC](https://discourse.nodered.org/t/correct-use-of-switch/35212/1 "2020-11-02T14:12:21Z")

</div>

hello  
I read a set of data from a DB, which is available to me as an array with 19 objects.

1. payload[0].MO  
..... up to
2. payload[0].Z\_Tone.

Can I split this with switch so that I have the 19 values at 19 outputs or do I have to use 19 functions.  
I get all possible at the outputs of the switch, except the read value from database.  
they can be true ore false; 1-100 and timestamps.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [2 November 2020 14:29 UTC](https://discourse.nodered.org/t/correct-use-of-switch/35212/2 "2020-11-02T14:29:40Z")

</div>

Not sure what you are asking.  
if you want to split the message into 19 different messages, use the split node.

Maybe you can give more explanation of what you are attempting to do.

---

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [2 November 2020 14:49 UTC](https://discourse.nodered.org/t/correct-use-of-switch/35212/3 "2020-11-02T14:49:36Z")

</div>

That is what I revive from maria DB:  
2.11.2020, 15:28:33[node: bd410f46.2525d](http://W.X.Y.Z1880/#)SELECT \* FROM Timer1 ORDER BY TIMESTAMP DESC LIMIT 1 : msg : Object  
object  
\_msgid: "2b0bfc16.ca5dd4"  
payload: array[1]  
0: object  
TIMESTAMP: "2020-11-01T22:59:53.000Z"  
MO: 1  
DI: 1  
MI: 1  
DO: 1  
FR: 1  
SA: 1  
SO: 1  
SRoff: 0  
SRon: 0  
SSoff: 0  
SSon: 1  
Toff: 1  
Ton: 0  
Auto: 0  
Ausgang: 0  
ZToff: "1604271600000"  
ZTon: "0"  
Z\_Toff: "23:00"  
Z\_Ton: "00:00"  
topic: "SELECT \* FROM Timer1 ORDER BY TIMESTAMP DESC LIMIT 1"

instead of creating 19 functions I want to break up the array  
I need to supply the 19 values to different switches or other functions. This is the status of the last saved state of the flow to bring node red after a restart back to work.  
Because this does not work as an array for a switch, i have to break this into single steps  
I am looking to find a nicer way as for using a bunch of functions.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [2 November 2020 15:23 UTC](https://discourse.nodered.org/t/correct-use-of-switch/35212/4 "2020-11-02T15:23:16Z")

</div>

example hope it helps

```auto
[{"id":"ec5fa027.d21158","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"mo\":0},{\"sd\":1},{\"so\":0}]","payloadType":"json","x":170,"y":2900,"wires":[["26c2fb46.151804"]]},{"id":"26c2fb46.151804","type":"function","z":"8d22ae29.7df6d","name":"","func":"var msg1 =[];\nfor(var a in msg.payload){\n msg1.push([{\"payload\":msg.payload[a]}]) \n }\nreturn msg1;","outputs":3,"noerr":0,"initialize":"","finalize":"","x":310,"y":2880,"wires":[["8272e1e1.6298d8"],["511689ea.78f488"],["927b9d3c.c35e78"]]},{"id":"8272e1e1.6298d8","type":"debug","z":"8d22ae29.7df6d","name":"1111111111","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":580,"y":2860,"wires":[]},{"id":"927b9d3c.c35e78","type":"debug","z":"8d22ae29.7df6d","name":"3333333333","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":580,"y":2940,"wires":[]},{"id":"511689ea.78f488","type":"debug","z":"8d22ae29.7df6d","name":"222222222","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":580,"y":2900,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![edstobi](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@edstobi](https://discourse.nodered.org/u/edstobi)\
**Post date:** [3 November 2020 08:16 UTC](https://discourse.nodered.org/t/correct-use-of-switch/35212/5 "2020-11-03T08:16:08Z")

</div>

Thanks  
this helps me and I now understand how it works with multiple outputs in the function.  
thank you very much for this

---

<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:** [17 November 2020 08:16 UTC](https://discourse.nodered.org/t/correct-use-of-switch/35212/6 "2020-11-17T08:16:11Z")

</div>

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