Changing sensor inputs

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.

Have a look at this node:

Something like this:

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

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?

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.

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:

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

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

[{"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 :grin:

1 Like

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.

2 Likes

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