Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sonoff basic javascript code
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')
await connection.setDevicePowerState(devices.lili, 'off')
} // else already powered off
} catch (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");
Copy link

nicolas-zozol commented Jan 7, 2021

.env file is placed at the same level than package.json
PASSWORD=confit de canard
# default is us

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment