From 98846a7bd336726e7bec2ad909b7942c180018ee Mon Sep 17 00:00:00 2001 From: zorruno Date: Thu, 26 Jan 2023 13:37:43 +1300 Subject: [PATCH] Add 'notificationnode.js' --- notificationnode.js | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 notificationnode.js diff --git a/notificationnode.js b/notificationnode.js new file mode 100644 index 0000000..ccc9be9 --- /dev/null +++ b/notificationnode.js @@ -0,0 +1,53 @@ +// Get Current Cycle Symmary Array +var currentCycle = flow.get("currentApplianceCycle"); + +var date = new Date(); +msg.payload = {}; + +// Function to convert unix epoch to X hours and Y mins +// and format as 'Xh Xmin'. Drop the hours if less than 1. +function secondsToHms(d) { + d = Number(d); + var h = Math.floor(d / 3600); + var m = Math.floor(d % 3600 / 60); + if (h < 1 ) { + return ('0' + m).slice(-2)+"min"; + } + else { + return ('0' + h).slice(-2) + "h " + ('0' + m).slice(-2)+"min"; + } +} + +// Function to convert unix epoch to 00:00 +function epochToFormattedTime(d) { + d = Date(d * 1000); + var h = d.gethours(); + var m = "0" + d.getminutes(); + return h + ':' + m.substr(-2); +} + + +// Extract start/stop time based on the unix timestamp +// First multiply by 1000 so that the argument is in milliseconds, not seconds. +var startDateTime = new Date(currentCycle.cycleTimeStart * 1000); +var stopDateTime = new Date(currentCycle.cycleTimeStop * 1000); +var startDateHours = "0" + startDateTime.getHours(); +var stopDateHours = "0" + stopDateTime.getHours(); +var startDateMinutes = "0" + startDateTime.getMinutes(); +var stopDateMinutes = "0" + stopDateTime.getMinutes(); + +// Format times +// Will display time in 10:30 format (no seconds) +var formattedStartTime = startDateHours.substr(-2) + ':' + startDateMinutes.substr(-2) +var formattedStopTime = stopDateHours.substr(-2) + ':' + stopDateMinutes.substr(-2) + +// Put together a notification ready for sending to notifying tools +msg.topic = flow.get("applianceName") + " Notification"; +msg.payload = "Your " + flow.get("applianceAction") + + " is complete.\nIt started at " + formattedStartTime + + ", finished at " + formattedStopTime + + ", and used " + currentCycle.totalCyclePowerFormatted + + ", taking " + secondsToHms(currentCycle.cycleTimeStop - currentCycle.cycleTimeStart) + + " at a cost of " + currentCycle.totalCycleCostFormatted; + +return msg;