Giving Back to community. With your help, I now have a 3-color button flow that does the following;
- Initial State: Orange color.
- If HTTP relay received command, button color turns Red for ON, and Blue for OFF.
- Upon clicking button, it turns Orange to acknowledge click.
- 1/2 second later (configurable), it turns Red or Blue for On or Off.
- 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