Skip to content

Instantly share code, notes, and snippets.

@zachowj
Created August 30, 2020 00:03
Show Gist options
  • Save zachowj/2325e34397d2b4fa3a39714b229fec35 to your computer and use it in GitHub Desktop.
Save zachowj/2325e34397d2b4fa3a39714b229fec35 to your computer and use it in GitHub Desktop.
Node-RED export for xfinity-data-usage
[{"id":"b8f1078b.24ad98","type":"ha-entity","z":"530c54b8.7cee3c","name":"Xfinity Usage","server":"55a65aed.d6a484","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Xfinity Usage"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:percent"},{"property":"unit_of_measurement","value":""}],"state":"currentUsage & \"/\" & allowableUsage & \" GB (\" & $round(currentUsage / allowableUsage * 100, 0) & \"%)\"","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":582,"y":128,"wires":[[]]},{"id":"f35fd1cd.e4526","type":"http in","z":"530c54b8.7cee3c","name":"","url":"/xfinity","method":"post","upload":false,"swaggerDoc":"","x":182,"y":176,"wires":[["325034f4.cb3e7c","bb07c2f8.50e11"]]},{"id":"325034f4.cb3e7c","type":"http response","z":"530c54b8.7cee3c","name":"","statusCode":"","headers":{},"x":354,"y":128,"wires":[]},{"id":"da9ef037.484c1","type":"comment","z":"530c54b8.7cee3c","name":"Xfinity","info":"","x":162,"y":128,"wires":[]},{"id":"bb07c2f8.50e11","type":"function","z":"530c54b8.7cee3c","name":"transform data","func":"const todayStartedAt = flow.get(\"xfinityStartedTodayAt\", \"file\") || 0;\nconst today = new Date();\nconst daysInMonth = new Date(today.getFullYear(), today.getMonth(),0).getDate();\nconst daysLeftInMonth = daysInMonth - today.getDate();\nconst [currentMonth] = msg.payload.usageMonths.slice(-1);\nconst allowableUsage = currentMonth.allowableUsage;\nconst currentUsage = currentMonth.totalUsage;\nconst remainingUsage = allowableUsage - currentUsage;\nconst averageUsed = currentUsage / today.getDate();\nconst averageRemaining = remainingUsage / (daysLeftInMonth + 1);\nconst todaysUsage = currentUsage - todayStartedAt;\n\nreturn {\n currentUsage,\n remainingUsage,\n allowableUsage,\n todaysUsage,\n averageUsed,\n averageRemaining\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":384,"y":176,"wires":[["b8f1078b.24ad98","560bf7c.883e508","d2126a44.1a5878","e3292468.d40c88","4e22b526.b0dfdc"]]},{"id":"67da98e1.5d2108","type":"inject","z":"530c54b8.7cee3c","name":"12am","props":[{"p":"payload"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"open","payloadType":"str","x":178,"y":320,"wires":[["4e22b526.b0dfdc"]]},{"id":"4e22b526.b0dfdc","type":"function","z":"530c54b8.7cee3c","name":"","func":"let open = context.get(\"open\", \"file\") || false;\nif(msg.payload === \"open\") {\n open = true;\n return;\n}\nif(!open) return;\n\nconst startOfYesterday = flow.get(\"xfinityStartedTodayAt\", \"file\") || 0;\nconst usedYesterday = msg.currentUsage - startOfYesterday;\n\nmsg.payload = `${usedYesterday} GB used yesterday. ${msg.remainingUsage} GB remaining`;\nnode.status({\"text\": msg.payload});\n\nflow.set(\"xfinityStartedTodayAt\", msg.currentUsage, \"file\");\ncontext.set(\"open\", false, \"file\");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":364,"y":320,"wires":[["53f45169.9aa7f"]]},{"id":"560bf7c.883e508","type":"ha-entity","z":"530c54b8.7cee3c","name":"Today's Usage","server":"55a65aed.d6a484","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Today's Usage"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:calendar-today"},{"property":"unit_of_measurement","value":"GB"}],"state":"todaysUsage","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":592,"y":176,"wires":[[]]},{"id":"d2126a44.1a5878","type":"ha-entity","z":"530c54b8.7cee3c","name":"Used Daily Average","server":"55a65aed.d6a484","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Average GB Used"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:chart-line"},{"property":"unit_of_measurement","value":"GB/day"}],"state":"$round(averageUsed,1)","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":602,"y":224,"wires":[[]]},{"id":"e3292468.d40c88","type":"ha-entity","z":"530c54b8.7cee3c","name":"Remaining Daily Average","server":"55a65aed.d6a484","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Average GB Remaining"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:chart-ppf"},{"property":"unit_of_measurement","value":"GB/day"}],"state":"$round(averageRemaining,1)","stateType":"jsonata","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":622,"y":272,"wires":[[]]},{"id":"53f45169.9aa7f","type":"api-call-service","z":"530c54b8.7cee3c","name":"","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_pixel","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":632,"y":320,"wires":[[]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment