Since version 0.9.0
On this page you can find a short tutorial with an example scripting scenario to let you get started with Shelly scripts. Follow the steps to create your first script about idle alerts. The resulting script Script will monitor the inputs of a Shelly and if there was no user interaction with the input(s), it will call an URL with a predefined message.
Then we will start receiving logs by executing the following command:
Create a script named my_idle_alert_script, using the method:
The result from this method will be some
id, let's say id=3.
To start creating scripts, let's create some code that prints "Hello world!":
To upload this code (chunk by chunk) to the script with id=3, which we previously created on the device, we can use:
Note that, when the code is long, we have to upload it chunk by chunk. In the example, it is uploaded line by line, but you may devide it differently across the invocations of
Another thing to note is that, as our first invocation of
Script.PutCode contained the parameter
"append":false, all of code uploaded previously in this Script object will be automatically removed and the content of the script will be replaced with the new code.
Alternatively (and much more simply) we can create the code in a file, let's say
hello_world.txt and upload it in one step, using the script
put_script. This script takes three parameters:
- the IP of the device on which we will upload the script (already set as value of the env variable SHELLY)
- the id of the script object, where we will upload the code (in our case id=3)
- the name of the file containing the code itself
Finally, we have the code uploaded and we can run our first Shelly script (the one with id=3):
This must print 'Hello world!' in the debug log.
Now, we will start to create more sophisticated and useful scripts.
Firstly, let's create some variables: an object CONFIG and an alertTimer with value null:
Create a new script object using
Script.Create, or just put new code to the previous Script object (the one with id=3) with
Script.PutCode. Again, you can just use put_script.py. The resulting script (let's say, again with id=3) can be run as before, but it won't print anything to the terminal.
Let's create a function called "replace". It will take three strings:
substr is a substring of the
origin. The function will replace
Add the function to the code from the previous step and upload it. The resulting script (let's say, again with id=3) can be run as before, but again, it won't print anything to the terminal. However, it can just be evaluated easily by the method:
This must result in the string "The second generation of Shelly devices rule the world!".
We will set a timer that fires repeatedly at every 12 hours, sending an http get request. If the response code from this request is 200, a message will be printed to notify about the success. The values used here are the ones we created in step 5.
Again, append this code to the already added one.
Let's add an event handler which will restart the timer when any event occurs. You will be notified about each reset timer by a message "TIMER WAS RESET" printed in the logs.
Append this code to the already created one. Finally, we have the whole script and you can upload it.
Now, you can start the script.
If you have followed the steps properly, you will be notified each time when there was no event occurred in the last 12 hours (it may indicate a problem at home, or just the fact that someone is on a vacation :) ).