Preparing MQTT Flow for Embedded Devices

Hi,

So I'm trying to build a dashboard for our laboratory to control some custom-built temperature controllers. I thought this was going to be a relatively straightforward task, but it's been giving me fits.

I want to design a basic dashboard with some parameters that a user can pick (Set Point, Soak Time, Lab Location, Start Reaction), which will allow them to build a reaction profile, and then send a single JSON message to the correct temperature controller, which will start the reaction.

Since I'm sending this to an embedded device, I want my JSON message to be relatively compact, which means that I have to use msg.topic for each key:Value pair that I want. Unfortunately, this means that I'm not able to figure out a way to use a dropdown to dynamically set which temperature controller I want to use. I'm using the following node setup (which has obvious problems, in that it doesn't work):

[
    {
        "id": "bf7a42a58759485f",
        "type": "debug",
        "z": "f17041932f355def",
        "name": "debug 6",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1020,
        "y": 1400,
        "wires": []
    },
    {
        "id": "2290481215668df2",
        "type": "join",
        "z": "f17041932f355def",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 790,
        "y": 1420,
        "wires": [
            [
                "bf7a42a58759485f"
            ]
        ]
    },
    {
        "id": "5fa2b4373297c37e",
        "type": "ui_button",
        "z": "f17041932f355def",
        "name": "",
        "group": "a2934e4658244d31",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Start RXN",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "true",
        "payloadType": "bool",
        "topic": "RunRXN",
        "topicType": "str",
        "x": 380,
        "y": 1480,
        "wires": [
            [
                "dafe2fb2e4736a34"
            ]
        ]
    },
    {
        "id": "dafe2fb2e4736a34",
        "type": "change",
        "z": "f17041932f355def",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "complete",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            },
            {
                "t": "set",
                "p": "RunRXN",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 600,
        "y": 1480,
        "wires": [
            [
                "2290481215668df2"
            ]
        ]
    },
    {
        "id": "367910fd3c734d8f",
        "type": "ui_dropdown",
        "z": "f17041932f355def",
        "name": "",
        "label": "",
        "tooltip": "",
        "place": "Select option",
        "group": "a2934e4658244d31",
        "order": 2,
        "width": 0,
        "height": 0,
        "passthru": true,
        "multiple": false,
        "options": [
            {
                "label": "Lab 4109 - Control",
                "value": "lab4109/control",
                "type": "str"
            },
            {
                "label": "Lab 4111 - Control",
                "value": "lab4111/control",
                "type": "str"
            }
        ],
        "payload": "",
        "topic": "topic",
        "topicType": "msg",
        "className": "",
        "x": 380,
        "y": 1300,
        "wires": [
            [
                "bf7a42a58759485f"
            ]
        ]
    },
    {
        "id": "05238e597c1d8d31",
        "type": "ui_text_input",
        "z": "f17041932f355def",
        "name": "",
        "label": "Set Point",
        "tooltip": "",
        "group": "a2934e4658244d31",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "number",
        "delay": 300,
        "topic": "setpoint",
        "sendOnBlur": true,
        "className": "",
        "topicType": "str",
        "x": 380,
        "y": 1420,
        "wires": [
            [
                "2290481215668df2"
            ]
        ]
    },
    {
        "id": "08a346c62aa0695a",
        "type": "ui_text_input",
        "z": "f17041932f355def",
        "name": "",
        "label": "Soak Time (min)",
        "tooltip": "",
        "group": "a2934e4658244d31",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": true,
        "mode": "number",
        "delay": 300,
        "topic": "SoakTime",
        "sendOnBlur": true,
        "className": "",
        "topicType": "str",
        "x": 400,
        "y": 1360,
        "wires": [
            [
                "2290481215668df2"
            ]
        ]
    },
    {
        "id": "a2934e4658244d31",
        "type": "ui_group",
        "name": "Debug",
        "tab": "dfc99f1973056be7",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "dfc99f1973056be7",
        "type": "ui_tab",
        "name": "Testing",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

I would like to be able to use a dropdown to set the message topic, but I simply can't figure a way to get it working. Worst case scenario, I was planning on using a Switch node to direct the message, but that's not working for me either. Is there a better way to accomplish this?

Thanks

Hi and welcome

Can you not just join it then move it to topic in a change node, which would also remove it from the payload.
e.g.

[{"id":"2290481215668df2","type":"join","z":"452103ea51141731","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":450,"y":4300,"wires":[["6dcd76f72528986b"]]},{"id":"dafe2fb2e4736a34","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"complete","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"RunRXN","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":4360,"wires":[["2290481215668df2"]]},{"id":"05238e597c1d8d31","type":"ui_text_input","z":"452103ea51141731","name":"","label":"Set Point","tooltip":"","group":"a2934e4658244d31","order":3,"width":0,"height":0,"passthru":true,"mode":"number","delay":300,"topic":"setpoint","sendOnBlur":true,"className":"","topicType":"str","x":60,"y":4300,"wires":[["2290481215668df2"]]},{"id":"08a346c62aa0695a","type":"ui_text_input","z":"452103ea51141731","name":"","label":"Soak Time (min)","tooltip":"","group":"a2934e4658244d31","order":4,"width":0,"height":0,"passthru":true,"mode":"number","delay":300,"topic":"SoakTime","sendOnBlur":true,"className":"","topicType":"str","x":80,"y":4240,"wires":[["2290481215668df2"]]},{"id":"367910fd3c734d8f","type":"ui_dropdown","z":"452103ea51141731","name":"","label":"","tooltip":"","place":"Select option","group":"a2934e4658244d31","order":2,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"Lab 4109 - Control","value":"lab4109/control","type":"str"},{"label":"Lab 4111 - Control","value":"lab4111/control","type":"str"}],"payload":"","topic":"dropdown","topicType":"str","className":"","x":60,"y":4180,"wires":[["2290481215668df2"]]},{"id":"6dcd76f72528986b","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"move","p":"payload.dropdown","pt":"msg","to":"topic","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":4260,"wires":[["bf7a42a58759485f"]]},{"id":"5fa2b4373297c37e","type":"ui_button","z":"452103ea51141731","name":"","group":"a2934e4658244d31","order":1,"width":0,"height":0,"passthru":false,"label":"Start RXN","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"true","payloadType":"bool","topic":"RunRXN","topicType":"str","x":60,"y":4360,"wires":[["dafe2fb2e4736a34"]]},{"id":"bf7a42a58759485f","type":"debug","z":"452103ea51141731","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":4380,"wires":[]},{"id":"a2934e4658244d31","type":"ui_group","name":"Debug","tab":"dfc99f1973056be7","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"dfc99f1973056be7","type":"ui_tab","name":"Testing","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

Try this:

[{"id":"4e7137d98fc54cb4","type":"tab","label":"Flow 7","disabled":false,"info":"","env":[]},{"id":"bf7a42a58759485f","type":"debug","z":"4e7137d98fc54cb4","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":900,"y":240,"wires":[]},{"id":"2290481215668df2","type":"join","z":"4e7137d98fc54cb4","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":570,"y":240,"wires":[["4030388714bedc19"]]},{"id":"5fa2b4373297c37e","type":"ui_button","z":"4e7137d98fc54cb4","name":"","group":"a2934e4658244d31","order":1,"width":0,"height":0,"passthru":false,"label":"Start RXN","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"true","payloadType":"bool","topic":"RunRXN","topicType":"str","x":160,"y":300,"wires":[["dafe2fb2e4736a34"]]},{"id":"dafe2fb2e4736a34","type":"change","z":"4e7137d98fc54cb4","name":"","rules":[{"t":"set","p":"complete","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"RunRXN","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":300,"wires":[["2290481215668df2"]]},{"id":"367910fd3c734d8f","type":"ui_dropdown","z":"4e7137d98fc54cb4","name":"","label":"","tooltip":"","place":"Select option","group":"a2934e4658244d31","order":2,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"Lab 4109 - Control","value":"lab4109/control","type":"str"},{"label":"Lab 4111 - Control","value":"lab4111/control","type":"str"}],"payload":"","topic":"topic","topicType":"msg","className":"","x":160,"y":120,"wires":[["4fd77b6e40f1d367"]]},{"id":"05238e597c1d8d31","type":"ui_text_input","z":"4e7137d98fc54cb4","name":"","label":"Set Point","tooltip":"","group":"a2934e4658244d31","order":3,"width":0,"height":0,"passthru":true,"mode":"number","delay":300,"topic":"setpoint","sendOnBlur":true,"className":"","topicType":"str","x":160,"y":240,"wires":[["2290481215668df2"]]},{"id":"08a346c62aa0695a","type":"ui_text_input","z":"4e7137d98fc54cb4","name":"","label":"Soak Time (min)","tooltip":"","group":"a2934e4658244d31","order":4,"width":0,"height":0,"passthru":true,"mode":"number","delay":300,"topic":"SoakTime","sendOnBlur":true,"className":"","topicType":"str","x":180,"y":180,"wires":[["2290481215668df2"]]},{"id":"4fd77b6e40f1d367","type":"change","z":"4e7137d98fc54cb4","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"topic","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":120,"wires":[["24257319966e9006"]]},{"id":"24257319966e9006","type":"debug","z":"4e7137d98fc54cb4","name":"debug 39","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":120,"wires":[]},{"id":"4030388714bedc19","type":"change","z":"4e7137d98fc54cb4","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"topic","tot":"flow","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":240,"wires":[["bf7a42a58759485f"]]},{"id":"a2934e4658244d31","type":"ui_group","name":"Debug","tab":"dfc99f1973056be7","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"dfc99f1973056be7","type":"ui_tab","name":"Testing","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

what it does is this

This is not complete, you should probably display the current flow.topic to make sure the user doesn't 'assume' which device it will go to. You might even want to have a verification step - display the choices and have them verify them before sending the mqtt msg

1 Like

Both of these solutions work well. I think I have a fundamental misunderstanding of namespaces(?) within NodeRED which is causing some of my issues when trying to accomplish this.

Is it typical in NodeRED to start a new flow for each individual task that you want to accomplish? I had assumed that you would have one flow per dashboard, but I'm seeing now that this would cause issues when you're trying to use flow.topic for instance.

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

That said, depending on what you are doing, you could have one or many flows on one or many editor tabs. You can also have one or more dashboard tabs.

Data in one flow could be sent to two dashboard tabs. Data from dashboard node will each produce a seperate msg, but you can have multiple ones feed into the same flow.

In other words, many things can be done in many ways :grin: (as you can see in the two answers you got)

Take it slow and play arround. Experiment and when something doesn't work the way you think it should, add debug nodes so you an see what is coming out of nodes.

And remember, like everything else in life, you won't be an expert on day one but each day you will know a little bit more. And you can always ask questions.

1 Like

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