[
{
"id": "9c1d17307d70c8e7",
"type": "tab",
"label": "Water Supply",
"disabled": false,
"info": "",
"env": []
},
{
"id": "e742fddb55d91320",
"type": "tcp request",
"z": "9c1d17307d70c8e7",
"name": "elevated_pot",
"server": "192.168.0.10",
"port": "8888",
"out": "sit",
"ret": "buffer",
"splitc": " ",
"newline": "",
"trim": false,
"tls": "",
"x": 310,
"y": 180,
"wires": [
[
"9599dd34ec985ecc"
]
]
},
{
"id": "1fd33c29198b6d97",
"type": "inject",
"z": "9c1d17307d70c8e7",
"name": "5s",
"props": [
{
"p": "payload"
}
],
"repeat": "5",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "[\"0x55\"]",
"payloadType": "bin",
"x": 110,
"y": 140,
"wires": [
[
"c3edfc8a8fabd312"
]
]
},
{
"id": "9599dd34ec985ecc",
"type": "function",
"z": "9c1d17307d70c8e7",
"name": "process, filter & assemble",
"func": "if (msg.payload.length != 2) \n// invalid reading; exit \n{\n return null;\n}\n\nvar elevated_pot = flow.get('elevated_pot');\n//node.warn(elevated_pot);\n\nvar radius = elevated_pot[9];\nvar height = msg.payload[0] * 256 + msg.payload[1];\nvar cavity = 22/7 * radius * radius * height;\n// arbitrarily reduce anomalies\nvar minmax = Math.floor(cavity / 10) * 10;\nvar volume = elevated_pot[8] - minmax;\n\nif (volume < 0) \n// below reserve\n{\n volume = 0;\n}\n\nif (volume === elevated_pot[3]) \n// no change; skip checks\n{\n // send reading to db\n var record = true; \n}\nelse if (volume === elevated_pot[2]) \n// passed secondary check\n{\n // update new reading\n flow.set('elevated_pot[3]', volume); \n\n // send reading to db\n var record = true; \n}\nelse if (volume === elevated_pot[1]) \n// passed primary check\n{\n // update secondary check\n flow.set('elevated_pot[2]', volume) \n}\nelse // failed both checks\n{\n // update primary check\n flow.set('elevated_pot[1]', volume); \n \n // clear secondary check\n flow.set('elevated_pot[2]', 0); \n}\n\nif (record) \n{\n // clear both checks\n flow.set('elevated_pot[1]', 0);\n flow.set('elevated_pot[2]', 0);\n\n // record timestamp\n flow.set('elevated_pot[4]', new Date());\n\n // send reading to db\n msg.payload = \n [\n {\n measurement: \"elevated_pot\",\n fields: \n {\n \"Liters\": volume\n },\n tags:\n {\n _sensor: \"US-100\"\n }\n }\n ];\n return msg;\n}\nelse\n{\n return null;\n}\n",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 510,
"y": 180,
"wires": [
[
"62aa0f16d23499da"
]
]
},
{
"id": "62aa0f16d23499da",
"type": "influxdb batch",
"z": "9c1d17307d70c8e7",
"influxdb": "1234567890ABCDEF",
"precision": "",
"retentionPolicy": "",
"name": "record",
"database": "database",
"precisionV18FluxV20": "ms",
"retentionPolicyV18Flux": "",
"org": "UmbrellaCorporation",
"bucket": "sensors",
"x": 690,
"y": 180,
"wires": []
},
{
"id": "53db5c1222a9636d",
"type": "function",
"z": "9c1d17307d70c8e7",
"name": "notify?",
"func": "var elevated_pot = flow.get('elevated_pot');\n\nvar old_reading = elevated_pot[5];\nvar new_reading = Math.floor(elevated_pot[3] / 100) * 100;\nvar percentage = Math.floor(new_reading / elevated_pot[8] * 100);\n\nvar last_one = elevated_pot[6];\nvar this_run = new Date();\n\nvar notify = false;\n\nif (this_run.getTime() - last_one.getTime() > 900000)\n{\n notify = true;\n}\n\nif (Math.abs(new_reading - old_reading >= elevated_pot[8]/10))\n{\n notify = true;\n} \nelse if (new_reading === old_reading)\n{\n notify = false;\n}\n\nif (notify)\n{\n // set new reading\n flow.set('elevated_pot[5]', new_reading);\n flow.set('elevated_pot[7]', percentage);\n\n // record timestamp\n flow.set('elevated_pot[6]', new Date());\n\n msg.payload =\n {\n chatId: '-1234567890ABC',\n type: 'message',\n content: '*Rooftop Tank at ' \n + percentage \n + '%* \\n_ ' \n + new_reading \n + ' liters_'\n };\n msg.payload.options =\n {\n 'parse_mode': 'MarkdownV2'\n };\n return msg;\n}\nelse \n{\n return null;\n}",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 550,
"y": 140,
"wires": [
[
"288263b022b2458e"
]
]
},
{
"id": "288263b022b2458e",
"type": "link out",
"z": "9c1d17307d70c8e7",
"name": "telegram",
"mode": "link",
"links": [
"4a52930bbe78b3e5",
"097d635a5e4929af",
"bd3bc90dde1594ea",
"73498046dd2c8752"
],
"x": 725,
"y": 140,
"wires": []
},
{
"id": "c3edfc8a8fabd312",
"type": "function",
"z": "9c1d17307d70c8e7",
"name": "init & break",
"func": "// break for 60s between successive valid readings\n\nvar last_one = flow.get('elevated_pot[4]');\nvar this_run = new Date();\n\nif (this_run.getTime() - last_one.getTime() >= 60000)\n{\n return msg;\n}\nelse \n{\n return null;\n}",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "// power-on reset\nif (flow.get('elevated_pot') === undefined) {\n flow.set('elevated_pot', [\n false,\n 0,\n 0,\n 0,\n new Date(),\n 0,\n new Date(),\n 0,\n 2000,\n 0.72\n ])\n}\n",
"finalize": "",
"libs": [],
"x": 130,
"y": 180,
"wires": [
[
"e742fddb55d91320",
"9bfbebd5d8078c65"
]
]
},
{
"id": "9bfbebd5d8078c65",
"type": "delay",
"z": "9c1d17307d70c8e7",
"name": "1x per min",
"pauseType": "rate",
"timeout": "1",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "minute",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": true,
"allowrate": false,
"outputs": 1,
"x": 310,
"y": 140,
"wires": [
[
"63edd20ca459180c",
"53db5c1222a9636d"
]
]
},
{
"id": "63edd20ca459180c",
"type": "function",
"z": "9c1d17307d70c8e7",
"name": "send out percentage",
"func": "msg.payload = flow.get('elevated_pot[7]');\nif (msg.payload === 0)\n{\n msg.payload = 10;\n}\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 500,
"y": 100,
"wires": [
[
"de92b574bda3f709"
]
]
},
{
"id": "de92b574bda3f709",
"type": "mqtt out",
"z": "9c1d17307d70c8e7",
"name": "to display",
"topic": "sensors/display/percent/elevated_pot",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "71efac7808763b18",
"x": 680,
"y": 100,
"wires": []
},
{
"id": "95a6d622911dd5bf",
"type": "influxdb",
"hostname": "192.168.0.11",
"port": "8086",
"protocol": "http",
"database": "sensors",
"name": "InfluxDB",
"usetls": false,
"tls": "",
"influxdbVersion": "2.0",
"url": "http://192.168.0.11:8086",
"rejectUnauthorized": true
},
{
"id": "71efac7808763b18",
"type": "mqtt-broker",
"name": "Mosquitto",
"broker": "192.168.0.12",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]