Skip to content

Instantly share code, notes, and snippets.

@Deses
Last active February 14, 2020 18:12
Show Gist options
  • Save Deses/bf2355075ac38137923129c2da136da0 to your computer and use it in GitHub Desktop.
Save Deses/bf2355075ac38137923129c2da136da0 to your computer and use it in GitHub Desktop.
A little script to turn off a Tuya plug,
const TuyAPI = require('tuyapi');
const device = new TuyAPI({
id: 'XXXXXXXXXXXXXXXXXXXX',
key: 'XXXXXXXXXXXXXXXX'});
let stateHasChanged = false;
// Find device on network
device.find().then(() => {
device.connect();
});
// Add event listeners
device.on('connected', () => {
console.log('Connected to device!');
});
device.on('disconnected', () => {
console.log('Disconnected from device.');
});
device.on('error', error => {
console.log('Error!', error);
});
device.on('data', data => {
//console.log('Data from device:', data.dps['1']);
if (!data.dps['1']) {
console.log('The printer is already off!');
} else {
console.log('The printer is on. Turning it off.');
if (!stateHasChanged) {
device.set({set: false});
stateHasChanged = true;
}
}
});
setTimeout(() => { device.disconnect(); }, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment