How to set button color on HTTP node response code?

Is there an easy method to get hold of the HTTP response status code when I use the HTTP node
to send a GET command and URL? I would like to change the color of the switch button, ONLY
if the remote relay received the command by replying with the HTTP response code 200 OK.

I have an HTTP node that controls remote relays based on specific cryptic URLs.
For example, if I click a button to turn on relay 2, my HTTP node sends;
http://192.168.1.97/setswitch.htm?ap2

Using a debug node, I can see that the entire message object does appear to include
the HTTP response code;

MESSAGE OBJECT:
_msgid: "b74a3290.708e6"
topic: ""
payload: ""
statusCode: 200 <<== OK ?
headers: object
responseUrl: "http://192.168.1.97/setswitch.htm?ap2"

Any example or description would be sincerely appreciated and educational.
I assume if there is an easy way to parse out that response status code from the HTTP send node,
then I could connect it to a function to convert the 200 or OK, to a 1 or a 0 and control the
color on a button background?

Neal

Yes - using a switch node followed by a change node... also read this to ensure you find the part you are looking for . https://nodered.org/docs/user-guide/messages

OK, I read that section on messages and it makes sense.
But I am still confused about the HTTP response status code.
My HTTP node "sends" the GET and URL. To me that sounds like output or send.
What does the "receive" for the http response code? This same HTTP node?
Is the field in my msg.payload that is called;
statusCode: 200
just indicating what my send would like to get back? Or is that field of msg.payload actually already
receiving the HTTP response message and putting it in this same node's message object?

To make use of that statusCode (200 = OK), would I just link from the output of my http node
and then reference that field as msg.payload.statusCode and then route it to the change node and switch node?

Thanks,

Neal

Yes. Your last sentences are correct

The output of the node is the reply that it gets from the other end.

Giving Back to community. With your help, I now have a 3-color button flow that does the following;

  1. Initial State: Orange color.
  2. If HTTP relay received command, button color turns Red for ON, and Blue for OFF.
  3. Upon clicking button, it turns Orange to acknowledge click.
  4. 1/2 second later (configurable), it turns Red or Blue for On or Off.
  5. If there is a remote device failure or network outage, button will remain orange, alerting
    you that there is a problem to investigate.

DISCLAIMER: I am very much a beginner, this flow can probably be condensed more.

UPDATE 16-May-2020; Thanks for the tip Lena. I have corrected the mangled posting of the code;

[
    {
        "id": "93ea5bd6.288a38",
        "type": "tab",
        "label": "ANT SWITCH",
        "disabled": false,
        "info": ""
    },
    {
        "id": "d918f4c3.efe138",
        "type": "http request",
        "z": "93ea5bd6.288a38",
        "name": "ANT2-off",
        "method": "GET",
        "ret": "txt",
        "paytoqs": false,
        "url": "http://192.168.1.28/setswitch.htm?aa2",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 440,
        "y": 400,
        "wires": [
            [
                "52a03d8e.2bccc4"
            ]
        ]
    },
    {
        "id": "a4df041c.e49998",
        "type": "debug",
        "z": "93ea5bd6.288a38",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 1250,
        "y": 320,
        "wires": []
    },
    {
        "id": "e8da1504.096d68",
        "type": "http request",
        "z": "93ea5bd6.288a38",
        "name": "ANT2-on",
        "method": "GET",
        "ret": "txt",
        "paytoqs": false,
        "url": "http://192.168.1.28/setswitch.htm?ap2",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 440,
        "y": 540,
        "wires": [
            [
                "d3ec9cf2.ac424"
            ]
        ]
    },
    {
        "id": "1f44c1ce.2ef1be",
        "type": "debug",
        "z": "93ea5bd6.288a38",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 1250,
        "y": 480,
        "wires": []
    },
    {
        "id": "895389d.89bbe78",
        "type": "change",
        "z": "93ea5bd6.288a38",
        "name": "SET to Red",
        "rules": [
            {
                "t": "set",
                "p": "colour",
                "pt": "msg",
                "to": "red",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 850,
        "y": 460,
        "wires": [
            [
                "7219beb1.663df"
            ]
        ]
    },
    {
        "id": "d3ec9cf2.ac424",
        "type": "switch",
        "z": "93ea5bd6.288a38",
        "name": "HTTP 200 OK",
        "property": "statusCode",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "200",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 660,
        "y": 460,
        "wires": [
            [
                "895389d.89bbe78"
            ]
        ]
    },
    {
        "id": "52a03d8e.2bccc4",
        "type": "switch",
        "z": "93ea5bd6.288a38",
        "name": "HTTP 200 OK",
        "property": "statusCode",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "200",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 660,
        "y": 360,
        "wires": [
            [
                "fb9c58e4.f7ac28"
            ]
        ]
    },
    {
        "id": "fb9c58e4.f7ac28",
        "type": "change",
        "z": "93ea5bd6.288a38",
        "name": "SET to Blue",
        "rules": [
            {
                "t": "set",
                "p": "colour",
                "pt": "msg",
                "to": "blue",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 850,
        "y": 360,
        "wires": [
            [
                "a3a8e08e.db249"
            ]
        ]
    },
    {
        "id": "9b890efe.6ac45",
        "type": "comment",
        "z": "93ea5bd6.288a38",
        "name": "Push Button Flow - ANT 2",
        "info": "This has two states:\nA and B.\nAll future refrence is to these two state names.\n\nREQUIRED:\nInputs:\nButton background colour for state A.  msg.colurA\nButton background colour for state B.  msg.colourB\nButton text for state A.               msg.txtA\nButton text for state B.               msg.txtB\nFont colour for state A.               msg.txtclrA\nFont colour for state B.               msg.txtclrB\nPayload for state A.                   msg.payloadA\nPayload for state B.                   msg.payloadB\nTopic.                                 msg.topicSET\n\nOutputs:\nmsg.payload - this is used to control what ever you need.\nmsg.topic - this is if it is needed for control of the next node.\nmsg.colour - this sets the colour of the button.\nmsg.txt - this is the text to be displayed in the button.\nmsg.fontclr - this is the colour of the text on the button.\n",
        "x": 210,
        "y": 60,
        "wires": []
    },
    {
        "id": "d77b428c.f31a3",
        "type": "inject",
        "z": "93ea5bd6.288a38",
        "name": "Setup - Inject",
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "3",
        "x": 190,
        "y": 130,
        "wires": [
            [
                "b8cce1e8.c44ac"
            ]
        ]
    },
    {
        "id": "b8cce1e8.c44ac",
        "type": "function",
        "z": "93ea5bd6.288a38",
        "name": "Setup",
        "func": "msg = {\n    \"colourA\": \"DarkOrange\",\n    \"colourB\": \"DarkOrange\",\n    \"txtA\": \"ANTENNA 2\",\n    \"txtB\": \"ANTENNA 2\",\n    \"txtclrA\": \"white\",\n    \"txtclrB\": \"white\",\n    \"payloadA\": \"1\",\n    \"payloadB\": \"0\",\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 160,
        "y": 230,
        "wires": [
            [
                "a456ed98.1e9b1"
            ]
        ]
    },
    {
        "id": "a456ed98.1e9b1",
        "type": "function",
        "z": "93ea5bd6.288a38",
        "name": "Push Button",
        "func": "var state = context.get(\"STATE\")||0;\n\n//  Look for a !X message to get values.\nif (msg.payload != \"X\")     //Do this if the message is NOT \"X\"\n{\n    //\n    //  Background colours first.\n    //\n    context.set(\"ABGC\", msg.colourA);\n    context.set(\"BBGC\", msg.colourB);\n    //\n    //  Now do text.\n    //\n    context.set(\"Atxt\", msg.txtA);\n    context.set(\"Btxt\", msg.txtB);\n    //\n    //  Font colours.\n    //\n    context.set(\"AFC\",msg.txtclrA);\n    context.set(\"BFC\",msg.txtclrB);\n    //\n    //  Payloads.\n    //\n    context.set(\"PayloadA\", msg.payloadA);\n    context.set(\"PayloadB\", msg.payloadB);\n    //\n    //  Topic.\n    //\n    if (msg.topicSET !== null)\n    {\n        context.set(\"Topic\",msg.topicSET);\n    } else\n    {\n        context.set(\"Topic\",\"~\");\n    }\n    return;\n}\n//      Now on to the real stuff.\nif (msg.payload == \"X\")\n{\n    state = (state + 1)% 2;\n    //node.warn(state);\n    context.set(\"STATE\",state);\n}\nif (state === 0)\n{\n    //  Condition A\n    msg.payload = context.get(\"PayloadA\");\n    msg.colour = context.get(\"ABGC\");\n    msg.txt = context.get(\"Atxt\");\n    msg.fontclr = context.get(\"AFC\");\n} else\n{\n    //  Condition B\n    msg.payload = context.get(\"PayloadB\");\n    msg.colour = context.get(\"BBGC\");\n    msg.txt = context.get(\"Btxt\");\n    msg.fontclr = context.get(\"BFC\");\n}\nif (context.get(\"Topic\") == \"~\")\n{\n    msg.topic = \"\";\n} else\n{\n    msg.topic = context.get(\"Topic\");\n}\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "x": 340,
        "y": 230,
        "wires": [
            [
                "d532eb93.7ca028",
                "35b3e652.8ae61a",
                "2aeada87.609546"
            ]
        ]
    },
    {
        "id": "d532eb93.7ca028",
        "type": "ui_button",
        "z": "93ea5bd6.288a38",
        "name": "RBE Button 2",
        "group": "2204584c.cd1948",
        "order": 5,
        "width": "0",
        "height": "0",
        "passthru": false,
        "label": "{{msg.txt}}",
        "tooltip": "",
        "color": "{{msg.fontclr}}",
        "bgcolor": "{{msg.colour}}",
        "icon": "",
        "payload": "X",
        "payloadType": "str",
        "topic": "",
        "x": 580,
        "y": 120,
        "wires": [
            [
                "a456ed98.1e9b1"
            ]
        ]
    },
    {
        "id": "35b3e652.8ae61a",
        "type": "switch",
        "z": "93ea5bd6.288a38",
        "name": "Payload 0 or 1",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 200,
        "y": 440,
        "wires": [
            [
                "d918f4c3.efe138"
            ],
            [
                "e8da1504.096d68"
            ]
        ]
    },
    {
        "id": "a3a8e08e.db249",
        "type": "delay",
        "z": "93ea5bd6.288a38",
        "name": "1/2-sec dalay",
        "pauseType": "delay",
        "timeout": "0.5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 1050,
        "y": 360,
        "wires": [
            [
                "d532eb93.7ca028",
                "a4df041c.e49998"
            ]
        ]
    },
    {
        "id": "7219beb1.663df",
        "type": "delay",
        "z": "93ea5bd6.288a38",
        "name": "1/2-sec dalay",
        "pauseType": "delay",
        "timeout": "0.5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 1050,
        "y": 460,
        "wires": [
            [
                "d532eb93.7ca028",
                "1f44c1ce.2ef1be"
            ]
        ]
    },
    {
        "id": "2aeada87.609546",
        "type": "debug",
        "z": "93ea5bd6.288a38",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "colour",
        "targetType": "msg",
        "x": 560,
        "y": 260,
        "wires": []
    },
    {
        "id": "62c89b26.4b3ac4",
        "type": "comment",
        "z": "93ea5bd6.288a38",
        "name": "ON/OFF Button, ORANGE when you push button",
        "info": "This is normally a two color button, where the SETUP node before it would define colorA as RED and colorB as BLUE, or your choice.  When you click the button they would change.\nBut I wanted two things better;  1.  Acknowledge click of the button by turning orange color, then only change color to ON (RED) or OFF (BLUE) by getting status from the remote end.  This could work with MQTT status query, but in this example I use HTTP and the HTTP response code (200 = OK) to then change the button color to on or off, red or blue.",
        "x": 680,
        "y": 80,
        "wires": []
    },
    {
        "id": "62d48693.f74ea8",
        "type": "comment",
        "z": "93ea5bd6.288a38",
        "name": "Push Button above outputs a 1 or 0",
        "info": "",
        "x": 160,
        "y": 360,
        "wires": []
    },
    {
        "id": "8a83731b.a8ada",
        "type": "comment",
        "z": "93ea5bd6.288a38",
        "name": "Send HTTP command for relay ON or OFF",
        "info": "",
        "x": 400,
        "y": 600,
        "wires": []
    },
    {
        "id": "8165eb46.840e58",
        "type": "comment",
        "z": "93ea5bd6.288a38",
        "name": "IF HTTP succeeded, change button color.",
        "info": "",
        "x": 740,
        "y": 520,
        "wires": []
    },
    {
        "id": "cff3eeb0.0583e",
        "type": "comment",
        "z": "93ea5bd6.288a38",
        "name": "After a short delay.",
        "info": "",
        "x": 1030,
        "y": 520,
        "wires": []
    },
    {
        "id": "2204584c.cd1948",
        "type": "ui_group",
        "z": "",
        "name": "Antenna Switch",
        "tab": "3bf30c50.08a7a4",
        "order": 4,
        "disp": true,
        "width": 3,
        "collapse": false
    },
    {
        "id": "3bf30c50.08a7a4",
        "type": "ui_tab",
        "z": "",
        "name": "EXAMPLE STATION DASHBAORD",
        "icon": "dashboard",
        "order": 3,
        "disabled": false,
        "hidden": false
    }
]

The above posting should be correct now.

Cheers,

Neal

Next Question: Can the LINK-IN and LINK-OUT node be used on the same flow TAB?
If I want to duplicate 6 of the above "Buttons" on one flow, and if I want to make the logic so that
only ONE of the SIX buttons can be on (the other 5 turn off if you push one ON), then I need to
connect the ON node from one button flow to the OFF nodes for all the other 5. Then I need to
repeat for button #2 flow, and connect it's ON node to all other 5 OFF nodes, etc, etc.
Soon that is going to look like a hair ball. Is the LINK-IN and LINK-OUT node useful here, or is
there another way to keep it looking a little more understandable?

Cheers,

Neal

Yes you can

Sorry to bug you like this, but can you edit your post and put your flow in again following the flow including guidelines? The forum software automatically replaces [] in the flow with , making it not importable when selected/copied. It also adds a quick copy button to the format that makes it easy to copy it :slight_smile:

:slight_smile: Don't be sorry, don't apologize. I am the newbie who posted it wrong :slight_smile:

I went back and reposted it correctly. Let me know if you are able to import it.

Cheers,

Neal

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