Rules cover the vast majority of needs, and it is better to stick with them for as long as they suffice: they can be read at a glance, and anyone can change them.
A script becomes useful when you hit:
The language used is Lua, chosen for its simplicity and small footprint.
A script is used in two places within a rule:
As a condition — the script returns true or false, and the rule runs accordingly.
As an action — the script runs when the rule triggers.
In both cases, the editor built into Calaos Installer provides syntax highlighting.
A global object named calaos gives access to your installation. Each IO is designated by its identifier, visible in its properties.
-- Read the state of an IO
local temperature = calaos:getIOValue("io_12")
-- Act on an output
calaos:setIOValue("output_3", true)
getIOValue returns a boolean, a number or text depending on the IO type. The names getInputValue, getOutputValue and setOutputValue also exist and do the same thing: they date from older versions.
local name = calaos:getIOParam("io_12", "name")
calaos:setIOParam("io_12", "visible", "false")
calaos:waitForIO("io_12")
The script pauses until the IO changes state.
local response = calaos:requestUrl("https://example.com/api")
calaos:sendPushNotif("The freezer is above -15 °C")
print("Measured temperature: " .. temperature)
print writes to the server logs — see Logs. It is your main tool for understanding what a script does.
Switching on a booster heater if the average of three sensors drops below the target:
local s1 = calaos:getIOValue("temp_living")
local s2 = calaos:getIOValue("temp_hallway")
local s3 = calaos:getIOValue("temp_bedroom")
local average = (s1 + s2 + s3) / 3
local target = calaos:getIOValue("var_target")
print("Average: " .. average .. " / target: " .. target)
if average < target then
calaos:setIOValue("booster_heater", true)
else
calaos:setIOValue("booster_heater", false)
end
The target here is an internal variable adjustable from the applications: the user tunes their comfort without anyone touching the script.
A script blocks the server while it runs. Avoid long loops and needless waits: a request to an unreachable site can freeze your home automation for the length of the timeout.
Comment your identifiers. io_12 says nothing; a comment line recalling which device it is will save you time later.
Check your values before computing. An unplugged sensor can return an unexpected value, and a division by zero will stop the script.
Test with print. Show intermediate values in the logs before letting the script act on real devices.
Prefer a rule when you can. A script that only switches a lamp on when another comes on is harder to maintain than the equivalent rule.