Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sonoff basic javascript code
require('dotenv').config()
const ewelink = require('ewelink-api')
const credentials = {
username: process.env.EMAIL,
password: process.env.PASSWORD
}
const devices = {
lili: process.env.LILI // device Id
}
const connection = new ewelink({
email: process.env.EMAIL,
password: process.env.PASSWORD,
region: process.env.REGION // eu, us ....
})
connection.getDevices().then(work) // connection.getDevices().then(console.log) to find device ids
async function work() {
try {
const {temperature, power} = await liliStatus()
console.log({temperature, power});
if (temperature < 18 && !power && timeAndDayToPowerOn()) {
console.log({temperature, power}, "Should power lili on");
await connection.setDevicePowerState(devices.lili, 'on')
}else{
if(power){
await connection.setDevicePowerState(devices.lili, 'off')
} // else already powered off
}
} catch (e) {
console.error(e)
}
}
function timeAndDayToPowerOn(){
// look into google calendar
return true;
}
// fetch device informations and parse them
async function liliStatus() {
const stringTemperature = (await connection.getDeviceCurrentTemperature(devices.lili)).temperature
const stringPower = (await connection.getDevicePowerState(devices.lili)).state
const temperature = parseFloat(stringTemperature)
const power = stringPower ==='on';
return {temperature, power}
}
async function powerLili() {
await connection.setDevicePowerState(devices.lili, 'on')
console.log("Lili powered on");
}
@nicolas-zozol

This comment has been minimized.

Copy link
Owner Author

@nicolas-zozol nicolas-zozol commented Jan 7, 2021

.env file is placed at the same level than package.json

EMAIL=toto@gmail.com
PASSWORD=confit de canard
# default is us
REGION=eu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment