Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
NotEnoughTech
Published © CC BY-NC

Washing Machine Notifications

Get your "dumb" washing machine talking to Google Home and Android devices - never forget about the washing again!

IntermediateFull instructions provided2 hours2,317
Washing Machine Notifications

Things used in this project

Hardware components

sonoff pow r2
×1

Software apps and online services

Node-RED
Node-RED

Story

Read more

Code

Code snippet #2

Plain text
var power = msg.payload.StatusSNS.ENERGY.Power;var timer = flow.get("timeout");
var total = flow.get("Total");
var cost = flow.get("CostArray");//check if array exists
if(!total || !total.length || total === undefined){
        total = [];
    }//push element
total.unshift(power);
//remove X elementh
if(total[timer] === undefined) {
    
    flow.set("Total", total);
}
else {
    total.splice(timer, 1);
    flow.set("Total", total);
}

Code snippet #3

Plain text
function secondsToHms(d) {    d = Number(d);
    var h = Math.floor(d / 3600);
    var m = Math.floor(d % 3600 / 60);
    return ('0' + h).slice(-2) + "h " + ('0' + m).slice(-2)+"min";
}flow.set("announce", false);
var start = flow.get("WashStart");
var timer =  flow.get("timeout");//calculate wash time
var date = new Date();
var ms = date.getTime();var totaltimeinsec = (ms-start)/1000 - 60 *timer;
var totalWashTime = secondsToHms(totaltimeinsec);flow.set("TotalWashTime",totalWashTime);
flow.set("WashStart", 0);// save the wash power session
var washtotal = flow.get("WashTotal");
var sum = washtotal;function add(accumulator, a) {
    return accumulator + a;
}var average = sum.reduce(add);
msg.average = average / washtotal.length;
flow.set("WashTotal", null);//total cost
var sum = flow.get("CostArray");function add(accumulator, a) {
    return accumulator + a;
}var costofpower = sum.reduce(add);
var totalcost =  Math.round(costofpower * 100) / 100;
flow.set("CostArray", null);
flow.set("TotalCost", totalcost);msg = {};msg.payload = "Your washing is ready";
msg.ms = ms;
msg.totalWashTime = totalWashTime;return msg;

Code snippet #4

Plain text
flow.set("announce", true);var power = msg.payload;
var total = flow.get("WashTotal");
var start = flow.get("WashStart");
// just starting the wash
if(start === 0){
    var date = new Date();
    var sec = date.getTime();
    flow.set("WashStart", sec);
}
//check if array exists
if(!total || !total.length || total === undefined){
        total = [];
    }
//push element
total.unshift(power);
flow.set("WashTotal", total);
msg.payload =  total;
return msg;

Credits

NotEnoughTech

NotEnoughTech

10 projects • 82 followers
I'm Mat I play with gadgets, make new stuff and automate pretty much everything that requires repeating more than once!

Comments