Skip to content

Instantly share code, notes, and snippets.

@kylekyle
Created July 2, 2020 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylekyle/b350e7ff9c24750bda874200edfdc07f to your computer and use it in GitHub Desktop.
Save kylekyle/b350e7ff9c24750bda874200edfdc07f to your computer and use it in GitHub Desktop.
Monitor when an appliance turns on/off using a Tuya compatible (Smart Life and a bunch of other brands) smart plug
const axios = require('axios')
const TuyAPI = require('tuyapi');
var connected = false;
var washerWasOn = false;
// follow these instructions to get the key and id for your plug:
// https://github.com/AMoo-Miki/homebridge-tuya-lan/wiki/Setup-Instructions
const washer = new TuyAPI({
id: 'YOUR ID',
key: 'YOUR KEY'
});
washer.on('data', data => {
var power = data.dps['5'];
if (power !== undefined) {
// for light bulbs, the power is zero when the bulb is off
// some appliances draw more power when they're off, so adjust accordingly
// my washer draw is around 20 when it is idle. Seems high ...
var washerIsOn = power > 30;
console.log("washer is " + (washerIsOn ? 'on' : 'off') + " (" + power + ")");
if (washerWasOn && !washerIsOn) {
console.log("washer is done");
// I use the NotifyMe skill to send the notification to the Echo
// you could also use a custom skill and the ProactiveEvents API,
// but at the time this was written, the API was terrible
axios.post('https://api.notifymyecho.com/v1/NotifyMe', {
notification: "Washy washy washy washy washy washy washy washy washy washy washy washy",
accessCode: "YOUR ACCESS CODE"
})
}
washerWasOn = washerIsOn;
}
});
washer.on('connected', () => connected = true);
washer.on('error', error => {
connected = false;
console.log(error);
});
var connect = setInterval(() => {
if (connected == false) {
console.log('Connecting ...');
washer
.find()
.then(() => washer.connect())
.catch(console.log);
}
}, 5_000);
// only one socket can be connected to the plug at a time,
// so make sure to disconnect before exiting
var exit = () => {
console.log("\nDisconnecting ...");
clearInterval(connect);
washer.disconnect();
process.exit();
}
process.on('SIGINT', exit);
process.on('SIGTERM', exit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment