Add 'notificationnode.js'

This commit is contained in:
2023-01-26 13:37:43 +13:00
parent c9eca4971c
commit 98846a7bd3

53
notificationnode.js Normal file
View File

@@ -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;