const TuyAPI = require('tuyapi'); /*const device = new TuyAPI({ id: '72016014bcddc2a3630e', key: '2c01c1f6216ea935', version:3.1, ip:'192.168.1.33'}); */ const device = new TuyAPI({ id: '88840765bcddc2293084', key: 'd9e6c472a3f9e4cb', ip:'192.168.1.31'}); let stateHasChanged = false; // Find device on network device.find(30).then(() => { // Connect to device 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); console.log(`Boolean status of default property: ${data.dps['1']}.`); // Set default property to opposite if (!stateHasChanged) { device.set({set: !(data.dps['1'])}); // Otherwise we'll be stuck in an endless // loop of toggling the state. stateHasChanged = true; } }); // Disconnect after 10 seconds setTimeout(() => { device.disconnect(); }, 30000);