Shelly Script Examples - nygma2004/km GitHub Wiki
This document contains a few Shelly Plus script examples. Scripts are a new feature in Shelly Plus devices that are available from the 0.9.0-beta2 firmware. Since this is a beta firmware you need to install it manually (will not get updated automatically). You can do it like any firmware update from the app or web interface under Device/Firmware.
Official Shelly documentation can be found here: https://shelly-api-docs.shelly.cloud/gen2/Scripts/Tutorial
Tutorial Videos
Examples below explained in detail in this video:
Simple flasher cycle
This script toggles the relay (output 0) on and off. Both and off cycle is 1 second.
function callback(userdata) {
Shelly.call("Switch.Toggle","{ id:0 }",null,null);
}
let timer_handle = Timer.set(1000,true,callback,null);
Asymmetric cycle
This script toggles the relay (output 0) on and off. On time is 1 second, off time is 2 seconds.
let bool = false;
let timer_handle = null;
function callback(userdata) {
bool = !bool;
print("Executed: ",bool);
if (bool) {
// turn on and start on-delay
Shelly.call("Switch.Set","{ id:0, on:true }",null,null);
timer_handle = Timer.set(1000,false,callback,null);
} else {
// turn off and start off-delay
Shelly.call("Switch.Set","{ id:0, on:false }",null,null);
timer_handle = Timer.set(2000,false,callback,null);
}
}
callback(null);
Event handler
This script listens to button down even and starts and stops the asymetric cycle (see above)
let userdata = null;
let state = false;
let bool = false;
let timer_handle = null;
function timercallback(userdata) {
bool = !bool;
if (bool) {
// turn on and start on-delay
Shelly.call("Switch.Set","{ id:0, on:true }",null,null);
timer_handle = Timer.set(1000,false,timercallback,null);
} else {
// turn off and start off-delay
Shelly.call("Switch.Set","{ id:0, on:false }",null,null);
timer_handle = Timer.set(2000,false,timercallback,null);
}
}
function eventcallback(userdata) {
print("Event called: ", JSON.stringify(userdata));
// check if this a button press event
if (userdata.info.event==="btn_down") {
print("button down event");
state = !state;
if (state) {
timercallback(null);
} else {
Timer.clear(timer_handle);
}
}
}
Shelly.addEventHandler(eventcallback, userdata);
Remote script call
This script calls a 3rd party web service with a JSON response
Shelly.call("HTTP.GET", { url: "http://192.168.1.91/rpc/Script.Start?id=1" },
function (res, error_code, error_msg, ud) {
if (res.code === 200) {
print("Successfully transmitted a message");
};
},
null);
Control two devices
This script also listens to button down event on the first input and start/stops a script on another device (192.168.1.91) and also toggles the local output:
let userdata = null;
let state = false;
let bool = false;
function eventcallback(userdata) {
// check if this a button press event
if (userdata.info.event==="btn_down") {
print("button down event");
state = !state;
if (state) {
// Start script on the other Shelly
Shelly.call("HTTP.GET", { url: "http://192.168.1.91/rpc/Script.Start?id=1" },
function (res, error_code, error_msg, ud) {
if (res.code === 200) {
print("Successfully transmitted a message");
};
},
null);
// Turn the output on
Shelly.call("Switch.Set","{ id:0, on:true }",null,null);
} else {
// Stop script on the other Shelly
Shelly.call("HTTP.GET", { url: "http://192.168.1.91/rpc/Script.Stop?id=1" },
function (res, error_code, error_msg, ud) {
if (res.code === 200) {
print("Successfully transmitted a message");
};
},
null);
// Turn the output off
Shelly.call("Switch.Set","{ id:0, on:false }",null,null);
}
}
}
Shelly.addEventHandler(eventcallback, userdata);
Public websevice call - HTTP.Get
This script calls a random public API which has a JSON response and toggles the output based on the value in the response.
Shelly.call("HTTP.GET", { url: "https://api.agify.io/?name=meelad" },
function (res, error_code, error_msg, ud) {
if (res.code === 200) {
let resobj = JSON.parse(res.body);
print("Age: ", resobj.age);
if (resobj.age>30) {
Shelly.call("Switch.Set","{ id:0, on:true }",null,null);
} else {
Shelly.call("Switch.Set","{ id:0, on:false }",null,null);
}
};
},
null);
Changing the value of a virtual switch (boolean)
This example sets the state of a boolean:200 virtual switch to myvalue (which should be a boolean value).
Shelly.call("Boolean.Set",{ id:200, value:myvalue},null,null);
Changing the value of a virtual number
This example sets the state of a number:200 virtual switch to myvalue.
Shelly.call("Number.Set",{ id:200, value:resp.myvalue },null,null);
Catch the event when the input button is pushed
This event handler is looking for when the first button is pressed, and specifically listening to the single_push event.
Shelly.addEventHandler(function (event) {
// for debugging
// console.log("Event: ",JSON.stringify(event));
// handle button press events
if (typeof event === 'object' &&
event.info &&
event.info.component === 'input:0' &&
event.info.event === 'single_push') {
console.log("Button is pressed");
}
});
Catch the update when the output status has changed
The output changes do not generate events, but status updates. This code listens to when the first output switch:0 changes.
Shelly.addStatusHandler(function (status) {
// for debugging
// console.log("Status: ",JSON.stringify(status));
// look for the status message when the switch was changed by pressing the button
if (typeof status === "object" && status.component === "switch:0" && status.delta) {
if (status.delta.output) {
console.log("Switch on");
} else {
console.log("Switch off");
}
}
});
