HDL Automation to Home Assistant
Adds HDL Automation support to Home Assistant.
Currently supports:
- Scene Control
- PIR Control
[{"id":"19314f19fb237662","type":"subflow","name":"Home Assistant Event","info":"","category":"","in":[],"out":[{"x":1040,"y":220,"wires":[{"id":"b1dc0a0f26f5713a","port":0}]}],"env":[{"name":"Event","type":"str","value":"","ui":{"type":"select","opts":{"opts":[{"l":{"en-US":"Loaded"},"v":"loaded"},{"l":{"en-US":"Script Update"},"v":"scriptUpdate"}]}}}],"meta":{},"color":"#DDAA99"},{"id":"53b0710ef31acff2","type":"function","z":"19314f19fb237662","name":"function 14","func":"switch (env.get(\"Event\")) {\n case \"loaded\": {\n if (msg.topic == \"home_assistant_client:ready\" && msg.payload == \"ready\") {\n msg.delay = 1;\n return msg;\n }\n break;\n }\n case \"scriptUpdate\": {\n if (msg.topic == \"call_service\" && msg.payload.event.domain == \"script\" && msg.payload.event.service == \"reload\") {\n return msg;\n }\n break;\n }\n case \"disconnected\": {\n if (msg.topic == \"home_assistant_client\" && msg.payload == \"disconnected\") {\n return msg;\n }\n break;\n }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":220,"wires":[["b1dc0a0f26f5713a"]]},{"id":"4138ef0b7ad4ecdd","type":"server-events","z":"19314f19fb237662","name":"","server":"19cf4248.b7861e","version":2,"eventType":"home_assistant_client","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"x":500,"y":220,"wires":[["53b0710ef31acff2"]]},{"id":"b1dc0a0f26f5713a","type":"delay","z":"19314f19fb237662","name":"","pauseType":"delayv","timeout":"0","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":900,"y":220,"wires":[[]]},{"id":"2fd3616cd7d46d84","type":"server-events","z":"19314f19fb237662","name":"","server":"19cf4248.b7861e","version":2,"eventType":"call_service","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"x":530,"y":260,"wires":[["53b0710ef31acff2"]]},{"id":"19cf4248.b7861e","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true},{"id":"5e89dcaa06413f09","type":"subflow","name":"HASS Event","info":"","category":"","in":[],"out":[{"x":860,"y":140,"wires":[{"id":"7c798a8f61be78ab","port":0}]}],"env":[{"name":"Event","type":"str","value":"","ui":{"type":"select","opts":{"opts":[{"l":{"en-US":"Loaded"},"v":"loaded"},{"l":{"en-US":"Script Update"},"v":"scriptUpdate"}]}}}],"meta":{},"color":"#DDAA99"},{"id":"2306a1c4ae9bd0d7","type":"function","z":"5e89dcaa06413f09","name":"function 18","func":"switch (env.get(\"Event\")) {\n case \"loaded\": {\n if (msg.topic == \"home_assistant_client:ready\" && msg.payload == \"ready\") {\n msg.delay = 1;\n return msg;\n }\n break;\n }\n case \"scriptUpdate\": {\n if (msg.topic == \"call_service\" && msg.payload.event.domain == \"script\" && msg.payload.event.service == \"reload\") {\n return msg;\n }\n break;\n }\n case \"disconnected\": {\n if (msg.topic == \"home_assistant_client\" && msg.payload == \"disconnected\") {\n return msg;\n }\n break;\n }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":140,"wires":[["7c798a8f61be78ab"]]},{"id":"0bbb1a7f6689f423","type":"server-events","z":"5e89dcaa06413f09","name":"","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"event_type":"home_assistant_client","x":320,"y":140,"wires":[["2306a1c4ae9bd0d7"]]},{"id":"7c798a8f61be78ab","type":"delay","z":"5e89dcaa06413f09","name":"","pauseType":"delayv","timeout":"0","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":720,"y":140,"wires":[[]]},{"id":"4efbf8f6c335d350","type":"server-events","z":"5e89dcaa06413f09","name":"","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"event_type":"call_service","x":350,"y":180,"wires":[["2306a1c4ae9bd0d7"]]},{"id":"79b96b56f278c523","type":"function","z":"72ef382b6e644a68","name":"Set HDL information","func":"flow.set(\"hdlDevices\", {\n \"scene\": {\n \"Scene 1\": {\n \"area\": 1,\n \"subnetId\": 1,\n \"deviceId\": 10,\n \"scenes\": [\"Scene1\", \"Scene2\"] // 0, 1\n },\n \"Scene 2\": {\n \"area\": 2,\n \"subnetId\": 1,\n \"deviceId\": 10,\n \"scenes\": [\"Scene1\", \"Scene2\"]\n },\n \"Multiple devices\": { // Will send to 1.10.2, 1.11.3\n \"area\": [2, 3],\n \"subnetId\": [1, 1],\n \"deviceId\": [10, 11],\n \"scenes\": [\"Scene1\", \"Scene2\"]\n },\n },\n \"PIR\": {\n \"PIR\": {\n \"subnetId\": 1,\n \"deviceId\": 1\n }\n },\n});","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":240,"wires":[[]]},{"id":"cc4762f1d1be0963","type":"inject","z":"72ef382b6e644a68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":175,"y":240,"wires":[["79b96b56f278c523"]],"l":false},{"id":"a12f3119126bf823","type":"server-events","z":"72ef382b6e644a68","name":"","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"$outputData(\"eventData\").event_type","valueType":"jsonata"}],"event_type":"call_service","x":230,"y":300,"wires":[["15ee959ecfebab1f"]]},{"id":"15ee959ecfebab1f","type":"function","z":"72ef382b6e644a68","name":"Set","func":"if (!msg.payload.event.service_data.entity_id) { return; }\nif (typeof msg.payload.event.service_data.entity_id == \"string\") { msg.payload.event.service_data.entity_id = [msg.payload.event.service_data.entity_id]; }\n\nvar areas = flow.get(\"hdlDevices\")[\"scene\"];\nvar msgs = [];\nfor (var i in msg.payload.event.service_data.entity_id) {\n if (msg.payload.event.service_data.entity_id[i].includes(\"hdl_\")) {\n var entityIdSplit = msg.payload.event.service_data.entity_id[i].split(\".\")[1].split(\"_\");\n if (entityIdSplit[1] != \"scene\") { break; }\n\n //Find the scene\n for (var j in areas) {\n if (j.replace(/[^A-Za-z0-9]/g, '').toLowerCase() == entityIdSplit[2]) {\n for (var k in areas[j].scenes) {\n if (areas[j].scenes[k] == msg.payload.event.service_data.option) {\n var add = function (subnetId, deviceId, area) {\n msgs.push({\n \"delay\": (areas[j].delayOff !== undefined && parseInt(k) == 0) ? parseInt(areas[j].delayOff) : 0,\n \"payload\": {\n \"operate\": \"sceneControl\",\n \"subnetId\": parseInt(subnetId),\n \"deviceId\": parseInt(deviceId),\n \"mode\": \"set\",\n \"direction\": \"request\",\n \"data\": {\n \"areaNumber\": parseInt(area),\n \"sceneNumber\": parseInt(k)\n }\n }\n });\n }\n\n if (areas[j].area.length > 1) {\n //Validate\n if (areas[j].area.length == areas[j].subnetId.length && areas[j].subnetId.length == areas[j].deviceId.length) {\n for (var l in areas[j].area) {\n add(areas[j].subnetId[l], areas[j].deviceId[l], areas[j].area[l]);\n }\n }\n }\n else {\n add(areas[j].subnetId, areas[j].deviceId, areas[j].area);\n }\n }\n }\n }\n }\n }\n}\nreturn [msgs];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":300,"wires":[["3fab145079543c7b"]]},{"id":"2ea046eb887b7843","type":"function","z":"72ef382b6e644a68","name":"Request entities","func":"var scenes = flow.get(\"hdlDevices\")[\"scene\"];\nvar msgs = [];\nfor (var i in scenes) {\n msgs.push({\n \"payload\": {\n \"operate\": \"sceneControl\",\n \"subnetId\": parseInt(scenes[i].subnetId),\n \"deviceId\": parseInt(scenes[i].deviceId),\n \"mode\": \"get\",\n \"direction\": \"request\",\n \"data\": {\n \"areaNumber\": parseInt(scenes[i].area)\n }\n }\n });\n}\n\nreturn [msgs];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":360,"wires":[["c214088a68f4368c"]],"outputLabels":["BUS"]},{"id":"c214088a68f4368c","type":"hdl-bus","z":"72ef382b6e644a68","name":"","outputMode":"all","network":"","x":540,"y":360,"wires":[["e97b7cccf5ba1552"]]},{"id":"e97b7cccf5ba1552","type":"function","z":"72ef382b6e644a68","name":"HDL > HASS","func":"if (msg.payload.operate == \"sceneControl\" && msg.payload.direction == \"answerBack\") {\n var scenes = flow.get(\"hdlDevices\")[\"scene\"];\n var msgs = [];\n for (var i in scenes) {\n if (msg.payload.sender.subnetId == parseInt(scenes[i].subnetId) && msg.payload.sender.deviceId == parseInt(scenes[i].deviceId)) {\n if (parseInt(scenes[i].area) == msg.payload.data.areaNumber) {\n return {\n \"entity_id\": `input_select.hdl_scene_${i.replace(/[^A-Za-z0-9]/g, '').toLowerCase()}`,\n \"payload\": {\n \"data\": {\n \"state\": scenes[i].scenes[msg.payload.data.sceneNumber],\n \"attributes\": {\n \"friendly_name\": i,\n \"options\": scenes[i].scenes,\n \"deviceId\": `${scenes[i].subnetId}_${scenes[i].deviceId}`,\n \"editable\": true\n }\n }\n }\n }\n }\n }\n }\n}\n//PIR\nelse if (msg.payload.operate == \"dryContact\" && msg.payload.mode == \"broadcast\" && msg.payload.direction == \"request\") {\n if (msg.payload.data.areaNumber == 255 && msg.payload.data.switchNumber == 3) {\n var pirs = flow.get(\"hdlDevices\")[\"PIR\"];\n for (var i in pirs) {\n if (pirs[i].subnetId == msg.payload.sender.subnetId && pirs[i].deviceId == msg.payload.sender.deviceId) {\n return {\n \"entity_id\": `binary_sensor.hdl_pir_${i.replace(/[^A-Za-z0-9]/g, '').toLowerCase()}`,\n \"payload\": {\n \"data\": {\n \"state\": msg.payload.data.state,\n \"attributes\": {\n \"friendly_name\": i,\n \"deviceId\": `${pirs[i].subnetId}_${pirs[i].deviceId}`,\n \"editable\": true\n }\n }\n }\n }\n }\n }\n }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":360,"wires":[["84031b152ab1342e"]],"outputLabels":["BUS"]},{"id":"84031b152ab1342e","type":"ha-api","z":"72ef382b6e644a68","name":"","server":"","version":1,"debugenabled":false,"protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","responseType":"json","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"x":850,"y":360,"wires":[[]]},{"id":"79893b573a520262","type":"comment","z":"72ef382b6e644a68","name":"HDL","info":"Add the scenes in the \"Set HDL Information\" function above.\n\nThese will be added to HASS automatically","x":190,"y":180,"wires":[]},{"id":"3fab145079543c7b","type":"delay","z":"72ef382b6e644a68","name":"","pauseType":"delayv","timeout":"0","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":540,"y":300,"wires":[["c214088a68f4368c"]]},{"id":"95f545bb88114c28","type":"subflow:5e89dcaa06413f09","z":"72ef382b6e644a68","name":"Loaded","env":[{"name":"Event","value":"loaded","type":"str"}],"x":190,"y":360,"wires":[["2ea046eb887b7843"]]},{"id":"562876401d2fc057","type":"function","z":"72ef382b6e644a68","name":"Register entities","func":"\nvar msgs = [];\n\nvar scenes = flow.get(\"hdlDevices\")[\"scene\"];\nfor (var i in scenes) {\n msgs.push({\n \"entity_id\": `input_select.hdl_scene_${i.replace(/[^A-Za-z0-9]/g, '').toLowerCase()}`,\n \"payload\": {\n \"data\": {\n \"state\": \"unknown\",\n \"attributes\": {\n \"friendly_name\": i,\n \"options\": scenes[i].scenes,\n \"deviceId\": `${scenes[i].subnetId}_${scenes[i].deviceId}`,\n \"editable\": true\n }\n }\n }\n });\n}\n\n//PIR\nvar pirs = flow.get(\"hdlDevices\")[\"PIR\"];\nfor (var i in pirs) {\n msgs.push({\n \"entity_id\": `binary_sensor.hdl_pir_${i.replace(/[^A-Za-z0-9]/g, '').toLowerCase()}`,\n \"payload\": {\n \"data\": {\n \"state\": \"unknown\",\n \"attributes\": {\n \"friendly_name\": i,\n \"deviceId\": `${pirs[i].subnetId}_${pirs[i].deviceId}`,\n \"editable\": true\n }\n }\n }\n });\n}\n\nreturn [msgs];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":400,"wires":[["84031b152ab1342e"]],"outputLabels":["BUS"]},{"id":"6e3496bc2fbb04db","type":"subflow:19314f19fb237662","z":"72ef382b6e644a68","name":"Load","env":[{"name":"Event","value":"loaded","type":"str"}],"x":530,"y":400,"wires":[["562876401d2fc057"]]}]