UNPKG

node-red-contrib-home-assistant-websocket

Version:
2 lines (1 loc) 15.1 kB
[{"id":"6615a5aad4b9ba51","type":"group","z":"1222406.8e2dfc","name":"Xfinity Usage","style":{"label":true},"nodes":["379b142b447e8ab7","4a33964bcdbd56d2","f85adedd5b32df85","68b863061ef4854f","d366fcc43a925970","9e55614bb42a6ef2","ab7a80d12efeaf5f","9d90f05012917222","b6ed587759967948","ee7d560c9248b5ed","08f4b8c0bfe2e999","d4897a6db2c8f634","da4171d93923d766","c6b4b9b22c1638c8","3a690e3333160edd","caf83470cf44375d","2111e9b06b258ddf","82e81d1a2fed3a97","9ae81fe6351498ae"],"x":14,"y":1095,"w":1084,"h":562},{"id":"379b142b447e8ab7","type":"function","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"hours left","func":"const now = new Date();\nconst lastDay = new Date(now.getFullYear(), now.getMonth() + 1, 0);\nconst hoursLeft = (lastDay.getDate() - now.getDate()) * 24 - now.getHours();\nconst hoursPassed = (now.getDate() - 1) * 24 + now.getHours();\nconst percent = hoursPassed / (hoursPassed + hoursLeft) * 100;\n\nmsg.payload = Math.round(percent);\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":324,"y":1136,"wires":[["4a33964bcdbd56d2"]]},{"id":"4a33964bcdbd56d2","type":"ha-sensor","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"Percent of hours passed","entityConfig":"fe831d83fa8a12c1","version":0,"state":"payload","stateType":"msg","attributes":[],"inputOverride":"block","outputProperties":[],"x":742,"y":1136,"wires":[[]]},{"id":"f85adedd5b32df85","type":"ha-sensor","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"Usage Percent","entityConfig":"8c7d0c6eecee9819","version":0,"state":"$round(usagePercent)","stateType":"jsonata","attributes":[],"inputOverride":"block","outputProperties":[],"x":712,"y":1232,"wires":[[]]},{"id":"68b863061ef4854f","type":"ha-sensor","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"Today's Usage","entityConfig":"cfe3bcc794d1f2c1","version":0,"state":"todaysUsage","stateType":"msg","attributes":[],"inputOverride":"block","outputProperties":[],"x":710,"y":1472,"wires":[[]]},{"id":"d366fcc43a925970","type":"ha-sensor","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"Current Usage","entityConfig":"71242371557e25f3","version":0,"state":"currentUsage","stateType":"msg","attributes":[],"inputOverride":"block","outputProperties":[],"x":712,"y":1184,"wires":[[]]},{"id":"9e55614bb42a6ef2","type":"ha-sensor","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"Remaining Daily Average","entityConfig":"12a17e3c3e3d36ed","version":0,"state":"$round(averageRemaining,1)","stateType":"jsonata","attributes":[],"inputOverride":"block","outputProperties":[],"x":742,"y":1328,"wires":[[]]},{"id":"ab7a80d12efeaf5f","type":"ha-sensor","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"Used Daily Average","entityConfig":"3d5eaa4799914c8e","version":0,"state":"$round(averageUsed,1)","stateType":"jsonata","attributes":[],"inputOverride":"block","outputProperties":[],"x":732,"y":1376,"wires":[[]]},{"id":"9d90f05012917222","type":"function","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"build sensors","func":"const usage = msg.usageMonths?.reverse();\n\nif (!usage) return;\n\nusage.forEach((month, index) => {\n // skip current month\n if (index === 0) return;\n\n const currentDate = new Date(month.startDate)\n const name = currentDate.toLocaleString(\"defualt\", { month: 'long', year: 'numeric' })\n const nodeId = `xfinity_month_usage_${index - 1}`; \n\n // in case sensor has been created yet use nodeId as the entity id\n msg.payload = { data: {\n type: \"nodered/discovery\",\n server_id: \"noderedserver\",\n node_id: nodeId,\n component: \"sensor\",\n config: {\n name: nodeId\n }\n }}; \n node.send(msg); \n \n // Update monthly usage to use friendly month name\n msg.payload = { data: {\n type: \"nodered/discovery\",\n server_id: \"noderedserver\",\n node_id: nodeId,\n component: \"sensor\",\n config: {\n name\n }\n }};\n \n node.send(msg); \n \n msg.payload = { data: {\n type: \"nodered/entity\",\n server_id: \"noderedserver\",\n node_id: nodeId,\n state: month.totalUsage,\n }};\n\n node.send(msg);\n});\n\nnode.done();","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":1424,"wires":[["ee7d560c9248b5ed"]]},{"id":"b6ed587759967948","type":"ha-sensor","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"Yesterday's Usage","entityConfig":"2c0a50b69a193618","version":0,"state":"yesterdaysUsage","stateType":"msg","attributes":[],"inputOverride":"block","outputProperties":[],"x":720,"y":1520,"wires":[[]]},{"id":"ee7d560c9248b5ed","type":"ha-api","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"create monthly usage sensors","server":"","version":1,"debugenabled":false,"protocol":"websocket","method":"get","path":"","data":"","dataType":"jsonata","responseType":"json","outputProperties":[],"x":942,"y":1424,"wires":[[]]},{"id":"08f4b8c0bfe2e999","type":"function","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"build message","func":"if(msg.payload === \"open\") {\n context.set(\"open\", true);\n return;\n}\nif(context.get(\"open\") !== true) return;\n\nconst today = new Date();\nconst monthlyReset = today.getDate() === 1;\nconst lastMonth = msg.usageMonths.find(e => e.startDate.split(\"/\")[0] == getPreviousMonth(today.getMonth() + 1));\nconst currentUsage = monthlyReset ? lastMonth.totalUsage : msg.currentUsage;\nconst startOfYesterday = !isNaN(msg.xfinityStartedTodayAt) ? msg.xfinityStartedTodayAt : 0;\nconst usedYesterday = currentUsage - startOfYesterday; \n\nmsg.payload = `${usedYesterday} GB used yesterday`;\nmsg.yesterdaysUsage = usedYesterday;\n\nif(monthlyReset) {\n const percent = currentUsage/lastMonth.allowableUsage * 100;\n msg.payload += `<br>Used ${currentUsage} of ${lastMonth.allowableUsage} GB (${percent.toFixed(1)}%) last month`;\n} else {\n msg.payload += ` ${msg.remainingUsage} GB remaining`;\n}\n\ncontext.set(\"open\", false);\n\nmsg.todaysUsage = 0;\nmsg.xfinityStartedTodayAt = msg.currentUsage;\n\nreturn msg;\n\nfunction getPreviousMonth(month) { \n return month === 1 ? 12 : month - 1;\n}","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":464,"y":1472,"wires":[["b6ed587759967948","c6b4b9b22c1638c8","82e81d1a2fed3a97","68b863061ef4854f"]]},{"id":"d4897a6db2c8f634","type":"function","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"transform data","func":"const todayStartedAt = msg.xfinityStartedTodayAt || 0;\nconst today = new Date();\nconst currentDay = today.getDate();\nconst daysInMonth = new Date(today.getFullYear(), today.getMonth()+1, 0).getDate();\nconst daysLeftInMonth = daysInMonth - currentDay + 1;\nconst currentMonth = msg.payload.usageMonths.find(e => e.startDate.split(\"/\")[0] == today.getMonth() + 1)\nconst allowableUsage = currentMonth.allowableUsage;\nconst currentUsage = currentMonth.totalUsage;\nconst remainingUsage = allowableUsage - currentUsage;\nconst averageUsed = currentUsage / currentDay;\nconst averageRemaining = remainingUsage / daysLeftInMonth;\nconst todaysUsage = currentUsage - todayStartedAt;\nconst usagePercent = currentUsage / allowableUsage * 100;\n\nreturn {\n usagePercent,\n currentUsage,\n allowableUsage,\n todaysUsage,\n averageUsed,\n averageRemaining,\n remainingUsage,\n usageMonths: msg.payload.usageMonths,xfinityStartedTodayAt: todayStartedAt,\n};\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":464,"y":1328,"wires":[["f85adedd5b32df85","d366fcc43a925970","9e55614bb42a6ef2","ab7a80d12efeaf5f","9d90f05012917222","2111e9b06b258ddf","68b863061ef4854f","08f4b8c0bfe2e999"]]},{"id":"da4171d93923d766","type":"mqtt in","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"","topic":"xfinity","qos":"2","datatype":"auto-detect","broker":"644903cf.1e7dbc","nl":false,"rap":true,"rh":0,"inputs":0,"x":94,"y":1328,"wires":[["9ae81fe6351498ae"]]},{"id":"c6b4b9b22c1638c8","type":"api-call-service","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"send daily notification","server":"","version":7,"debugenabled":false,"action":"notify.person","floorId":[],"areaId":[],"deviceId":[],"entityId":[],"labelId":[],"data":"{\t \"title\": \"Data Usage\",\t \"message\": payload\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","blockInputOverrides":true,"domain":"notify","service":"person","x":732,"y":1616,"wires":[[]]},{"id":"3a690e3333160edd","type":"inject","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"12 am","props":[{"p":"payload"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"open","payloadType":"str","x":268,"y":1472,"wires":[["08f4b8c0bfe2e999"]]},{"id":"caf83470cf44375d","type":"inject","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"every hour","props":[],"repeat":"","crontab":"0 0-23 * * *","once":true,"onceDelay":0.1,"topic":"","x":130,"y":1136,"wires":[["379b142b447e8ab7"]]},{"id":"2111e9b06b258ddf","type":"ha-sensor","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"Remaining Usage","entityConfig":"edb2ab02c66aafda","version":0,"state":"remainingUsage","stateType":"msg","attributes":[],"inputOverride":"block","outputProperties":[],"x":722,"y":1280,"wires":[[]]},{"id":"82e81d1a2fed3a97","type":"ha-sensor","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"Started Today At","entityConfig":"8e9f1b420bd7ac23","version":0,"state":"xfinityStartedTodayAt","stateType":"msg","attributes":[],"inputOverride":"block","outputProperties":[],"x":722,"y":1568,"wires":[[]]},{"id":"9ae81fe6351498ae","type":"api-current-state","z":"1222406.8e2dfc","g":"6615a5aad4b9ba51","name":"startedTodayAt","server":"","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.xfinity_started_today_at","state_type":"str","blockInputOverrides":true,"outputProperties":[{"property":"xfinityStartedTodayAt","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":272,"y":1328,"wires":[["d4897a6db2c8f634"]]},{"id":"fe831d83fa8a12c1","type":"ha-entity-config","server":"","deviceConfig":"157d6b39b3e283d5","name":"Percent of hours passed","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Percent of hours passed"},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"%"},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false},{"id":"8c7d0c6eecee9819","type":"ha-entity-config","server":"","deviceConfig":"157d6b39b3e283d5","name":"Xfinity Usage","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Xfinity Usage Percent"},{"property":"icon","value":"mdi:percent"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"%"},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false},{"id":"cfe3bcc794d1f2c1","type":"ha-entity-config","server":"","deviceConfig":"157d6b39b3e283d5","name":"Xfinity Today's Usage","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Xfinity Today's Usage"},{"property":"icon","value":"mdi:calendar-today"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"GB"},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false},{"id":"71242371557e25f3","type":"ha-entity-config","server":"","deviceConfig":"157d6b39b3e283d5","name":"Xfinity Current Usage","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Xfinity Current Usage"},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"GB"},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false},{"id":"12a17e3c3e3d36ed","type":"ha-entity-config","server":"","deviceConfig":"157d6b39b3e283d5","name":"Xfinity Remaining Daily Average","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Xfinity Average GB Remaining"},{"property":"icon","value":"mdi:chart-ppf"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"GB/day"},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false},{"id":"3d5eaa4799914c8e","type":"ha-entity-config","server":"","deviceConfig":"157d6b39b3e283d5","name":"Used Daily Average","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Xfinity Average GB Used"},{"property":"icon","value":"mdi:chart-line"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"GB/day"},{"property":"state_class","value":""}],"resend":false,"debugEnabled":false},{"id":"2c0a50b69a193618","type":"ha-entity-config","server":"","deviceConfig":"157d6b39b3e283d5","name":"Xfinity Yesterday's Usage","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Xfinity Yesterday's Usage"},{"property":"icon","value":"mdi:calendar-today"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"GB"},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false},{"id":"edb2ab02c66aafda","type":"ha-entity-config","server":"","deviceConfig":"157d6b39b3e283d5","name":"Xfinity Remaining Usage","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Xfinity Remaining Usage"},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"GB"},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false},{"id":"8e9f1b420bd7ac23","type":"ha-entity-config","server":"","deviceConfig":"157d6b39b3e283d5","name":"Xfinity Started Today At","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Xfinity Started Today At"},{"property":"icon","value":""},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":"GB"},{"property":"state_class","value":""}],"resend":true,"debugEnabled":false},{"id":"157d6b39b3e283d5","type":"ha-device-config","name":"Xfinity Usage","hwVersion":"","manufacturer":"Node-RED","model":"","swVersion":""}]