Here is the API description for the Lua scripts as actions in scenes.
With the runCustomScript block, it is possible to execute custom scripts directly from the scene.
For example:
{
"blockOptions":{
"method":{
"name":"runCustomScript",
"args":{
"script":"script"
}
}
},
"blockType":"then",
"fields":[
{
"name":"script",
"type":"scriptId",
"value":"5cf0dc8c7f000068d223e88a"
}
]
}
Commands:
hub.scenes.scripts.add
hub.scenes.scripts.set
hub.scenes.scripts.get
hub.scenes.scripts.delete
hub.scenes.scripts.list
hub.scenes.scripts.run
hub.scenes.delete
hub.scenes.edit
Can be executed:
- as a direct API request with the script - will be executed immediately
- as a scene with the script which can be typed and saved from UI and run by some trigger event
- as a manually running from the app scene that contains script as action block.
Here is the example of the code which will turn on all devices which have "switch " items.
As a direct API request:
{
"id": "_ID_",
"jsonrpc": "2.0",
"method": "hub.scenes.scripts.add",
"params": {
"name": "Switch on all lights",
"code": "require \"core\"\n local items = core.get_items()\n if items then \n \n for _, item in ipairs( items ) do\n if item.value_type == \"bool\" and item.has_setter then\n core.set_item_value( item.id, true )\n end\n end\n end"
}
}
As a scene with the script which can be typed and saved from UI:
require "core"
local items = core.get_items()
if items then
for _, item in ipairs( items ) do
if item.value_type == "bool" and item.has_setter then
core.set_item_value( item.id, true )
end
end
end
Script_As_Action.pdf (67.0 KB)