Different IP in HTTP POST request based on variable value

I am a complete newbie using Node-RED but have been working on what seemed a rather simple project for a couple of weeks now, and I am stuck. I am trying to monitor the status of a series of variables across multiple ESP32-based devices using HTTP GET. I also want to be able to send values to all or specific devices using HTTP POST. So far, the GET part is working perfectly for me, I can read the status of the different devices. I am stuck with the POST part. This is what I have:

I have two text inputs, each one accepts a number that is later combined in a single payload as the variables I want to post: "source" and "code". A payload looks something like this:

{"source":30,"code":62005}

The source value indicates to what device the post will go, say source 10, will send the code 3 to a device located in IP 192.168.0.10. What I cant seem to find is a way to change the IP address in the HTTP Request node so it is based on the source value I entered. Any ideas?

This is my flow (hopefully I am sharing the code correctly):

[
{
    "id": "dbbd9ac25d0245b7",
    "type": "change",
    "z": "af973086eb22abf7",
    "name": "",
    "rules": [
        {
            "t": "set",
            "p": "temp",
            "pt": "msg",
            "to": "payload",
            "tot": "msg"
        },
        {
            "t": "set",
            "p": "payload",
            "pt": "msg",
            "to": "{}",
            "tot": "json"
        },
        {
            "t": "set",
            "p": "payload.source",
            "pt": "msg",
            "to": "replySource",
            "tot": "flow"
        },
        {
            "t": "set",
            "p": "payload.code",
            "pt": "msg",
            "to": "replyCode",
            "tot": "flow"
        }
    ],
    "action": "",
    "property": "",
    "from": "",
    "to": "",
    "reg": false,
    "x": 720,
    "y": 100,
    "wires": [
        [
            "140f5851a4127af4"
        ]
    ]
},
{
    "id": "8a53079e89f84ec9",
    "type": "debug",
    "z": "af973086eb22abf7",
    "name": "",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "statusVal": "",
    "statusType": "auto",
    "x": 1150,
    "y": 40,
    "wires": []
},
{
    "id": "b3864866e9cd631c",
    "type": "ui_text_input",
    "z": "af973086eb22abf7",
    "name": "replySource",
    "label": "Source",
    "tooltip": "",
    "group": "dfc9abd922154e9a",
    "order": 3,
    "width": 0,
    "height": 0,
    "passthru": true,
    "mode": "number",
    "delay": "0",
    "topic": "",
    "sendOnBlur": true,
    "className": "",
    "topicType": "str",
    "x": 230,
    "y": 60,
    "wires": [
        [
            "ebdfa45c02f8ea4a"
        ]
    ]
},
{
    "id": "36d4bdfbad777b18",
    "type": "ui_text_input",
    "z": "af973086eb22abf7",
    "name": "replyCode",
    "label": "Code",
    "tooltip": "",
    "group": "dfc9abd922154e9a",
    "order": 2,
    "width": 0,
    "height": 0,
    "passthru": true,
    "mode": "number",
    "delay": "0",
    "topic": "",
    "sendOnBlur": true,
    "className": "",
    "topicType": "str",
    "x": 230,
    "y": 160,
    "wires": [
        [
            "149b45117cbb284e"
        ]
    ]
},
{
    "id": "140f5851a4127af4",
    "type": "ui_button",
    "z": "af973086eb22abf7",
    "name": "btnSend",
    "group": "dfc9abd922154e9a",
    "order": 4,
    "width": 0,
    "height": 0,
    "passthru": false,
    "label": "Send",
    "tooltip": "",
    "color": "",
    "bgcolor": "",
    "className": "",
    "icon": "",
    "payload": "",
    "payloadType": "json",
    "topic": "payload",
    "topicType": "msg",
    "x": 920,
    "y": 100,
    "wires": [
        [
            "eea42f6c187ac0fc"
        ]
    ]
},
{
    "id": "149b45117cbb284e",
    "type": "change",
    "z": "af973086eb22abf7",
    "name": "",
    "rules": [
        {
            "t": "set",
            "p": "replyCode",
            "pt": "flow",
            "to": "payload",
            "tot": "msg"
        }
    ],
    "action": "",
    "property": "",
    "from": "",
    "to": "",
    "reg": false,
    "x": 450,
    "y": 160,
    "wires": [
        [
            "dbbd9ac25d0245b7"
        ]
    ]
},
{
    "id": "ebdfa45c02f8ea4a",
    "type": "change",
    "z": "af973086eb22abf7",
    "name": "",
    "rules": [
        {
            "t": "set",
            "p": "replySource",
            "pt": "flow",
            "to": "payload",
            "tot": "msg"
        }
    ],
    "action": "",
    "property": "",
    "from": "",
    "to": "",
    "reg": false,
    "x": 460,
    "y": 60,
    "wires": [
        [
            "dbbd9ac25d0245b7"
        ]
    ]
},
{
    "id": "eea42f6c187ac0fc",
    "type": "http request",
    "z": "af973086eb22abf7",
    "name": "",
    "method": "POST",
    "ret": "txt",
    "paytoqs": "ignore",
    "url": "192.168.0.157/reply",
    "tls": "",
    "persist": false,
    "proxy": "",
    "authType": "",
    "senderr": false,
    "credentials": {},
    "x": 1150,
    "y": 100,
    "wires": [
        []
    ]
},
{
    "id": "dfc9abd922154e9a",
    "type": "ui_group",
    "name": "InputCommand",
    "tab": "e562a4a.39a9e58",
    "order": 4,
    "disp": true,
    "width": "6",
    "collapse": false,
    "className": ""
},
{
    "id": "e562a4a.39a9e58",
    "type": "ui_tab",
    "name": "Non-myth of the Noble Red's Dashboard",
    "icon": "dashboard",
    "order": 1,
    "disabled": false,
    "hidden": false
}

There are 2 ways...

  1. The HTTP request node accepts msg.url (you could build a URL in a change or function node)
  2. The URL field accepts mustache formatted values e.g. {{ip}}/path

As documented in the built in help

So simple.. thank you so much @Steve-Mcl for the quick reply. I will give it a try.

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