# Multi-state switch + passthrough / msg parts

**URL:** <https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069>\
**Category:** General\
**Created:** [3 November 2022 23:29 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069 "2022-11-03T23:29:31Z")\
**Posts on this page:** 14\
**Page:** 3

<div class="post-metadata">

**Author:** ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)\
**Post date:** [6 December 2022 04:28 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/42 "2022-12-06T04:28:08Z")

</div>

Hello again! Wondering if you could take a look at this 3 toggle switch UI template config. The switches work perfectly when toggled manually, but doing it programmatically( topic = "b", payload = "b\_on") generates a lot of duplicate messages, and on initialization I get a whole page. Thank you.

```auto
<div class="sw" style="margin-top: 1px" layout="row">

<div style="margin-left: 7px" ng-cloak>
    <md-switch id="B" ng-model="data.b" aria-label="B" ng-true-value="'b_on'" ng-false-value="'b_off'" ng-change="onChange(data.b)">B</md-switch>
</div>
<div style="margin-left: 5px" ng-cloak>
    <md-switch id="G" ng-model="data.g" aria-label="G" ng-true-value="'g_on'" ng-false-value="'g_off'" ng-change="onChange(data.g)">G</md-switch>
</div>
<div style="margin-left: 15px" ng-cloak>
    <md-switch id="S" ng-model="data.s" aria-label="G" ng-true-value="'s_on'" ng-false-value="'s_off'" ng-change="onChange(data.s)">SIM</md-switch>
</div>

</div>

<script>
    (function(scope) {
        scope.data = {
            b: 'b_on',
            g: 'g_off',
            s: 's_on'
        };
        
        scope.onChange = function(cbState) {
            scope.send({payload: cbState, topic: "switch" });
        };
        
        //handle arriving messages
        scope.$watch('msg', function(msg) {
            if(msg !== undefined && msg !== null){
                try{
                    switch(msg.topic) {
                        case "b":
                            scope.$applyAsync(function(){
                                scope.data.b = msg.payload;
                            });
                            scope.onChange(scope.data.b);
                        break;
                        case "g":
                            scope.$applyAsync(function(){
                                scope.data.g = msg.payload;
                            });
                            scope.onChange(scope.data.g);
                        break; 
                        case "s":
                            scope.$applyAsync(function(){
                                scope.data.s = msg.payload;
                            });
                            scope.onChange(scope.data.s);
                        break;                          

                        default:
                        break;
                    }
                }catch(err){
                    console.error(err);
                }
            }
        });
    })(scope);
</script>

```

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [6 December 2022 18:49 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/43 "2022-12-06T18:49:11Z")

</div>

Unchecked `Pass through messages from input` and only get one output message. This should solve your duplicate message issue. The initialization swarm must be down to the input. Stick a debug node on input to find the culpret.

```auto
[{"id":"7f125b642d47076c","type":"ui_template","z":"d69bda8308d00eeb","group":"1e6261b74bfa30c0","name":"","order":1,"width":0,"height":0,"format":"<div class=\"sw\" style=\"margin-top: 1px\" layout=\"row\">\n\n<div style=\"margin-left: 7px\" ng-cloak>\n <md-switch id=\"B\" ng-model=\"data.b\" aria-label=\"B\" ng-true-value=\"'b_on'\" ng-false-value=\"'b_off'\" ng-change=\"onChange(data.b)\">B</md-switch>\n</div>\n<div style=\"margin-left: 5px\" ng-cloak>\n <md-switch id=\"G\" ng-model=\"data.g\" aria-label=\"G\" ng-true-value=\"'g_on'\" ng-false-value=\"'g_off'\" ng-change=\"onChange(data.g)\">G</md-switch>\n</div>\n<div style=\"margin-left: 15px\" ng-cloak>\n <md-switch id=\"S\" ng-model=\"data.s\" aria-label=\"G\" ng-true-value=\"'s_on'\" ng-false-value=\"'s_off'\" ng-change=\"onChange(data.s)\">SIM</md-switch>\n</div>\n\n</div>\n\n<script>\n (function(scope) {\n scope.data = {\n b: 'b_on',\n g: 'g_off',\n s: 's_on'\n };\n \n scope.onChange = function(cbState) {\n scope.send({payload: cbState, topic: \"switch\" });\n };\n \n //handle arriving messages\n scope.$watch('msg', function(msg) {\n if(msg !== undefined && msg !== null){\n try{\n switch(msg.topic) {\n case \"b\":\n scope.$applyAsync(function(){\n scope.data.b = msg.payload;\n });\n scope.onChange(scope.data.b);\n break;\n case \"g\":\n scope.$applyAsync(function(){\n scope.data.g = msg.payload;\n });\n scope.onChange(scope.data.g);\n break; \n case \"s\":\n scope.$applyAsync(function(){\n scope.data.s = msg.payload;\n });\n scope.onChange(scope.data.s);\n break; \n\n default:\n break;\n }\n }catch(err){\n console.error(err);\n }\n }\n });\n })(scope);\n</script>","storeOutMessages":true,"fwdInMessages":false,"resendOnRefresh":true,"templateScope":"local","className":"","x":680,"y":1060,"wires":[["77654f66361ec1a1"]]},{"id":"fc01fa1c46ac8ae9","type":"inject","z":"d69bda8308d00eeb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"g","payload":"g_on","payloadType":"str","x":470,"y":1060,"wires":[["7f125b642d47076c"]]},{"id":"77654f66361ec1a1","type":"debug","z":"d69bda8308d00eeb","name":"debug 25","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":920,"y":1060,"wires":[]},{"id":"1e6261b74bfa30c0","type":"ui_group","name":"Group 4","tab":"7e13e6107768d821","order":4,"disp":true,"width":6},{"id":"7e13e6107768d821","type":"ui_tab","name":"CRYPTO","icon":"dashboard","order":8,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)\
**Post date:** [6 December 2022 19:11 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/44 "2022-12-06T19:11:53Z")

</div>

Thanks, singe input solved. The swarm continues. This the defaults initialization function, not seeing the issue. Basically could be one or both switched turned on with "b" being the default on.

```auto
msgB = {}; msgG = {}

let topic = msg.topic

let exchange = "Binance"

msgB.topic = "b"
msgG.topic = "g"

if(topic === "exchange") {exchange = msg.payload } else {exchange = "Binance"}

if(exchange === "Binance") {

  msgB.payload = "b_on"
  msgG.payload = "g_off"

    
} 

if(exchange === "Gate.io") {

  msgB.payload = "b_off"
  msgG.payload = "g_on"
}  

return [msgB, msgG]

```

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [6 December 2022 19:54 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/45 "2022-12-06T19:54:48Z")

</div>

This should be

```auto
let msgB = {}
let msgG = {}

```

This function is not going to generate a swarm so it must be wherever the input `msg` is coming from. How are you feeding the 2 outputs into the `uiTemplate node`?

---

<div class="post-metadata">

**Author:** ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)\
**Post date:** [6 December 2022 20:47 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/46 "2022-12-06T20:47:29Z")

</div>

This stand-alone flow

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af6b3c6207a9e69c3c5bf9433509ed9b3b776056.png)

```auto
[
    {
        "id": "e960b1e1d199e066",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "20d7abbc73464451",
        "type": "ui_template",
        "z": "e960b1e1d199e066",
        "group": "a5ba0b01d5e9a353",
        "name": "3 SW",
        "order": 5,
        "width": "2",
        "height": "1",
        "format": "<style>\n\n.sw md-switch.md-checked .md-thumb-container {\n transform: none !important;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch.md-checked:not([disabled]) .md-thumb {\n background-color: #ccc;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch .md-bar .myswitch{\n background-color: rgba(111, 111, 111, 0.5);\n display: none;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch.md-checked:not([disabled]) .md-thumb {\n background-color: #609f70;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch .md-bar {\n background-color: rgba(111, 111, 111, 0.5);\n display: none;\n}\n\n.sw md-switch .md-container {\n margin-right: 3px;\n width: 25px;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch .md-thumb {\n background-color: red;\n}\n\nmd-switch {\n align-items: center;\n height: 30px;\n line-height: 18px;\n margin: 0px;\n margin-left: inherit;\n margin-right: 16px;\n}\n\n.sw md-switch .md-label {\n font-size: 13px;\n color: #728faa;\n letter-spacing: 1px;\n font-weight: bold;\n}\n\nmd-switch .md-thumb .md-ripple-container {\n display: none;\n}\n\n</style>\n\n<div class=\"sw\" style=\"margin-top: 1px\" layout=\"row\">\n\n<div style=\"margin-left: 7px\" ng-cloak>\n <md-switch id=\"B\" ng-model=\"data.b\" aria-label=\"B\" ng-true-value=\"'b_on'\" ng-false-value=\"'b_off'\" ng-change=\"onChange(data.b)\">B</md-switch>\n</div>\n<div style=\"margin-left: 5px\" ng-cloak>\n <md-switch id=\"G\" ng-model=\"data.g\" aria-label=\"G\" ng-true-value=\"'g_on'\" ng-false-value=\"'g_off'\" ng-change=\"onChange(data.g)\">G</md-switch>\n</div>\n<div style=\"margin-left: 15px\" ng-cloak>\n <md-switch id=\"S\" ng-model=\"data.s\" aria-label=\"G\" ng-true-value=\"'s_on'\" ng-false-value=\"'s_off'\" ng-change=\"onChange(data.s)\">SIM</md-switch>\n</div>\n\n</div>\n\n<script>\n (function(scope) {\n scope.data = {\n b: 'b_on',\n g: 'g_off',\n s: 's_on'\n };\n \n scope.onChange = function(cbState) {\n scope.send({payload: cbState, topic: \"switch\" });\n };\n \n //handle arriving messages\n scope.$watch('msg', function(msg) {\n if(msg !== undefined && msg !== null){\n try{\n switch(msg.topic) {\n case \"b\":\n scope.$applyAsync(function(){\n scope.data.b = msg.payload;\n });\n scope.onChange(scope.data.b);\n break;\n case \"g\":\n scope.$applyAsync(function(){\n scope.data.g = msg.payload;\n });\n scope.onChange(scope.data.g);\n break; \n case \"s\":\n scope.$applyAsync(function(){\n scope.data.s = msg.payload;\n });\n scope.onChange(scope.data.s);\n break; \n\n default:\n break;\n }\n }catch(err){\n console.error(err);\n }\n }\n });\n })(scope);\n</script>",
        "storeOutMessages": true,
        "fwdInMessages": false,
        "resendOnRefresh": true,
        "templateScope": "local",
        "className": "",
        "x": 810,
        "y": 440,
        "wires": [
            [
                "fdea08aa8ad586f2"
            ]
        ]
    },
    {
        "id": "81688b60b73259ef",
        "type": "function",
        "z": "e960b1e1d199e066",
        "name": "exchange",
        "func": "let msgB = {}; let msgG = {}\n\nlet topic = msg.topic\n\n\nmsgB.topic = \"b\"\nmsgG.topic = \"g\"\n\n\nif(topic === \"exchange\") {exchange = msg.payload } else {exchange = \"Binance\"}\n\nif(exchange === \"Binance\") {\n\n msgB.payload = \"b_on\"\n msgG.payload = \"g_off\"\n\n \n} \n\nif(exchange === \"Gate.io\") {\n\n msgB.payload = \"b_off\"\n msgG.payload = \"g_on\"\n} \n\n\nreturn [msgB, msgG]",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 660,
        "y": 440,
        "wires": [
            [
                "20d7abbc73464451"
            ],
            [
                "20d7abbc73464451"
            ]
        ]
    },
    {
        "id": "fdea08aa8ad586f2",
        "type": "debug",
        "z": "e960b1e1d199e066",
        "name": "debug 33",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 960,
        "y": 440,
        "wires": []
    },
    {
        "id": "79b805e8027c6a5f",
        "type": "inject",
        "z": "e960b1e1d199e066",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "exchange",
        "payload": "Binance",
        "payloadType": "flow",
        "x": 520,
        "y": 440,
        "wires": [
            [
                "81688b60b73259ef"
            ]
        ]
    },
    {
        "id": "a5ba0b01d5e9a353",
        "type": "ui_group",
        "name": "RIGHT",
        "tab": "7e13e6107768d821",
        "order": 4,
        "disp": false,
        "width": "12",
        "collapse": false,
        "className": ""
    },
    {
        "id": "7e13e6107768d821",
        "type": "ui_tab",
        "name": "CRYPTO",
        "icon": "dashboard",
        "order": 2,
        "disabled": false,
        "hidden": false
    }
]

```

with this function

```auto
let msgB = {}; let msgG = {}

let topic = msg.topic

msgB.topic = "b"
msgG.topic = "g"

if(topic === "exchange") {exchange = msg.payload } else {exchange = "Binance"}

if(exchange === "Binance") {

  msgB.payload = "b_on"
  msgG.payload = "g_off"

    
} 

if(exchange === "Gate.io") {

  msgB.payload = "b_off"
  msgG.payload = "g_on"
}  

return [msgB, msgG]

```

is generating this swarm (actually now a lot smaller)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/740424f1bd5d7f5ef4842d2dffe70c92d2a803d5.png)

I have two outputs in the function because I couldn't figure out a different way to send two different sets with two different topics (since I need to set both switches) ... not sure if that's the best way to do it.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [6 December 2022 21:40 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/47 "2022-12-06T21:40:30Z")

</div>

You are getting that because of the two inputs.

I have shown an alternative way of processing the input using `payload.g` & `payload.b`. It may not meet your needs, but it might give you some ideas

Why are you using `scope.$applyAsync` it is causing the` uiTemplate` node output to be 1 input behind? I got rid of it and the node still works. If you have to use it put the `scope.onChange(scope.data.*)` inside the function.

```auto
[{"id":"739de59a292077a9","type":"ui_template","z":"d69bda8308d00eeb","group":"1e6261b74bfa30c0","name":"3 SW","order":5,"width":"5","height":"1","format":"<style>\n\n.sw md-switch.md-checked .md-thumb-container {\n transform: none !important;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch.md-checked:not([disabled]) .md-thumb {\n background-color: #ccc;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch .md-bar .myswitch{\n background-color: rgba(111, 111, 111, 0.5);\n display: none;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch.md-checked:not([disabled]) .md-thumb {\n background-color: #609f70;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch .md-bar {\n background-color: rgba(111, 111, 111, 0.5);\n display: none;\n}\n\n.sw md-switch .md-container {\n margin-right: 3px;\n width: 25px;\n}\n\n.nr-dashboard-theme .nr-dashboard-template md-switch .md-thumb {\n background-color: red;\n}\n\nmd-switch {\n align-items: center;\n height: 30px;\n line-height: 18px;\n margin: 0px;\n margin-left: inherit;\n margin-right: 16px;\n}\n\n.sw md-switch .md-label {\n font-size: 13px;\n color: #728faa;\n letter-spacing: 1px;\n font-weight: bold;\n}\n\nmd-switch .md-thumb .md-ripple-container {\n display: none;\n}\n\n</style>\n\n<div class=\"sw\" style=\"margin-top: 1px\" layout=\"row\">\n\n<div style=\"margin-left: 7px\" ng-cloak>\n <md-switch id=\"B\" ng-model=\"data.b\" aria-label=\"B\" ng-true-value=\"'b_on'\" ng-false-value=\"'b_off'\" ng-change=\"onChange(data.b)\">B</md-switch>\n</div>\n<div style=\"margin-left: 5px\" ng-cloak>\n <md-switch id=\"G\" ng-model=\"data.g\" aria-label=\"G\" ng-true-value=\"'g_on'\" ng-false-value=\"'g_off'\" ng-change=\"onChange(data.g)\">G</md-switch>\n</div>\n<div style=\"margin-left: 15px\" ng-cloak>\n <md-switch id=\"S\" ng-model=\"data.s\" aria-label=\"G\" ng-true-value=\"'s_on'\" ng-false-value=\"'s_off'\" ng-change=\"onChange(data.s)\">SIM</md-switch>\n</div>\n\n</div>\n\n<script>\n (function(scope) {\n scope.data = {\n b: 'b_on',\n g: 'g_off',\n s: 's_on'\n };\n \n scope.onChange = function(cbState) {\n scope.send({payload: cbState, topic: \"switch\" });\n };\n \n //handle arriving messages\n scope.$watch('msg', function(msg) {\n if(msg !== undefined && msg !== null){\n try{\n switch(msg.topic) {\n case \"Binance\":\n case \"Gate.io\":\n //scope.$applyAsync(function(){\n scope.data.b = msg.payload.b;\n scope.data.g = msg.payload.g;\n //});\n scope.onChange(scope.data);\n break; \n case \"s\":\n scope.$applyAsync(function(){\n scope.data.s = msg.payload;\n });\n scope.onChange(scope.data.s);\n break; \n\n default:\n break;\n }\n }catch(err){\n console.error(err);\n }\n }\n });\n })(scope);\n</script>","storeOutMessages":true,"fwdInMessages":false,"resendOnRefresh":true,"templateScope":"local","className":"","x":750,"y":1200,"wires":[["31f5a9de15f6c819"]]},{"id":"c1ec7bd453fa1a1c","type":"function","z":"d69bda8308d00eeb","name":"exchange","func":"let msgB = {}; let msgG = {}\n\nlet topic = msg.topic\nlet exchange = ''\n\nlet newPayload = {}\n\nif(topic === \"exchange\") {exchange = msg.payload } else {exchange = \"Binance\"}\n\nif(exchange === \"Binance\") {\n\n newPayload.b = \"b_on\"\n newPayload.g = \"g_off\"\n\n} \n\nif(exchange === \"Gate.io\") {\n\n newPayload.b = \"b_off\"\n newPayload.g = \"g_on\"\n\n} \n\nmsg.topic = exchange\nmsg.payload = newPayload\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":1200,"wires":[["739de59a292077a9","17259ab11c63879e"]]},{"id":"31f5a9de15f6c819","type":"debug","z":"d69bda8308d00eeb","name":"debug 33","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":900,"y":1200,"wires":[]},{"id":"9c4fd8b5d1548133","type":"inject","z":"d69bda8308d00eeb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"exchange","payload":"Gate.io","payloadType":"str","x":340,"y":1200,"wires":[["c1ec7bd453fa1a1c"]]},{"id":"17259ab11c63879e","type":"debug","z":"d69bda8308d00eeb","name":"debug 34","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":780,"y":1140,"wires":[]},{"id":"1e6261b74bfa30c0","type":"ui_group","name":"Group 4","tab":"7e13e6107768d821","order":4,"disp":true,"width":6},{"id":"7e13e6107768d821","type":"ui_tab","name":"CRYPTO","icon":"dashboard","order":8,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)\
**Post date:** [6 December 2022 22:09 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/48 "2022-12-06T22:09:06Z")

</div>

Oh, the payload.g | payload.b approach is much better, didn't think of it.

The `scope.$applyAsync` is a remnant from the original configuration, I wasn't entirely sure why it was there.

I tried the flow that you sent back (standalone, no changes) - getting 3 duplicate msg instead of 1

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da0ea5c9e744c37601848475736e1e8c9fa209a2.png)

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [6 December 2022 23:01 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/49 "2022-12-06T23:01:43Z")

</div>

Very weird, I only get 1. What do you get out of debug 34?

---

<div class="post-metadata">

**Author:** ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)\
**Post date:** [7 December 2022 03:51 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/50 "2022-12-07T03:51:28Z")

</div>

Yes, weird, the output seems to have normalized on my end. I noticed that you brought in some of the logic into the template itself; I actually really need a "generic" solution.

Could I ask you to do one my pass please. Forget that input function, exchange variable logic, etc. I just want to be able to control the 3 switches independently (in any combination) with a single msg.payload - i.e. "b" on; "g" off; "s" on. Thank you so much!

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [7 December 2022 21:42 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/51 "2022-12-07T21:42:49Z")

</div>

I have made some comments in the uiTemplate node on how to change the way the outputs are presented  
.  
This version

- does not send any data when initialised
- uses payload.b, payload.g, payload.s for both input and output
- values of switches are 'on' and 'off' (so initialisation input would be msg.payload.b = 'on', the output would be the same when a switch is changed).

```auto
[{"id":"7f125b642d47076c","type":"ui_template","z":"d69bda8308d00eeb","group":"1367e4a4e42824b6","name":"New Input","order":2,"width":5,"height":1,"format":"<div class=\"sw\" style=\"margin-top: 1px\" layout=\"row\">\n\n<div style=\"margin-left: 7px\" ng-cloak>\n <md-switch id=\"B\" ng-model=\"data.b\" aria-label=\"B\" ng-true-value = \"'on'\" ng-false-value = \"'off'\" ng-change=\"onChange('b')\">B</md-switch>\n</div>\n<div style=\"margin-left: 5px\" ng-cloak>\n <md-switch id=\"G\" ng-model=\"data.g\" aria-label=\"G\" ng-true-value = \"'on'\" ng-false-value = \"'off'\" ng-change=\"onChange('g')\">G</md-switch>\n</div>\n<div style=\"margin-left: 15px\" ng-cloak>\n <md-switch id=\"S\" ng-model=\"data.s\" aria-label=\"G\" ng-true-value = \"'on'\" ng-false-value = \"'off'\" ng-change=\"onChange('s')\">SIM</md-switch>\n</div>\n\n</div>\n\n<script>\n (function(scope) {\n scope.data = {\n b: 'on',\n g: 'off',\n s: 'on'\n };\n\n // To send state of all switches when any one switch changes state\n // replace \n // {[cbSwitch]: scope.data[cbSwitch]}\n // with\n // scope.data\n scope.onChange = function(cbSwitch) {\n scope.send({payload: {[cbSwitch]: scope.data[cbSwitch]}, topic: \"switch\" })\n\n }\n \n //handle arriving messages\n scope.$watch('msg', function(msg) {\n if(msg !== undefined && msg !== null){\n try {\n let inputData = ''\n for (const property of Object.keys(msg.payload)) {\n if ('bgs'.includes(property)) {\n inputData = msg.payload[property].toLowerCase()\n if (inputData === 'off' || inputData === 'on') {\n scope.data[property] = inputData\n\n // scope.onChange(property) //here to send a msg for each switch on initialisation\n\n }\n }\n\n }\n\n // scope.send(scope.data) //here to send a msg for all switches on initialisation\n \n } catch(err){\n console.error(err)\n }\n }\n });\n })(scope)\n</script>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","className":"","x":680,"y":1060,"wires":[["77654f66361ec1a1"]]},{"id":"fc01fa1c46ac8ae9","type":"inject","z":"d69bda8308d00eeb","name":"","props":[{"p":"payload.b","v":"on","vt":"str"},{"p":"payload.g","v":"off","vt":"str"},{"p":"payload.s","v":"off","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Start","x":470,"y":1060,"wires":[["7f125b642d47076c"]]},{"id":"77654f66361ec1a1","type":"debug","z":"d69bda8308d00eeb","name":"Switch Out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":1060,"wires":[]},{"id":"1367e4a4e42824b6","type":"ui_group","name":"Right","tab":"7e13e6107768d821","order":3,"disp":true,"width":24,"collapse":false,"className":""},{"id":"7e13e6107768d821","type":"ui_tab","name":"CRYPTO","icon":"dashboard","order":8,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)\
**Post date:** [8 December 2022 02:45 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/52 "2022-12-08T02:45:19Z")

</div>

> [@Buckskin](#):
>
> msg.payload.b

Thank you, it's exactly what I was looking for! I am still experiencing the issue of duplicated outputs (right double of what it should be), and only when the switch is triggered programmatically (not manually).

I noticed that it happens when other "continuous" routines are running (like stream of data coming from an api) - if the routines are paused, the output becomes normal. What's particularly odd it that it's happening even while the switch is not connected to anything else - just the standalone flow that you sent back. I managed to suppress the duplicates by adding a "dedupe" node. Does any of it make sense?

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [9 December 2022 17:26 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/53 "2022-12-09T17:26:58Z")

</div>

Short answer, no. I don't get any output until I click on a switch. 🤔

---

<div class="post-metadata">

**Author:** ![interscope101](https://avatars.discourse-cdn.com/v4/letter/i/a88e4f/32.png) [@interscope101](https://discourse.nodered.org/u/interscope101)\
**Post date:** [9 December 2022 17:38 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/54 "2022-12-09T17:38:48Z")

</div>

I actually need the output to set defaults, so I uncommented the relevant statement. All works as expected except for unexplained duplicates that seem to be triggered by other routines in the app (even thought it's disconnected from the rest).

---

<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:** [7 February 2023 17:39 UTC](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069/55 "2023-02-07T17:39:33Z")

</div>

Closed on 7 February 2023 17:39 UTC

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

[Previous page](https://discourse.nodered.org/t/multi-state-switch-passthrough-msg-parts/70069.md?page=2)
