Huge rework of Main Scenes Handler #3

Fixed logic where it attempted to notify a TV even in rooms with no TV.

Cut down on a LOT of nodes/connections using async msg sending.

Single node for setting nightlight vs not.
This commit is contained in:
2023-08-20 13:50:55 -04:00
parent 31aab348c2
commit 1a6608c40d

View File

@ -310,11 +310,11 @@
], ],
"out": [ "out": [
{ {
"x": 720, "x": 520,
"y": 40, "y": 40,
"wires": [ "wires": [
{ {
"id": "011c7df420cc7f1a", "id": "78a60e2da212f6aa",
"port": 0 "port": 0
} }
] ]
@ -483,16 +483,16 @@
"color": "#DDAA99", "color": "#DDAA99",
"icon": "node-red-contrib-huemagic/hue-scene.svg", "icon": "node-red-contrib-huemagic/hue-scene.svg",
"status": { "status": {
"x": 1560, "x": 680,
"y": 500, "y": 360,
"wires": [ "wires": [
{ {
"id": "6ae79d1701e78840", "id": "4ab49b8485f1886f",
"port": 0 "port": 3
}, },
{ {
"id": "defe84d113029e8e", "id": "9af9a2966a477b33",
"port": 0 "port": 3
} }
] ]
} }
@ -700,7 +700,9 @@
"37f1c11c59a6ce16" "37f1c11c59a6ce16"
], ],
"x": 94, "x": 94,
"y": 339 "y": 339,
"w": 1872,
"h": 622
}, },
{ {
"id": "39a7ac02f2727f1d", "id": "39a7ac02f2727f1d",
@ -864,7 +866,9 @@
"51b11f9d5b8bfbb0" "51b11f9d5b8bfbb0"
], ],
"x": 34, "x": 34,
"y": 179 "y": 179,
"w": 1292,
"h": 382
}, },
{ {
"id": "477e752fcd2e2a19", "id": "477e752fcd2e2a19",
@ -952,7 +956,9 @@
"febc179b34c8cf71" "febc179b34c8cf71"
], ],
"x": 34, "x": 34,
"y": 39 "y": 39,
"w": 692,
"h": 682
}, },
{ {
"id": "46275dd0f9461013", "id": "46275dd0f9461013",
@ -970,7 +976,9 @@
"ca88f85ba00a6744" "ca88f85ba00a6744"
], ],
"x": 34, "x": 34,
"y": 739 "y": 739,
"w": 472,
"h": 202
}, },
{ {
"id": "ff6d8150e8d5e606", "id": "ff6d8150e8d5e606",
@ -1033,7 +1041,9 @@
"c0933fcc8737bc0b" "c0933fcc8737bc0b"
], ],
"x": 34, "x": 34,
"y": 239 "y": 239,
"w": 1032,
"h": 322
}, },
{ {
"id": "1ca205a84922c8c2", "id": "1ca205a84922c8c2",
@ -1171,7 +1181,9 @@
"b281061a4b08d2eb" "b281061a4b08d2eb"
], ],
"x": 34, "x": 34,
"y": 759 "y": 759,
"w": 612,
"h": 202
}, },
{ {
"id": "550e5b2d99316b12", "id": "550e5b2d99316b12",
@ -1196,7 +1208,9 @@
"2516c6e3146cc851" "2516c6e3146cc851"
], ],
"x": 34, "x": 34,
"y": 619 "y": 619,
"w": 1112,
"h": 282
}, },
{ {
"id": "f519aff597ece351", "id": "f519aff597ece351",
@ -1315,7 +1329,9 @@
"51d05298561cfddb" "51d05298561cfddb"
], ],
"x": 754, "x": 754,
"y": 39 "y": 39,
"w": 672,
"h": 262
}, },
{ {
"id": "b708cb6d93f40dee", "id": "b708cb6d93f40dee",
@ -1348,7 +1364,9 @@
"a8d7f5e5d4f549b0" "a8d7f5e5d4f549b0"
], ],
"x": 34, "x": 34,
"y": 579 "y": 579,
"w": 732,
"h": 162
}, },
{ {
"id": "4886967d0f8c0156", "id": "4886967d0f8c0156",
@ -1389,7 +1407,9 @@
"5ba9cfc0194ef62a" "5ba9cfc0194ef62a"
], ],
"x": 754, "x": 754,
"y": 319 "y": 319,
"w": 672,
"h": 622
}, },
{ {
"id": "931cc27100c9317e", "id": "931cc27100c9317e",
@ -1408,7 +1428,9 @@
"f9bc4d36a0df7947" "f9bc4d36a0df7947"
], ],
"x": 34, "x": 34,
"y": 759 "y": 759,
"w": 672,
"h": 222
}, },
{ {
"id": "6e83103b5c5311e9", "id": "6e83103b5c5311e9",
@ -1426,7 +1448,9 @@
"a75651492e779bfc" "a75651492e779bfc"
], ],
"x": 34, "x": 34,
"y": 19 "y": 19,
"w": 912,
"h": 142
}, },
{ {
"id": "88f099618e56926d", "id": "88f099618e56926d",
@ -1508,7 +1532,9 @@
"15f7382daddb4b3e" "15f7382daddb4b3e"
], ],
"x": 34, "x": 34,
"y": 999 "y": 999,
"w": 632,
"h": 82
}, },
{ {
"id": "31767ffd6a647579", "id": "31767ffd6a647579",
@ -1537,7 +1563,9 @@
"e23e7b080ee3773f" "e23e7b080ee3773f"
], ],
"x": 14, "x": 14,
"y": 399 "y": 399,
"w": 1532,
"h": 282
}, },
{ {
"id": "1cc8b382530c33d0", "id": "1cc8b382530c33d0",
@ -1589,7 +1617,9 @@
"6b5d3e550ad76528" "6b5d3e550ad76528"
], ],
"x": 34, "x": 34,
"y": 39 "y": 39,
"w": 692,
"h": 82
}, },
{ {
"id": "4329e464360d0afa", "id": "4329e464360d0afa",
@ -1631,7 +1661,9 @@
"f260e2410571356a" "f260e2410571356a"
], ],
"x": 34, "x": 34,
"y": 139 "y": 139,
"w": 1592,
"h": 422
}, },
{ {
"id": "fba69dfc80829db7", "id": "fba69dfc80829db7",
@ -3535,7 +3567,7 @@
"type": "function", "type": "function",
"z": "a062d9e6d44506dc", "z": "a062d9e6d44506dc",
"name": "Format", "name": "Format",
"func": "const tpc = msg.topic\nconst pld = msg.payload\nvar dest = tpc.replaceAll(\"_\",\" \")\nvar scene = pld.replaceAll(\"_\",\" \")\n\nmsg.topic = dest\nmsg.payload = scene\n\nreturn msg;", "func": "const tpc = msg.topic\nconst pld = msg.payload\nvar dest = tpc.replaceAll(\"_\",\" \")\nvar scene = pld.replaceAll(\"_\",\" \")\n\nmsg.topic = dest\nmsg.payload = scene\n\nreturn msg",
"outputs": 1, "outputs": 1,
"noerr": 0, "noerr": 0,
"initialize": "", "initialize": "",
@ -3570,25 +3602,6 @@
"queue": "none", "queue": "none",
"x": 370, "x": 370,
"y": 40, "y": 40,
"wires": [
[
"011c7df420cc7f1a"
]
]
},
{
"id": "011c7df420cc7f1a",
"type": "function",
"z": "a062d9e6d44506dc",
"name": "Notification",
"func": "const tpc = msg.topic\nvar dest = tpc.replaceAll(\" \",\"_\")\n\nscene = msg.payload\nmsg.dest = dest\n\nmsg.topic = \"Lights Changed\"\nmsg.payload = ('Lights changed to a new scene: ' + scene)\n\nif (msg.payload === 'Lights changed to a new scene: Reset') {\n return null;\n} else {\n return msg;\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 590,
"y": 40,
"wires": [ "wires": [
[] []
] ]
@ -3766,12 +3779,10 @@
"mustacheAltTags": false, "mustacheAltTags": false,
"outputProperties": [], "outputProperties": [],
"queue": "none", "queue": "none",
"x": 1230, "x": 930,
"y": 440, "y": 460,
"wires": [ "wires": [
[ []
"6ae79d1701e78840"
]
] ]
}, },
{ {
@ -3825,52 +3836,19 @@
"x": 1530, "x": 1530,
"y": 280, "y": 280,
"wires": [ "wires": [
[ []
"22250920efccf4d1"
]
]
},
{
"id": "a4926fb1dba17563",
"type": "switch",
"z": "a4ee891237e460a2",
"name": "Nightlight?",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "cont",
"v": "Nightlight",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 810,
"y": 440,
"wires": [
[
"c19b4dccc5d6a9af"
],
[
"f07ca0de800cbc8f"
]
] ]
}, },
{ {
"id": "c19b4dccc5d6a9af", "id": "c19b4dccc5d6a9af",
"type": "api-call-service", "type": "api-call-service",
"z": "a4ee891237e460a2", "z": "a4ee891237e460a2",
"name": "Nightlight On", "name": "Nightlight Set",
"server": "9e87348d.9c1c48", "server": "9e87348d.9c1c48",
"version": 5, "version": 5,
"debugenabled": false, "debugenabled": false,
"domain": "switch", "domain": "switch",
"service": "turn_on", "service": "turn_{{nightlight}}",
"areaId": [], "areaId": [],
"deviceId": [], "deviceId": [],
"entityId": [ "entityId": [
@ -3882,37 +3860,8 @@
"mustacheAltTags": false, "mustacheAltTags": false,
"outputProperties": [], "outputProperties": [],
"queue": "none", "queue": "none",
"x": 990, "x": 750,
"y": 420, "y": 460,
"wires": [
[
"942a915b6599fb27"
]
]
},
{
"id": "f07ca0de800cbc8f",
"type": "api-call-service",
"z": "a4ee891237e460a2",
"name": "Nightlight Off",
"server": "9e87348d.9c1c48",
"version": 5,
"debugenabled": false,
"domain": "switch",
"service": "turn_off",
"areaId": [],
"deviceId": [],
"entityId": [
"switch.adaptive_lighting_sleep_mode_{{topic}}"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 990,
"y": 480,
"wires": [ "wires": [
[ [
"942a915b6599fb27" "942a915b6599fb27"
@ -4029,25 +3978,6 @@
[] []
] ]
}, },
{
"id": "6ae79d1701e78840",
"type": "function",
"z": "a4ee891237e460a2",
"name": "Format",
"func": "const str = msg.payload\nvar scene = str.replaceAll(\"_\",\" \")\n\nmsg.dest = msg.topic\n\nmsg.topic = \"Lights Changed\"\nmsg.payload = ('Lights changed to a new scene: ' + scene)\n\nif (msg.payload === 'Lights changed to a new scene: Reset') {\n return null;\n} else {\n return msg;\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1400,
"y": 440,
"wires": [
[
"60924f02885cc3c8"
]
]
},
{ {
"id": "dc0972a3964e6513", "id": "dc0972a3964e6513",
"type": "api-call-service", "type": "api-call-service",
@ -4075,46 +4005,15 @@
[] []
] ]
}, },
{
"id": "22250920efccf4d1",
"type": "link out",
"z": "a4ee891237e460a2",
"name": "Adaptive Lighting Flow",
"mode": "link",
"links": [
"a092a70d91589196"
],
"x": 1695,
"y": 280,
"wires": []
},
{
"id": "a092a70d91589196",
"type": "link in",
"z": "a4ee891237e460a2",
"name": "Scene Formatting",
"links": [
"22250920efccf4d1"
],
"x": 1265,
"y": 380,
"wires": [
[
"6ae79d1701e78840"
]
]
},
{ {
"id": "defe84d113029e8e", "id": "defe84d113029e8e",
"type": "subflow:a062d9e6d44506dc", "type": "subflow:a062d9e6d44506dc",
"z": "a4ee891237e460a2", "z": "a4ee891237e460a2",
"name": "", "name": "",
"x": 1280, "x": 980,
"y": 340, "y": 400,
"wires": [ "wires": [
[ []
"60924f02885cc3c8"
]
] ]
}, },
{ {
@ -4183,8 +4082,8 @@
"mustacheAltTags": false, "mustacheAltTags": false,
"outputProperties": [], "outputProperties": [],
"queue": "none", "queue": "none",
"x": 1580, "x": 920,
"y": 440, "y": 340,
"wires": [ "wires": [
[] []
] ]
@ -4213,8 +4112,8 @@
], ],
[ [
"a81fd188d89faa25", "a81fd188d89faa25",
"f9f11a5fa1492190", "dc0972a3964e6513",
"dc0972a3964e6513" "4ab49b8485f1886f"
] ]
], ],
"outputLabels": [ "outputLabels": [
@ -4228,14 +4127,14 @@
"id": "9af9a2966a477b33", "id": "9af9a2966a477b33",
"type": "function", "type": "function",
"z": "a4ee891237e460a2", "z": "a4ee891237e460a2",
"name": "Dynamic Rooms", "name": "Routing",
"func": "var dynamicRooms = global.get(\"dynamicRooms\")\nvar topic = msg.topic\n\nif (dynamicRooms.includes(topic)) {\n return[msg,null]\n} else {\n return[null,msg]\n}", "func": "const str = msg.payload\nvar scene = str.replaceAll(\"_\", \" \")\nvar dynamicRooms = global.get(\"dynamicRooms\")\nvar tvRooms = global.get(\"tvRooms\")\nvar room = msg.topic\n\nvar notify = {\n \"dest\": room,\n \"topic\": \"Lights Changed\",\n \"payload\": \"Lights changed to a new scene: \" + scene\n}\n\nvar statusMsg = {\n \"payload\": \"Lights changed to a new scene: \" + scene\n}\n\nif (dynamicRooms.includes(room)) {\n node.send([msg,null,null,null])\n} else {\n node.send([null,msg,null,null])\n}\n\nif (tvRooms.includes(room)) {\n node.send([null,null,notify,null])\n}\n\nnode.send([null,null,null,statusMsg])",
"outputs": 2, "outputs": 4,
"noerr": 0, "noerr": 0,
"initialize": "", "initialize": "",
"finalize": "", "finalize": "",
"libs": [], "libs": [],
"x": 580, "x": 560,
"y": 280, "y": 280,
"wires": [ "wires": [
[ [
@ -4243,37 +4142,49 @@
], ],
[ [
"3a4f567b17b2214e" "3a4f567b17b2214e"
] ],
[
"60924f02885cc3c8"
],
[]
], ],
"outputLabels": [ "outputLabels": [
"Dynamic", "Dynamic",
"Else" "Else",
"",
""
] ]
}, },
{ {
"id": "f9f11a5fa1492190", "id": "4ab49b8485f1886f",
"type": "function", "type": "function",
"z": "a4ee891237e460a2", "z": "a4ee891237e460a2",
"name": "Dynamic Rooms", "name": "Routing",
"func": "var states = global.get('homeassistant.homeAssistant.states')\nvar dynamicRooms = global.get(\"dynamicRooms\")\nvar topic = msg.topic\n\nif (dynamicRooms.includes(topic)) {\n var inputBoolean = \"input_boolean.\" + topic + \"_dynamic_scenes\"\n var boolState = states[inputBoolean].state\n if (boolState === 'on') {\n return [msg, null]\n } else {\n return [null, msg]\n }\n} else {\n return[null,msg]\n}", "func": "const str = msg.payload\nvar scene = str.replaceAll(\"_\", \" \")\nvar states = global.get('homeassistant.homeAssistant.states')\nvar dynamicRooms = global.get(\"dynamicRooms\")\nvar tvRooms = global.get(\"tvRooms\")\nvar room = msg.topic\n\nvar notify = {\n \"dest\": room,\n \"topic\":\"Lights Changed\",\n \"payload\":\"Lights changed to a new scene: \" + scene\n}\n\nvar statusMsg = {\n \"payload\": \"Lights changed to a new scene: \" + scene\n}\n\nif (scene.includes(\"Nightlight\")) {\n msg.nightlight = \"on\"\n} else {\n msg.nightlight = \"off\"\n}\n\nif (dynamicRooms.includes(room)) {\n var inputBoolean = \"input_boolean.\" + room + \"_dynamic_scenes\"\n var boolState = states[inputBoolean].state\n if (boolState === 'on') {\n node.send([msg,null,null,null])\n } else {\n node.send([null,msg,null,null])\n }\n} else {\n node.send([null,msg,null,null])\n}\n\nif (tvRooms.includes(room)) {\n node.send([null,null,notify,null])\n}\n\nnode.send([null,null,null,statusMsg])",
"outputs": 2, "outputs": 4,
"noerr": 0, "noerr": 0,
"initialize": "", "initialize": "",
"finalize": "", "finalize": "",
"libs": [], "libs": [],
"x": 580, "x": 560,
"y": 440, "y": 440,
"wires": [ "wires": [
[ [
"defe84d113029e8e" "defe84d113029e8e"
], ],
[ [
"a4926fb1dba17563" "c19b4dccc5d6a9af"
] ],
[
"60924f02885cc3c8"
],
[]
], ],
"outputLabels": [ "outputLabels": [
"Dynamic", "Dynamic",
"Else" "Non-Dynamic",
"Notify",
""
] ]
}, },
{ {
@ -4428,9 +4339,7 @@
] ]
], ],
"outputLabels": [ "outputLabels": [
"Adaptive", "Adaptive"
"Reset",
"Otherwise"
] ]
}, },
{ {
@ -10642,8 +10551,7 @@
"y": 340, "y": 340,
"wires": [ "wires": [
[ [
"b19f4a996fd4c2ee", "b19f4a996fd4c2ee"
"d790134891fedfcc"
] ]
] ]
}, },
@ -28314,17 +28222,15 @@
"type": "function", "type": "function",
"z": "3e4ba157b540d183", "z": "3e4ba157b540d183",
"name": "Constants", "name": "Constants",
"func": "// Set options here\nconst dynamicRooms = [\"living_room\",\"basement_studio\"]\n\n// Commit those options to global context\nglobal.set(\"dynamicRooms\", dynamicRooms);\n\nnode.status({fill:\"green\",shape:\"dot\",text:\"Saved\"});", "func": "// Set options here\nconst dynamicRooms = [\"living_room\",\"basement_studio\"]\nconst tvRooms = [\"living_room\",\"basement\",\"basement_studio\",\"master_bedroom\"]\n\n// Commit those options to global context\nglobal.set(\"dynamicRooms\", dynamicRooms)\nglobal.set(\"tvRooms\", tvRooms)\n\nnode.status({fill:\"green\",shape:\"dot\",text:\"Saved\"});",
"outputs": 1, "outputs": 0,
"noerr": 0, "noerr": 0,
"initialize": "", "initialize": "",
"finalize": "", "finalize": "",
"libs": [], "libs": [],
"x": 1040, "x": 1040,
"y": 620, "y": 620,
"wires": [ "wires": []
[]
]
}, },
{ {
"id": "f49fc3da65a68efd", "id": "f49fc3da65a68efd",