# Changing sensor inputs

**URL:** https://discourse.nodered.org/t/changing-sensor-inputs/60821
**Category:** General
**Created:** [5 April 2022 09:17 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821 "2022-04-05T09:17:26Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Surefire01](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/surefire01/32/71490_2.png) [@Surefire01](https://discourse.nodered.org/u/Surefire01)
#### Post date: [5 April 2022 09:17 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821/1 "2022-04-05T09:17:26Z")

</div>

Chaps.  
Looking to make my Node-Red heating flow more flexible. Currently a hallway Sonoff sensor provides MQTT temperature data to my flow which then controls my central heating. I would like to add a second sensor which sends MQTT temperature data from a different room. I would like the ability to easily change which sensor data is used. I envisage a change over switch in the dashboard which can be labeled sensor 1/sensor 2 and when selected, the appropriate data is used to control the heating. I feel sure its just a matter of adding some code to a function node to switch between the two but I'm having trouble imagining it. I have included a screen shot showing two flows that I imagine could be used but I'm unsure as which one would be best.  
Any pointers/examples would be gratefully received.

 ![Heating selector flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/51015c8431fa12dbff83627646270eb13a65dfb0.jpeg)

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [5 April 2022 09:30 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821/2 "2022-04-05T09:30:42Z")

</div>

Have a look at this node:

> **[node-red-contrib-simple-gate](https://flows.nodered.org/node/node-red-contrib-simple-gate)**
>
> A gate node for node-RED

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [5 April 2022 10:07 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821/3 "2022-04-05T10:07:41Z")

</div>

Something like this:

```auto
[
    {
        "id": "b0423532fbc767c5",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "cc4f83b3bdf7e7cc",
        "type": "inject",
        "z": "b0423532fbc767c5",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Sensor 1",
        "payloadType": "str",
        "x": 220,
        "y": 40,
        "wires": [
            [
                "3332362c8575cde9"
            ]
        ]
    },
    {
        "id": "ae9577b39a108727",
        "type": "inject",
        "z": "b0423532fbc767c5",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Sensor 2",
        "payloadType": "str",
        "x": 200,
        "y": 300,
        "wires": [
            [
                "c7d825e9f48255cc"
            ]
        ]
    },
    {
        "id": "6d7bb652b60b7ec2",
        "type": "debug",
        "z": "b0423532fbc767c5",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 160,
        "wires": []
    },
    {
        "id": "3332362c8575cde9",
        "type": "gate",
        "z": "b0423532fbc767c5",
        "name": "",
        "controlTopic": "control",
        "defaultState": "open",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": false,
        "storeName": "memory",
        "x": 510,
        "y": 100,
        "wires": [
            [
                "6d7bb652b60b7ec2"
            ]
        ]
    },
    {
        "id": "c7d825e9f48255cc",
        "type": "gate",
        "z": "b0423532fbc767c5",
        "name": "",
        "controlTopic": "control",
        "defaultState": "open",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": false,
        "storeName": "memory",
        "x": 510,
        "y": 240,
        "wires": [
            [
                "6d7bb652b60b7ec2"
            ]
        ]
    },
    {
        "id": "63552570c5d92081",
        "type": "inject",
        "z": "b0423532fbc767c5",
        "name": "Choose 1",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "Control",
        "payload": "open",
        "payloadType": "str",
        "x": 140,
        "y": 100,
        "wires": [
            [
                "3332362c8575cde9",
                "3a9a1a6ccfed5f14"
            ]
        ]
    },
    {
        "id": "26b5604688e917ef",
        "type": "inject",
        "z": "b0423532fbc767c5",
        "name": "Choose 2",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "Control",
        "payload": "open",
        "payloadType": "str",
        "x": 140,
        "y": 240,
        "wires": [
            [
                "c7d825e9f48255cc",
                "0867e89ae4cc94a4"
            ]
        ]
    },
    {
        "id": "3a9a1a6ccfed5f14",
        "type": "change",
        "z": "b0423532fbc767c5",
        "name": "Close 2",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "close",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Control",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 320,
        "y": 160,
        "wires": [
            [
                "c7d825e9f48255cc"
            ]
        ]
    },
    {
        "id": "0867e89ae4cc94a4",
        "type": "change",
        "z": "b0423532fbc767c5",
        "name": "Close 1",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "close",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Control",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 320,
        "y": 200,
        "wires": [
            [
                "3332362c8575cde9"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![Surefire01](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/surefire01/32/71490_2.png) [@Surefire01](https://discourse.nodered.org/u/Surefire01)
#### Post date: [5 April 2022 10:24 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821/4 "2022-04-05T10:24:52Z")

</div>

Thanks for that ghayne, works well. Is there a way to replace the choose 1 and choose 2 inject nodes with one slider switch operated from the dashboard?

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [5 April 2022 10:27 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821/5 "2022-04-05T10:27:48Z")

</div>

> [@Surefire01](#):
>
> Is there a way to replace the choose 1 and choose 2 inject nodes with one slider switch operated from the dashboard?

It's probably simple, unfortunately I almost never use the dashboard so I can't help you there.  
I'm sure others will be able to help.

---

<div class="post-metadata">

### Author: ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)
#### Post date: [5 April 2022 10:46 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821/6 "2022-04-05T10:46:06Z")

</div>

This just uses 1 switch, notice the default state of the switch nodes, they need to be set to oppose each other or it won't work:

```auto
[
    {
        "id": "b0423532fbc767c5",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "cc4f83b3bdf7e7cc",
        "type": "inject",
        "z": "b0423532fbc767c5",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Sensor 1",
        "payloadType": "str",
        "x": 200,
        "y": 100,
        "wires": [
            [
                "3332362c8575cde9"
            ]
        ]
    },
    {
        "id": "ae9577b39a108727",
        "type": "inject",
        "z": "b0423532fbc767c5",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Sensor 2",
        "payloadType": "str",
        "x": 200,
        "y": 220,
        "wires": [
            [
                "c7d825e9f48255cc"
            ]
        ]
    },
    {
        "id": "6d7bb652b60b7ec2",
        "type": "debug",
        "z": "b0423532fbc767c5",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 160,
        "wires": []
    },
    {
        "id": "3332362c8575cde9",
        "type": "gate",
        "z": "b0423532fbc767c5",
        "name": "",
        "controlTopic": "control",
        "defaultState": "open",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": false,
        "storeName": "memory",
        "x": 510,
        "y": 100,
        "wires": [
            [
                "6d7bb652b60b7ec2"
            ]
        ]
    },
    {
        "id": "c7d825e9f48255cc",
        "type": "gate",
        "z": "b0423532fbc767c5",
        "name": "",
        "controlTopic": "control",
        "defaultState": "closed",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": false,
        "storeName": "memory",
        "x": 510,
        "y": 220,
        "wires": [
            [
                "6d7bb652b60b7ec2"
            ]
        ]
    },
    {
        "id": "63552570c5d92081",
        "type": "inject",
        "z": "b0423532fbc767c5",
        "name": "Toggle",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "Control",
        "payload": "toggle",
        "payloadType": "str",
        "x": 190,
        "y": 160,
        "wires": [
            [
                "3332362c8575cde9",
                "c7d825e9f48255cc"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [5 April 2022 10:47 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821/7 "2022-04-05T10:47:57Z")

</div>

Probably very similar to what @ghayne just posted...  
OK, similar yes, but his use of the default state is more elegant.

Assuming that the values sent by your Switch are "hall" or "room1", and it sets msg.topic to "control":  
You need two gates. For one the Open command is "room1" and Close is "hall". The other gate has them the other way round.

So the switch will open one gate and close the other.  
 ![Untitled 5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/00d764150b275337d143a4e96ac6d8b2e2f9909f.jpeg)

```auto
[{"id":"d85150ca7ed60af6","type":"function","z":"60ddb49f23d04d93","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":340,"wires":[[]]},{"id":"5976f1ca9e996f54","type":"gate","z":"60ddb49f23d04d93","name":"","controlTopic":"control","defaultState":"open","openCmd":"room1","closeCmd":"hall","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memory","x":570,"y":320,"wires":[["d85150ca7ed60af6"]]},{"id":"a60f237431ce8ba8","type":"gate","z":"60ddb49f23d04d93","name":"","controlTopic":"control","defaultState":"open","openCmd":"hall","closeCmd":"room1","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memory","x":570,"y":360,"wires":[["d85150ca7ed60af6"]]},{"id":"994f40a34b80cba6","type":"ui_switch","z":"60ddb49f23d04d93","name":"","label":"switch","tooltip":"","group":"73d32e8c8352f4c3","order":2,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"control","topicType":"str","style":"","onvalue":"hall","onvalueType":"str","onicon":"","oncolor":"","offvalue":"room1","offvalueType":"str","officon":"","offcolor":"","animate":false,"className":"","x":350,"y":340,"wires":[["a60f237431ce8ba8","5976f1ca9e996f54"]]},{"id":"278f3c3f82186ed9","type":"mqtt in","z":"60ddb49f23d04d93","name":"","topic":"sensor/hall","qos":"2","datatype":"auto","broker":"4c682b3a.2ab5c4","nl":false,"rap":true,"rh":0,"inputs":0,"x":360,"y":300,"wires":[["5976f1ca9e996f54"]]},{"id":"eec7b926e7abc67e","type":"mqtt in","z":"60ddb49f23d04d93","name":"","topic":"sensor/room1","qos":"2","datatype":"auto","broker":"4c682b3a.2ab5c4","nl":false,"rap":true,"rh":0,"inputs":0,"x":370,"y":380,"wires":[["a60f237431ce8ba8"]]},{"id":"73d32e8c8352f4c3","type":"ui_group","name":"One","tab":"ef7df567c051ee20","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"4c682b3a.2ab5c4","type":"mqtt-broker","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"ef7df567c051ee20","type":"ui_tab","name":"Demo","icon":"dashboard","disabled":false,"hidden":false}]

```

Edit, nope, that's not right. sensor/hall should only be wired to gate1, sensor/room1 to gate2  
There, that's better. And no nasty crossed wires 😁

---

<div class="post-metadata">

### Author: ![Surefire01](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/surefire01/32/71490_2.png) [@Surefire01](https://discourse.nodered.org/u/Surefire01)
#### Post date: [5 April 2022 11:42 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821/8 "2022-04-05T11:42:22Z")

</div>

Thank you Mr Hayne and Mr Budd for your participation and your insights. With your assistance I have once again been able to achieve what I wanted. What a great forum.

---

<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: [4 June 2022 11:43 UTC](https://discourse.nodered.org/t/changing-sensor-inputs/60821/9 "2022-06-04T11:43:19Z")

</div>

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