Skip to content

Instantly share code, notes, and snippets.

@Rudd-O
Created November 8, 2022 13:18
Show Gist options
  • Save Rudd-O/e347d30fe4d1baf9eaf9c01338b3dc1c to your computer and use it in GitHub Desktop.
Save Rudd-O/e347d30fe4d1baf9eaf9c01338b3dc1c to your computer and use it in GitHub Desktop.
Wasp mode in Node-RED
[
{
"id": "6f299a411f95a45d",
"type": "subflow",
"name": "Wasp lights",
"info": "",
"category": "",
"in": [
{
"x": 40,
"y": 100,
"wires": [
{
"id": "b4926cd3e069be3d"
}
]
}
],
"out": [
{
"x": 2110,
"y": 420,
"wires": [
{
"id": "de95227372bea674",
"port": 0
}
]
}
],
"env": [
{
"name": "delay",
"type": "num",
"value": "30"
}
],
"meta": {},
"color": "#DDAA99",
"inputLabels": [
"entity payload"
],
"outputLabels": [
"room status"
],
"status": {
"x": 2060,
"y": 500,
"wires": [
{
"id": "73c9f72c1ae17ecc",
"port": 0
}
]
}
},
{
"id": "adb8772dcea10f40",
"type": "junction",
"z": "6f299a411f95a45d",
"x": 1640,
"y": 180,
"wires": [
[
"999bd49459ba3632"
]
]
},
{
"id": "73c9f72c1ae17ecc",
"type": "status",
"z": "6f299a411f95a45d",
"name": "",
"scope": [
"28e5cf65d5a3a4db",
"418ff3bb54fad782",
"cf7d63dcbde7088d",
"9eb6127d2a609002",
"d8a85e0c6fdd85d0",
"b20d9f730b2285dc",
"cd556864d92145dc"
],
"x": 1600,
"y": 500,
"wires": [
[
"de95227372bea674"
]
]
},
{
"id": "cb533c2f2ed44742",
"type": "function",
"z": "6f299a411f95a45d",
"name": "openings",
"func": "var entities = flow.get(\"openings\");\nif (entities === undefined) {\n entities = {};\n flow.set(\"openings\", entities);\n}\n\nentities[msg.topic] = msg.payload;\n\nvar stuff = [];\n\nvar count = 0;\nfor (var entity in entities) {\n if (entities[entity] == \"on\") {\n stuff.push(entity);\n count = count + 1;\n }\n}\n\nflow.set(\"openings_open\", stuff)\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 360,
"y": 60,
"wires": [
[
"972eadcc65789454"
]
],
"outputLabels": [
"at least one on",
"none on"
]
},
{
"id": "fbd75eaeb0ab28b6",
"type": "function",
"z": "6f299a411f95a45d",
"name": "occupancies",
"func": "var entities = flow.get(\"occupancies\");\nif (entities === undefined) {\n entities = {};\n flow.set(\"occupancies\", entities);\n}\n\nentities[msg.topic] = msg.payload;\n\nvar stuff = [];\n\nvar count = 0;\nfor (var entity in entities) {\n if (entities[entity] == \"on\") {\n stuff.push(entity);\n count = count + 1;\n }\n}\n\nflow.set(\"occupancies_detected\", stuff);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 370,
"y": 140,
"wires": [
[
"972eadcc65789454"
]
],
"outputLabels": [
"at least one on",
"none on"
]
},
{
"id": "b4926cd3e069be3d",
"type": "switch",
"z": "6f299a411f95a45d",
"name": "",
"property": "topic",
"propertyType": "msg",
"rules": [
{
"t": "cont",
"v": "opening",
"vt": "str"
},
{
"t": "jsonata_exp",
"v": "$contains($.topic, \"motion\") or $contains($.topic, \"occupancy\")",
"vt": "jsonata"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 190,
"y": 100,
"wires": [
[
"cb533c2f2ed44742"
],
[
"fbd75eaeb0ab28b6"
]
],
"outputLabels": [
"is opening",
"is occupancy"
]
},
{
"id": "28e5cf65d5a3a4db",
"type": "function",
"z": "6f299a411f95a45d",
"name": "turned on due to door open",
"func": "node.status({ \"text\": \"on due to door open\", \"fill\": \"green\" });\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1180,
"y": 60,
"wires": [
[
"adb8772dcea10f40"
]
]
},
{
"id": "418ff3bb54fad782",
"type": "function",
"z": "6f299a411f95a45d",
"name": "turned off due to no occupancy with door open",
"func": "node.status({ \"text\": \"off due to no occupancy with door open\", \"fill\": \"yellow\" });\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1600,
"y": 780,
"wires": [
[]
]
},
{
"id": "972eadcc65789454",
"type": "function",
"z": "6f299a411f95a45d",
"name": "consolidate",
"func": "var openings_open = flow.get(\"openings_open\");\nif (openings_open === undefined || openings_open === null) {\n openings_open = [];\n flow.set(\"openings_open\", openings_open);\n}\nvar occupancies_detected = flow.get(\"occupancies_detected\");\nif (occupancies_detected === undefined || occupancies_detected === null) {\n occupancies_detected = [];\n flow.set(\"occupancies_detected\", occupancies_detected);\n}\n\nflow.set(\"report\", \"\" + openings_open.length + \" openings, \" + occupancies_detected.length + \" occupancies\")\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 570,
"y": 100,
"wires": [
[
"9bca5282057e33d7"
]
]
},
{
"id": "2cf64075b9a33894",
"type": "trigger",
"z": "6f299a411f95a45d",
"name": "wait $delay",
"op1": "",
"op2": "",
"op1type": "nul",
"op2type": "pay",
"duration": "5",
"extend": true,
"overrideDelay": true,
"units": "s",
"reset": "",
"bytopic": "all",
"topic": "topic",
"outputs": 1,
"x": 2030,
"y": 180,
"wires": [
[
"01a6bb4e6b0887ce"
]
]
},
{
"id": "01a6bb4e6b0887ce",
"type": "function",
"z": "6f299a411f95a45d",
"name": "director",
"func": "var occ = flow.get(\"occupancies_detected\");\nif (occ === null || occ === undefined) {\n occ = [];\n}\nvar opo = flow.get(\"openings_open\");\nif (opo === null || opo === undefined) {\n opo = [];\n}\nif (occ.length > 0) {\n if (opo.length > 0) { return [msg, null, null, null]; }\n return [null, msg, null, null];\n} else {\n if (opo.length > 0) { return [null, null, msg, null]; }\n return [null, null, null, msg];\n}",
"outputs": 4,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 800,
"y": 800,
"wires": [
[
"cf7d63dcbde7088d"
],
[
"9eb6127d2a609002"
],
[
"418ff3bb54fad782"
],
[
"d8a85e0c6fdd85d0"
]
],
"outputLabels": [
"occupancies > 0 openings > 0",
"occupancies > 0 openings == 0",
"occupancies == 0 openings > 0",
"occupancies == 0 openings == 0"
]
},
{
"id": "cf7d63dcbde7088d",
"type": "function",
"z": "6f299a411f95a45d",
"name": "extended due to continued occupancy",
"func": "node.status({ \"text\": \"on due to continued occupancy\", \"fill\": \"green\" });\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1210,
"y": 300,
"wires": [
[
"adb8772dcea10f40"
]
]
},
{
"id": "9eb6127d2a609002",
"type": "function",
"z": "6f299a411f95a45d",
"name": "extended due to wasp mode",
"func": "node.status({ \"text\": \"on due to wasp mode\", \"fill\": \"green\" });\nmsg.reset = true;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1180,
"y": 380,
"wires": [
[
"adb8772dcea10f40"
]
]
},
{
"id": "d8a85e0c6fdd85d0",
"type": "function",
"z": "6f299a411f95a45d",
"name": "turned off due to no occupancy with door closed",
"func": "node.status({ \"text\": \"off due to no occupancy with door closed\", \"fill\": \"yellow\" });\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1600,
"y": 860,
"wires": [
[]
]
},
{
"id": "b20d9f730b2285dc",
"type": "function",
"z": "6f299a411f95a45d",
"name": "turned on due to occupancy detected",
"func": "node.status({ \"text\": \"on due to occupancy detected\", \"fill\": \"green\" });\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1210,
"y": 140,
"wires": [
[
"adb8772dcea10f40"
]
]
},
{
"id": "9bca5282057e33d7",
"type": "function",
"z": "6f299a411f95a45d",
"name": "director initial",
"func": "var occ = flow.get(\"occupancies_detected\");\nif (occ === null || occ === undefined) {\n occ = [];\n}\nvar opo = flow.get(\"openings_open\");\nif (opo === null || opo === undefined) {\n opo = [];\n}\nvar last_occ = flow.get(\"last_occupancies_detected\");\nif (last_occ === null || last_occ === undefined) {\n last_occ = [];\n}\nvar last_opo = flow.get(\"last_openings_open\");\nif (last_opo === null || last_opo === undefined) {\n last_opo = [];\n}\nflow.set(\"last_occupancies_detected\", occ);\nflow.set(\"last_openings_open\", opo);\n\nif (opo.length > last_opo.length) {\n return [msg, null, null, null];\n} else if (occ.length > last_occ.length) {\n if (opo.length > 0) {\n return [null, msg, null, null];\n } else {\n return [null, null, msg, null];\n }\n} else {\n return [null, null, null, msg];\n}",
"outputs": 4,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 750,
"y": 100,
"wires": [
[
"28e5cf65d5a3a4db"
],
[
"b20d9f730b2285dc"
],
[
"9eb6127d2a609002"
],
[
"cd556864d92145dc"
]
],
"outputLabels": [
"openings increased",
"occupancy increased with door open",
"occupancy increased with door closed",
"else"
]
},
{
"id": "999bd49459ba3632",
"type": "change",
"z": "6f299a411f95a45d",
"name": "delay $delay",
"rules": [
{
"t": "set",
"p": "delay",
"pt": "msg",
"to": "$env(\"delay\") * 1000",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1790,
"y": 180,
"wires": [
[
"2cf64075b9a33894"
]
]
},
{
"id": "de95227372bea674",
"type": "function",
"z": "6f299a411f95a45d",
"name": "status output",
"func": "msg.payload = msg.status.text;\nflow.set(\"last_state\", msg.payload);\nvar m = flow.get(\"openings_open\");\nif (m !== null && m !== undefined) {\n msg.openings = m;\n}\nvar n = flow.get(\"occupancies_detected\");\nif (n !== null && n !== undefined) {\n msg.occupancies = n;\n}\ndelete msg.status;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1910,
"y": 420,
"wires": [
[]
]
},
{
"id": "cd556864d92145dc",
"type": "function",
"z": "6f299a411f95a45d",
"name": "update changes",
"func": "var opo = flow.get(\"openings_open\");\nvar occ = flow.get(\"occupancies_detected\");\nvar last = flow.get(\"last_state\");\nif (last === undefined || last === null) { return; }\nvar newtext = last.split(\" \")[0] + \" with \" + opo.length + \" openings and \" + occ.length + \" occupancies\";\nif (newtext != last) {\n node.status({ \"text\": newtext, fill: \"blue\"});\n}\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1140,
"y": 220,
"wires": [
[]
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment