Get values from different UI node and build JSON payload

Hi guys,

I would like to get values from 2 nodes which are a text input and a drop down list. When the user press a "Send data" button, a node build a JSON payload to send to a device.

The output format should be like that

{
  "command":"testPump",
  "pump":"pumpUp",
  "value":5
}

Here's my code :

[{"id":"55ce81f8.2b74f8","type":"ui_numeric","z":"b8c69cbb.cbe7b","name":"pumpVolume","label":"Volume (ml)","tooltip":"Volume in ml to dispense","group":"d03b9dd9.59f1e8","order":5,"width":0,"height":0,"wrap":false,"passthru":true,"topic":"pumpVolume","format":"{{value}}","min":0,"max":"50","step":"0.5","x":110,"y":1180,"wires":[["41e1ec7a.1bb2b4"]]},{"id":"4aff2f21.5760b8","type":"ui_dropdown","z":"b8c69cbb.cbe7b","name":"ddlPumps","label":"Pump","tooltip":"Select the peristaltic pump to test","place":"Select option","group":"d03b9dd9.59f1e8","order":0,"width":0,"height":0,"passthru":true,"options":[{"label":"pH up","value":"pumpUp","type":"str"},{"label":"ph down","value":"pumpDown","type":"str"},{"label":"anti-foaming","value":"pumpFoam","type":"str"}],"payload":"","topic":"pump","x":100,"y":1240,"wires":[["41e1ec7a.1bb2b4"]]},{"id":"ccbaec0e.1402b8","type":"debug","z":"b8c69cbb.cbe7b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":1180,"wires":[]},{"id":"f5533ca7.bb5db8","type":"ui_button","z":"b8c69cbb.cbe7b","name":"btnSendPumpData","group":"d03b9dd9.59f1e8","order":2,"width":0,"height":0,"passthru":false,"label":"Send","tooltip":"Send the settings to the pump","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"json","topic":"btnSendPumpData","x":140,"y":1300,"wires":[["41e1ec7a.1bb2b4"]]},{"id":"41e1ec7a.1bb2b4","type":"function","z":"b8c69cbb.cbe7b","name":"","func":"if (msg.topic == \"pump\") {\n    context.set(\"pump\", msg.payload);\n    return;\n}\n\nif (msg.topic == \"pumpVolume\") {\n    context.set(\"pumpVolume\", msg.payload);\n    return;\n}\n\nif (msg.topic == \"btnSendPumpData\") {\n    msg.topic = \"BIOREACT-2C48\";\n    \n    msg.payload = \"{\\\"command\\\":\\\"testPump\\\",\" + \n        \"pump:\\\"\" + ddlPumps[context.get(\"pump\")] + \"\\\",\" +\n        \"value:\" + context.get(\"pumpVolume\") + \"}\";\n    \n    return msg;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":1240,"wires":[["ccbaec0e.1402b8"]]},{"id":"d03b9dd9.59f1e8","type":"ui_group","name":"Peristaltics","tab":"823eb67.b157648","disp":true,"width":"6","collapse":false},{"id":"823eb67.b157648","type":"ui_tab","name":"Tests","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

How can I manage to do that?

Try this:

[
    {
        "id": "670d50fc0819dfb5",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "55ce81f8.2b74f8",
        "type": "ui_numeric",
        "z": "670d50fc0819dfb5",
        "name": "pumpVolume",
        "label": "Volume (ml)",
        "tooltip": "Volume in ml to dispense",
        "group": "d03b9dd9.59f1e8",
        "order": 5,
        "width": 0,
        "height": 0,
        "wrap": false,
        "passthru": true,
        "topic": "pumpVolume",
        "format": "{{value}}",
        "min": 0,
        "max": "50",
        "step": "0.5",
        "x": 210,
        "y": 120,
        "wires": [
            [
                "ddd9e0902b5a40f3"
            ]
        ]
    },
    {
        "id": "4aff2f21.5760b8",
        "type": "ui_dropdown",
        "z": "670d50fc0819dfb5",
        "name": "ddlPumps",
        "label": "Pump",
        "tooltip": "Select the peristaltic pump to test",
        "place": "Select option",
        "group": "d03b9dd9.59f1e8",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "options": [
            {
                "label": "pH up",
                "value": "pumpUp",
                "type": "str"
            },
            {
                "label": "ph down",
                "value": "pumpDown",
                "type": "str"
            },
            {
                "label": "anti-foaming",
                "value": "pumpFoam",
                "type": "str"
            }
        ],
        "payload": "",
        "topic": "pump",
        "x": 200,
        "y": 180,
        "wires": [
            [
                "f9360611b7213407"
            ]
        ]
    },
    {
        "id": "ccbaec0e.1402b8",
        "type": "debug",
        "z": "670d50fc0819dfb5",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 610,
        "y": 240,
        "wires": []
    },
    {
        "id": "f5533ca7.bb5db8",
        "type": "ui_button",
        "z": "670d50fc0819dfb5",
        "name": "btnSendPumpData",
        "group": "d03b9dd9.59f1e8",
        "order": 2,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Send",
        "tooltip": "Send the settings to the pump",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "1",
        "payloadType": "json",
        "topic": "btnSendPumpData",
        "x": 230,
        "y": 240,
        "wires": [
            [
                "41e1ec7a.1bb2b4"
            ]
        ]
    },
    {
        "id": "41e1ec7a.1bb2b4",
        "type": "function",
        "z": "670d50fc0819dfb5",
        "name": "",
        "func": "var pump = flow.get(\"pump\") || \"DEFAULT VALUE\"; \nvar volume = flow.get(\"volume\") || 0;\n\nmsg.topic = \"BIOREACT-2C48\";\nmsg.payload = {\n    \"command\": \"textPump\",\n    \"pump\": pump,\n    \"value\": volume\n}\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 240,
        "wires": [
            [
                "ccbaec0e.1402b8"
            ]
        ]
    },
    {
        "id": "ddd9e0902b5a40f3",
        "type": "change",
        "z": "670d50fc0819dfb5",
        "name": "Save Value",
        "rules": [
            {
                "t": "set",
                "p": "volume",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 430,
        "y": 120,
        "wires": [
            []
        ]
    },
    {
        "id": "f9360611b7213407",
        "type": "change",
        "z": "670d50fc0819dfb5",
        "name": "Save Value",
        "rules": [
            {
                "t": "set",
                "p": "pump",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 430,
        "y": 180,
        "wires": [
            []
        ]
    },
    {
        "id": "d03b9dd9.59f1e8",
        "type": "ui_group",
        "name": "Peristaltics",
        "tab": "823eb67.b157648",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "823eb67.b157648",
        "type": "ui_tab",
        "name": "Tests",
        "icon": "dashboard",
        "order": 3,
        "disabled": false,
        "hidden": false
    }
]
1 Like

As a newbie and a C# programmer, I thank you very much! :smiley:

There should be no need for a function or context storage.

If you give the text field, dropdown and the send button the correct topic values, then a join node would be all you need, no function required
e.g.

[{"id":"55ce81f8.2b74f8","type":"ui_numeric","z":"c74669a0.6a34f8","name":"pumpVolume","label":"Volume (ml)","tooltip":"Volume in ml to dispense","group":"d03b9dd9.59f1e8","order":5,"width":0,"height":0,"wrap":false,"passthru":true,"topic":"value","topicType":"str","format":"{{value}}","min":0,"max":"50","step":"0.5","x":170,"y":2740,"wires":[["e62db490.5576d8"]]},{"id":"e62db490.5576d8","type":"join","z":"c74669a0.6a34f8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":530,"y":2880,"wires":[["ccbaec0e.1402b8"]]},{"id":"4aff2f21.5760b8","type":"ui_dropdown","z":"c74669a0.6a34f8","name":"ddlPumps","label":"Pump","tooltip":"Select the peristaltic pump to test","place":"Select option","group":"d03b9dd9.59f1e8","order":0,"width":0,"height":0,"passthru":true,"multiple":false,"options":[{"label":"pH up","value":"pumpUp","type":"str"},{"label":"ph down","value":"pumpDown","type":"str"},{"label":"anti-foaming","value":"pumpFoam","type":"str"}],"payload":"","topic":"pump","topicType":"str","x":170,"y":2800,"wires":[["e62db490.5576d8"]]},{"id":"bee408bd.60ab08","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"complete","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":2880,"wires":[["e62db490.5576d8"]]},{"id":"ccbaec0e.1402b8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":2680,"wires":[]},{"id":"f5533ca7.bb5db8","type":"ui_button","z":"c74669a0.6a34f8","name":"command","group":"d03b9dd9.59f1e8","order":2,"width":0,"height":0,"passthru":false,"label":"Send","tooltip":"Send the settings to the pump","color":"","bgcolor":"","icon":"","payload":"testPump","payloadType":"str","topic":"command","topicType":"str","x":150,"y":2880,"wires":[["bee408bd.60ab08"]]},{"id":"d03b9dd9.59f1e8","type":"ui_group","name":"Peristaltics","tab":"823eb67.b157648","order":1,"disp":true,"width":"6","collapse":false},{"id":"823eb67.b157648","type":"ui_tab","name":"Tests","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

output

{"value":1,"pump":"pumpDown","command":"testPump"}
2 Likes

Interesting, I never thought about doing it that way. I may need to look at some of my flows

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