I'm new here so I apologize in advance if I don't pose this question correctly or don't post the right stuff.
I have an iot app (rtl_433) feeding mqtt. My goal is to create a web page that will display the latest readings from 2 temperature sensors.
I am doing this by updating several flow variables every time an mqtt message arrives and then reading all the variables on a web request,
it almost works.
This part creates and updates the variables:
[
{
"id": "4c373b36d685d664",
"type": "mqtt in",
"z": "800755e5a3a92a74",
"name": "rtl433",
"topic": "rtl_433",
"qos": "0",
"datatype": "auto-detect",
"broker": "09e0e9cb5c03de03",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 70,
"y": 20,
"wires": [
[
"8e04b49a0199527a"
]
]
},
{
"id": "8e04b49a0199527a",
"type": "json",
"z": "800755e5a3a92a74",
"name": "",
"property": "payload",
"action": "obj",
"pretty": true,
"x": 110,
"y": 100,
"wires": [
[
"4de57660ad2e7bb4",
"24c115a6.3b06d2"
]
]
},
{
"id": "4de57660ad2e7bb4",
"type": "debug",
"z": "800755e5a3a92a74",
"name": "message trace",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 460,
"y": 100,
"wires": []
},
{
"id": "24c115a6.3b06d2",
"type": "function",
"z": "800755e5a3a92a74",
"name": "Set Context Variables",
"func": "var outdoor = flow.get('outdoor') || 0;\n//if (typeof outdoor==\"undefined\") outdoor = 0;\nvar indoor = flow.get('indoor') || 0;\n//if (typeof indoor==\"undefined\") indoor = 0;\nvar outdoor_bat = flow.get('outdoor_bat') || 0;\n//if (typeof outdoor_bat==\"undefined\") outdoor_bat = 0;\nvar indoor_bat = flow.get('indoor_bat') || 0;\n//if (typeof outdoor_bat==\"undefined\") outdoor_bat = 0;\n\nvar channel = Number(msg.payload.channel); // convert it to a number in case it is a string\nvar temperature = Number(msg.payload.temperature_F); // convert it to a number in case it is a string msg.payload = {\"channel is \" : channel , \"temp is \" : temperature};\nvar battery = Number(msg.payload.battery_ok);\nif (channel == 1) {\n //outdoor = temperature\n flow.set('outdoor', temperature)\n flow.set('outdoor_bat', battery)\n msg.payload = {\n \"outdoor temp is \": flow.get('outdoor'),\n \"outdoor battery is \": + flow.get('outdoor_bat')\n };\n}\nelse {\n //indoor = temperature\n flow.set('indoor', temperature)\n flow.set('indoor_bat', battery)\n msg.payload = {\n \"indoor temp is \": flow.get('indoor'),\n \"indoor battery is \": + flow.get('indoor_bat')\n };\n}\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 280,
"y": 180,
"wires": [
[
"1051c8a369344a09"
]
]
},
{
"id": "1051c8a369344a09",
"type": "debug",
"z": "800755e5a3a92a74",
"name": "debug 1",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 480,
"y": 180,
"wires": []
},
{
"id": "09e0e9cb5c03de03",
"type": "mqtt-broker",
"name": "rtl433",
"broker": "192.168.1.108",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]
The other part works except it says that indoor_bat is undefined.
[
{
"id": "ecbb4afc2f0a2d5a",
"type": "http in",
"z": "800755e5a3a92a74",
"name": "",
"url": "/mysite",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 90,
"y": 320,
"wires": [
[
"930e5bee17edd3b4"
]
]
},
{
"id": "930e5bee17edd3b4",
"type": "function",
"z": "800755e5a3a92a74",
"name": "msg.url = \"mysitepost\";",
"func": "//msg.outdodor = flow.get('outdoor');\n//msg.outdodor_bat = flow.get('outdoor_bat');\nmsg.indoor = flow.get('indoor');\nmsg.indoor_bat = flow.get('indodor_bat');\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 330,
"y": 320,
"wires": [
[
"f401178b48986e0a",
"91fdaec28a2d5846"
]
]
},
{
"id": "76c9419f34710a07",
"type": "template",
"z": "800755e5a3a92a74",
"name": "CSS",
"field": "payload.style",
"fieldType": "msg",
"format": "html",
"syntax": "mustache",
"template": "input[type=text], select {\n width: 100%;\n padding: 12px 20px;\n margin: 8px 0;\n display: inline-block;\n border: 1px solid #ccc;\n border-radius: 4px;\n box-sizing: border-box;\n}\n\ninput[type=submit] {\n width: 100%;\n background-color: #4CAF50;\n color: white;\n padding: 14px 20px;\n margin: 8px 0;\n border: none;\n border-radius: 4px;\n cursor: pointer;\n}\n\ninput[type=submit]:hover {\n background-color: #45a049;\n}\n\ndiv {\n border-radius: 5px;\n background-color: #f2f2f2;\n padding: 20px;\n}\n\nhtml {\n font - family: 'Open Sans', sans-serif display: block margin: 0px auto text-align: centercolor: #444444\n}\n\nbody {\n margin: 0px\n}\n\nh1 {\n margin: 25px auto 15px;\n text-align: center;\n}\n\nh3 {\n text-align: center;\n}\n\n.side-by-side {\n display: table - cellvertical - align: middleposition: relative\n}\n\n.text {\n font - weight: 600font-size: 19pxwidth: 200px\n}\n\n.reading {\n font - weight: 300font-size: 50pxpadding-right: 25px\n}\n\n.temperature .reading {\ncolor: #F29C1F\n}\n\n.humidity .reading {\n color: #3B97D3\n}\n\n.pressure .reading {\n color: #26B99A\n}\n\n.altitude .reading {\n color: #955BA5\n}\n\n.superscript {\n font - size: 17pxfont-weight: 600position: absolutetop: 10px\n}\n\n.data {\n padding: 10px\n}\n\n.container {\n display: tablemargin: 0 auto\n}\n\n.icon {\n width: 65px\n}\n\n",
"x": 490,
"y": 420,
"wires": [
[
"c8f17365680d23fc"
]
]
},
{
"id": "f401178b48986e0a",
"type": "template",
"z": "800755e5a3a92a74",
"name": "JavaScript",
"field": "payload.script",
"fieldType": "msg",
"format": "javascript",
"syntax": "plain",
"template": "$(document).ready(function(e) {\n \n $(\"form[ajax=true]\").submit(function(e) {\n \n e.preventDefault();\n \n var form_data = $(this).serialize();\n var form_url = $(this).attr(\"action\");\n var form_method = $(this).attr(\"method\").toUpperCase();\n \n $(\"#loadingimg\").show();\n \n $.ajax({\n url: form_url, \n type: form_method, \n data: form_data, \n cache: false,\n success: function(returnhtml){ \n $(\"#result\").html(returnhtml); \n $(\"#loadingimg\").hide(); \n } \n }); \n \n });\n \n});",
"x": 330,
"y": 420,
"wires": [
[
"76c9419f34710a07"
]
]
},
{
"id": "454fc4dacfaa7a48",
"type": "http response",
"z": "800755e5a3a92a74",
"name": "",
"x": 770,
"y": 420,
"wires": []
},
{
"id": "c8f17365680d23fc",
"type": "template",
"z": "800755e5a3a92a74",
"name": "HTML",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "<html>\n<head>\n <title>Oregon and ESP8266 Weather Station</title>\n <meta name=\"viewport\" content=\"initial-scale=1.0, width=device-width\">\n <meta charset=\"utf-8\">\n <meta http-equiv='refresh' content='30'>\n <script src=\"http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js\">\n </script>\n <style>\n{{{payload.style}}}\n </style>\n</head>\n<h1>Oregon and ESP8266 Weather Station</h2>\n<h3>Location: Monrovia, CA (alt 705 feet)</h3>\n\n<div class='container'>\n <div class='data temperature'>\n <div class='side-by-side icon'>\n <svg enable-background='new 0 0 19.438 54.003' height=54.003px id=Layer_1 version=1.1\n viewBox='0 0 19.438 54.003' width=19.438px x=0px xml:space=preserve xmlns=http:>/www.w3.org/2000/svg\n xmlns:xlink=http://www.w3.org/1999/xlink y=0px>\n <g>\n <path d='M11.976,8.82v-2h4.084V6.063C16.06,2.715,13.345,0,9.996,0H9.313C5.965,0,3.252,2.715,3.252,6.063v30.982\nC1.261,38.825,0,41.403,0,44.286c0,5.367,4.351,9.718,9.719,9.718c5.368,0,9.719-4.351,9.719-9.718\nc0-2.943-1.312-5.574-3.378-7.355V18.436h-3.914v-2h3.914v-2.808h-4.084v-2h4.084V8.82H11.976z M15.302,44.833\nc0,3.083-2.5,5.583-5.583,5.583s-5.583-2.5-5.583-5.583c0-2.279,1.368-4.236,3.326-5.104V24.257C7.462,23.01,8.472,22,9.719,22\ns2.257,1.01,2.257,2.257V39.73C13.934,40.597,15.302,42.554,15.302,44.833z' fill=#F29C21/></g></svg>\n </div>\n <div class='side-by-side text'>\n Temperature\n </div>\n <div class='side-by-side reading'>\n<!-- global.get('outdoor') -->\n {{#payload}}\n \n {{/payload}}\n < span class='superscript'>°F</span>\n </div>\n </div>\n<!-- <div class='data pressure'> -->\n<!-- <div class='side-by-side icon'> -->\n<!-- <svg enable-background='new 0 0 40.542 40.541' height=40.541px id=Layer_1 version=1.1 -->\n<!-- viewBox='0 0 40.542 40.541' width=40.542px x=0px xml:space=preserve xmlns=http:>/www.w3.org/2000/svg -->\n<!-- xmlns:xlink=http://www.w3.org/1999/xlink y=0px> -->\n<!-- <g> -->\n<!-- <path d='M34.313,20.271c0-0.552,0.447-1,1-1h5.178c-0.236-4.841-2.163-9.228-5.214-12.593l-3.425,3.424 -->\n<!--c-0.195,0.195-0.451,0.293-0.707,0.293s-0.512-0.098-0.707-0.293c-0.391-0.391-0.391-1.023,0-1.414l3.425-3.424 -->\n<!--c-3.375-3.059-7.776-4.987-12.634-5.215c0.015,0.067,0.041,0.13,0.041,0.202v4.687c0,0.552-0.447,1-1,1s-1-0.448-1-1V0.25 -->\n<!--c0-0.071,0.026-0.134,0.041-0.202C14.39,0.279,9.936,2.256,6.544,5.385l3.576,3.577c0.391,0.391,0.391,1.024,0,1.414 -->\n<!--c-0.195,0.195-0.451,0.293-0.707,0.293s-0.512-0.098-0.707-0.293L5.142,6.812c-2.98,3.348-4.858,7.682-5.092,12.459h4.804 -->\n<!--c0.552,0,1,0.448,1,1s-0.448,1-1,1H0.05c0.525,10.728,9.362,19.271,20.22,19.271c10.857,0,19.696-8.543,20.22-19.271h-5.178 -->\n<!--C34.76,21.271,34.313,20.823,34.313,20.271z M23.084,22.037c-0.559,1.561-2.274,2.372-3.833,1.814 -->\n<!--c-1.561-0.557-2.373-2.272-1.815-3.833c0.372-1.041,1.263-1.737,2.277-1.928L25.2,7.202L22.497,19.05 -->\n<!--C23.196,19.843,23.464,20.973,23.084,22.037z' fill=#26B999/></g></svg> -->\n<!-- </div> -->\n<!-- <div class='side-by-side text'> -->\n<!-- Pressure -->\n<!-- </div> -->\n<!-- <div class='side-by-side reading'> -->\n<!-- (int)pressure -->\n<!-- <span class='superscript'>hPa</span> -->\n<!-- </div> -->\n<!-- </div> -->\n<!-- -->\n <div>\n <form method=\"post\" action=\"/{{url}}\" ajax=\"true\">\n <label for=\"fname\">First Name</label>\n <input type=\"text\" id=\"fname\" name=\"firstname\">\n\n <label for=\"lname\">Last Name</label>\n <input type=\"text\" id=\"lname\" name=\"lastname\">\n\n <label for=\"country\">Country</label>\n <select id=\"country\" name=\"country\">\n <option value=\"uk\">United Kingdom</option>\n <option value=\"canada\">Canada</option>\n <option value=\"usa\">USA</option>\n </select>\n\n <input type=\"submit\" value=\"Submit\">\n </form>\n </div>\n <div>\n <span id=\"result\"></span>\n </div>\n\n </body>\n</html>\n<script>\n { { { payload.script } } }\n</script>\n\n",
"x": 630,
"y": 420,
"wires": [
[
"454fc4dacfaa7a48"
]
]
},
{
"id": "f11b2d67bb30c7d7",
"type": "comment",
"z": "800755e5a3a92a74",
"name": "Login Form",
"info": "",
"x": 290,
"y": 380,
"wires": []
},
{
"id": "77a000a0a2ac2e4f",
"type": "comment",
"z": "800755e5a3a92a74",
"name": "Inject msg object properties",
"info": "",
"x": 340,
"y": 280,
"wires": []
},
{
"id": "f62e63adabd04bec",
"type": "comment",
"z": "800755e5a3a92a74",
"name": "Website",
"info": "",
"x": 80,
"y": 280,
"wires": []
},
{
"id": "91fdaec28a2d5846",
"type": "debug",
"z": "800755e5a3a92a74",
"name": "debug 4",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 620,
"y": 320,
"wires": []
}
]
I am completely baffled as yo why 3 of the 4 variables work as expected while indoor_bat is undefined when I try to access.
Help will be greatly appreciated.
Thank you