Script
Since version 0.9.0
The Script component handles management of scripts on the device. It uses Script
as RPC namespace and provides the methods:
Script.GetConfig
Script.SetConfig
Script.GetStatus
Script.List
Script.Create
Script.Delete
Script.Start
Script.Stop
Script.PutCode
Script.GetCode
Script.Eval
A script on the device is a container or a slot consisting of meta data and a file - the actual executable code. The Script component offers RPC endpoints for creation, modification, configuration and execution of scripts.
A script in a slot can be running or not and can be configured to run on boot. More information about the scripting language and the opportunities it gives can be found on the Language Features page. You can also check out the short tutorial.
A number of limitations apply and known issues exist. Please refer to the language features page for more details.
Methods
Script.SetConfig
Property | Type | Description |
---|---|---|
| number | Id of the script |
| object | Configuration that the method takes |
Find more about the config properties in config section
Script.GetConfig
Property | Type | Description |
---|---|---|
| number | Id of the script |
Find the Script.GetConfig response properties in config section
Script.GetStatus
Property | Type | Description |
---|---|---|
| number | Id of the script |
Script.Create
This method creates a new script.
Request
Parameters:
Property | Type | Description |
---|---|---|
| string | Name of the script. If it is missed, a default name |
Response
Attributes in the result:
Property | Type | Description |
---|---|---|
| number | Id of the created script |
Script.PutCode
This method allows uploading code to an existing, but not currently running script. If the script is running, it must be stopped before trying to overwrite, otherwise an error is returned. Furthermore, the maximum size of a script is limited, see the section on resource limits.
Uploading of code might need to happen in chunks. If the size of the file to be uploaded is large, and there is a need to upload files in chunks use the append parameter to add the next part of the content. We have a Python tool in our GitHub repo that is uploading scripts in chunks of 1024 bytes.
Request
Parameters:
Property | Type | Description |
---|---|---|
| number | Id of the created script |
| string | The code which will be included in the script (the length must be greater than 0). Required |
| boolean |
|
Response
Attributes in the result:
Property | Type | Description |
---|---|---|
| number | The total code length in bytes |
Script.GetCode
This method downloads code from an existing script.
Request
Parameters:
Property | Type | Description |
---|---|---|
| number | Id of the created script |
| number | Byte offset from the beginning. Default value: |
| number | Bytes to read. Default value: maximum possible number of bytes till the end is reached. Optional |
Response
Attributes in the result:
Property | Type | Description |
---|---|---|
| string | The requested data chunk |
| number | Number of bytes remaining till the end of the code |
Script.Eval
This method evaluates or executes some code inside of a specified script.
The specified script must be running.
Request
Parameters:
Property | Type | Description |
---|---|---|
| number | Id of the script |
| string | Argument to evaluate (the length must be greater than 0). Required |
let a = 5+5
Response
Attributes in the result:
Property | Type | Description |
---|---|---|
| string | The result of the evaluation |
Script.Start
This method runs a script.
Up to 3 scripts can be running at any given time.
If there is no code put in the script the method will return an error.
Request
Parameters:
Property | Type | Description |
---|---|---|
| number | Id of the created script |
Response
Attributes in the result:
Property | Type | Description |
---|---|---|
| boolean |
|
Script.Stop
This method stops the execution of a script.
Request
Parameters:
Property | Type | Description |
---|---|---|
| number | Id of the created script |
Response
Attributes in the result:
Property | Type | Description |
---|---|---|
| boolean |
|
Script.List
This method lists all scripts.
Request
This method takes no parameters.
Response
Attributes in the result:
Property | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| array of objects | List of all created scripts
|
Script.Delete
This method removes specified script.
Request
Parameters:
Property | Type | Description |
---|---|---|
| number | Id of the created script |
Response
The result from this method is null
.
Configuration
The configuration of the Script component shows the identifiers of the script, as well as whether it is run on boot.
Properties:
Property | Type | Description |
---|---|---|
| number | Id of the script |
| string | Name of the script |
| boolean |
|
Status
The status of the Script component shows whether the script is currently running.
Properties:
Property | Type | Description | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| number | Id of the script | ||||||||||||||||||||||
| boolean |
| ||||||||||||||||||||||
| array | Optional, present only when the script execution resulted in an error. The array contains description of the type of error. Possible errors are:
|
Examples
Script.SetConfig example
- Script.SetConfig HTTP GET Request
- Script.SetConfig Curl Request
- Script.SetConfig Mos Request
http://192.168.33.1/rpc/Script.SetConfig?id=1&config={"enable":true}
curl -X POST -d '{"id":1,"method":"Script.SetConfig","params":{"id":1,"config":{"enable":true}}}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.SetConfig '{"id":1,"config":{"enable":true}}'
Response
- Script.SetConfig HTTP GET Response
- Script.SetConfig Curl Response
- Script.SetConfig Mos Response
{
"restart_required": false
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"restart_required": false
}
}
{
"restart_required": false
}
Script.GetConfig example
- Script.GetConfig HTTP GET Request
- Script.GetConfig Curl Request
- Script.GetConfig Mos Request
http://192.168.33.1/rpc/Script.GetConfig?id=1
curl -X POST -d '{"id":1,"method":"Script.GetConfig","params":{"id":1}}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.GetConfig '{"id":1}'
Response
- Script.GetConfig HTTP GET Response
- Script.GetConfig Curl Response
- Script.GetConfig Mos Response
{
"id": 1,
"name": "scr_1",
"enable": false
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"id": 1,
"name": "scr_1",
"enable": false
}
}
{
"id": 1,
"name": "scr_1",
"enable": false
}
Script.GetStatus example
- Script.GetStatus HTTP GET Request
- Script.GetStatus Curl Request
- Script.GetStatus Mos Request
http://192.168.33.1/rpc/Script.GetStatus?id=1
curl -X POST -d '{"id":1,"method":"Script.GetStatus","params":{"id":1}}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.GetStatus '{"id":1}'
Response
- Script.GetStatus HTTP GET Response
- Script.GetStatus Curl Response
- Script.GetStatus Mos Response
{
"id": 1,
"running": false
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"id": 1,
"running": false
}
}
{
"id": 1,
"running": false
}
Script.Create example
- Script.Create HTTP GET Request
- Script.Create Curl Request
- Script.Create Mos Request
http://192.168.33.1/rpc/Script.Create?name="my_script"
curl -X POST -d '{"id":1,"method":"Script.Create","params":{"name":"my_script"}}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.Create '{"name":"my_script"}'
Response
- Script.Create HTTP GET Response
- Script.Create Curl Response
- Script.Create Mos Response
{
"id": 1
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"id": 1
}
}
{
"id": 1
}
Script.PutCode example
- Script.PutCode HTTP GET Request
- Script.PutCode Curl Request
- Script.PutCode Mos Request
http://192.168.33.1/rpc/Script.PutCode?id=1&code="console.log(5+6)"
curl -X POST -d '{"id":1,"method":"Script.PutCode","params":{"id":1,"code":"console.log(5+6)"}}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.PutCode '{"id":1,"code":"console.log(5+6)"}'
Response
- Script.PutCode HTTP GET Response
- Script.PutCode Curl Response
- Script.PutCode Mos Response
{
"len": 16
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"len": 16
}
}
{
"len": 16
}
Script.GetCode example
- Script.GetCode HTTP GET Request
- Script.GetCode Curl Request
- Script.GetCode Mos Request
http://192.168.33.1/rpc/Script.GetCode?id=1
curl -X POST -d '{"id":1,"method":"Script.GetCode","params":{"id":1}}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.GetCode '{"id":1}'
Response
- Script.GetCode HTTP GET Response
- Script.GetCode Curl Response
- Script.GetCode Mos Response
{
"data": "console.log(5+6)",
"left": 0
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"data": "console.log(5+6)",
"left": 0
}
}
{
"data": "console.log(5+6)",
"left": 0
}
Script.Eval example
- Script.Eval HTTP GET Request
- Script.Eval Curl Request
- Script.Eval Mos Request
http://192.168.33.1/rpc/Script.Eval
curl -X POST -d '{"id":1,"method":"Script.Eval"}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.Eval
Response
- Script.Eval HTTP GET Response
- Script.Eval Curl Response
- Script.Eval Mos Response
{
"result": "15"
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"result": "15"
}
}
{
"result": "15"
}
Script.Start example
- Script.Start HTTP GET Request
- Script.Start Curl Request
- Script.Start Mos Request
http://192.168.33.1/rpc/Script.Start?id=1
curl -X POST -d '{"id":1,"method":"Script.Start","params":{"id":1}}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.Start '{"id":1}'
Response
- Script.Start HTTP GET Response
- Script.Start Curl Response
- Script.Start Mos Response
{
"was_running": false
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"was_running": false
}
}
{
"was_running": false
}
Script.Stop example
- Script.Stop HTTP GET Request
- Script.Stop Curl Request
- Script.Stop Mos Request
http://192.168.33.1/rpc/Script.Stop?id=1
curl -X POST -d '{"id":1,"method":"Script.Stop","params":{"id":1}}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.Stop '{"id":1}'
Response
- Script.Stop HTTP GET Response
- Script.Stop Curl Response
- Script.Stop Mos Response
{
"was_running": true
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"was_running": true
}
}
{
"was_running": true
}
Script.List example
- Script.List HTTP GET Request
- Script.List Curl Request
- Script.List Mos Request
http://192.168.33.1/rpc/Script.List
curl -X POST -d '{"id":1,"method":"Script.List"}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.List
Response
- Script.List HTTP GET Response
- Script.List Curl Response
- Script.List Mos Response
{
"scripts": [
{
"id": 1,
"name": "my_script",
"enable": false,
"running": true
},
{
"id": 2,
"name": "my_script2",
"enable": false,
"running": false
}
]
}
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": {
"scripts": [
{
"id": 1,
"name": "my_script",
"enable": false,
"running": true
},
{
"id": 2,
"name": "my_script2",
"enable": false,
"running": false
}
]
}
}
{
"scripts": [
{
"id": 1,
"name": "my_script",
"enable": false,
"running": true
},
{
"id": 2,
"name": "my_script2",
"enable": false,
"running": false
}
]
}
Script.Delete example
- Script.Delete HTTP GET Request
- Script.Delete Curl Request
- Script.Delete Mos Request
http://192.168.33.1/rpc/Script.Delete?id=1
curl -X POST -d '{"id":1,"method":"Script.Delete","params":{"id":1}}' http://${SHELLY}/rpc
mos --port ${PORT} call Script.Delete '{"id":1}'
Response
- Script.Delete HTTP GET Response
- Script.Delete Curl Response
- Script.Delete Mos Response
null
{
"id": 1,
"src": "shellypro1-84cca87c1f90",
"params": null
}
null