Scripts
Write your own computations in Python or TypeScript, version them, try them out and run them as a component – in an isolated environment.
Overview#
A script is a component you write yourself in Python or TypeScript. It belongs to your tenant, has immutable versions and appears in the library under the classification "Script". A script runs like any other component: you set it up, bind its ports to nodes and start or stop it.
Scripts are stateless: whatever a run needs to know comes in through inputs, and results leave through outputs. The state of a run (for example the window of a moving average) lives only as long as the run.
You open scripts from the Components view (group Library, scripts first) or from the Workflows view (section Scripts).
Creating a script#
- In the Components view, open the ⋮ menu and choose New …. (In a workflow, New … is in the toolbar.)
- Choose the Write a script tile. You need the "Create scripts" permission on the tenant.
- Enter the name and choose the language (default: Python).
- Confirm. The script is created from a template as version 1.0.0 and its tab opens pinned.
Name and language cannot be changed later. The name is unique within the tenant, ignoring case, and consists of letters, digits, _ and -.
If created from a workflow, New … also creates a component and a box on the canvas; in the Components view only the script is created.
The script tab#
The tab has these sections:
| Section | Content |
|---|---|
| Overview | Description, ports and the versions. |
| Source | The editor with highlighting for the language, completion and help. |
| Ports | Table of inputs and outputs as read from the header line, with the change against the last version and the reasons. |
| Versions | List of all versions, the source of a version and a line-by-line comparison with the current one. |
| Try out | Test run of the latest saved version, see below. |
| In operation | Components of your tenant that use this script. |
| Permissions | Permissions on the script. |
The header shows the name, the classification "Script" with the language, Set up … (turns the script into a component) and Delete script. The footer shows the saved state of the latest version and the buttons Try out and Save as new version.
A dot on the tab marks unsaved changes. When you close the tab, the workbench asks whether to save as a new version, discard or cancel.
Help in the editor#
The editor knows the language: it highlights keywords and header lines, completes ctx accesses and the port names from the header line, and offers snippets that you insert with Tab. The Help button in the editor footer or F1 shows a help panel beside the editor. It follows the cursor and shows the matching statement, ports or ctx members for the current line.
Structure of a script#
The description is a comment at the start of the source, the header line. LooT reads it without running the script. The comment character is # in Python and // in TypeScript.
| Line | Meaning |
|---|---|
loot:mode job or loot:mode reactive | Required, exactly once: the mode of the script |
loot:description text | Description (once) |
loot:input name type [required] [nullable] [default=value] [-- description] | An input; type float, integer, bool, string or dynamic |
loot:output name type [nullable] [-- description] | An output |
loot:max-duration seconds | Deadline per run |
required and default exclude each other; nullable allows gaps (null). The header ends with the first line of code (in TypeScript, before the first import).
The code defines the function run(ctx). The ctx object is the same in both languages, in the spelling of the language (Python snake_case, TypeScript camelCase):
| Purpose | Python | TypeScript |
|---|---|---|
| Entry point | def run(ctx): | export function run(ctx) or export async function run(ctx) |
| Latest value | ctx.inputs.get(name) | ctx.inputs.get(name) |
| Value present | ctx.inputs.has(name) | ctx.inputs.has(name) |
| Values in order | for item in ctx.inputs.follow(name) | for await (const item of ctx.inputs.follow(name)) |
| Snapshot | ctx.inputs.sequence(name) | ctx.inputs.sequence(name) |
| Write an output | ctx.outputs.write(port, value, time) | ctx.outputs.write(port, value, time) |
| Log | ctx.log.info(text) | ctx.log.info(text) |
| Error and abort | ctx.error(text) | ctx.error(text) |
| Check for stop | ctx.stop_requested, ctx.sleep(s) | ctx.stopRequested, await ctx.sleep(s) |
Example: a reactive script that raises an alarm as soon as the measured value reaches the threshold. Gaps in the input stay gaps in the output.
# loot:mode reactive
# loot:input value float required nullable -- measured value
# loot:input threshold float default=10
# loot:output alarm bool nullable
def run(ctx):
for item in ctx.inputs.follow("value"):
if item.value is None:
ctx.outputs.write("alarm", None, item.timestamp)
else:
ctx.outputs.write("alarm", item.value >= ctx.inputs.get("threshold"), item.timestamp)The same in TypeScript:
// loot:mode reactive
// loot:input value float required nullable -- measured value
// loot:input threshold float default=10
// loot:output alarm bool nullable
import type { Context } from "loot";
export async function run(ctx: Context): Promise<void> {
for await (const item of ctx.inputs.follow("value")) {
ctx.outputs.write("alarm", item.value === null ? null : item.value >= ctx.inputs.get("threshold"), item.timestamp);
}
}If you bind value in the component to a node such as Brewery/Plant.Line1.Temperature and alarm to a bool node, the script writes the alarm there continuously.
The languages in detail#
- Python (version 3.13) ships with
numpy,scipyandpandas;import lootis not needed. Everyimportcosts start-up time in every run (numpyabout half a second,scipy.signalseveral seconds); in frequently running job scripts, import only what you need. - TypeScript (Node 24) only strips the types; it does not check them.
enum,namespacewith content and parameter properties do not work. Import types fromlootonly withimport type. Available packages arearquero,simple-statistics,d3-array,ml-matrix,fft.jsanddate-fns. A long, purely synchronous computation does not see the stop; wait withawait ctx.sleep(…).
Versions#
Every save with changed source creates a new, immutable version; the first is 1.0.0. Saving without a change creates none. The server assigns the version number according to the changes to the ports:
| Change | New version |
|---|---|
| Ports unchanged (only code, description or deadline) | Patch (1.0.0 → 1.0.1) |
| New optional input, new output, different default value, relaxed input | Minor (1.0.1 → 1.1.0) |
| Mode changed, port removed, type changed, stricter input, new required input without default value | Major (1.1.0 → 2.0.0) |
While you type, the tab shows which version saving would assign; the Ports section names the reasons. Components stay on their version until you switch them (see Components). A script box in a workflow shows "x.y.z available" when a newer version exists.
Trying out#
The Try out section starts a test run of the latest saved version; you cannot test an intermediate draft this way. With Save and test you first save as a new version and then start. A test run writes nothing into nodes.
- For job scripts, enter the inputs.
- For reactive scripts, also give an input sequence (one value per line, at most 1000) and a deadline in seconds (default 10, at most 120). After the last value, at the latest after the deadline, the test run stops by itself.
The result shows the state, outputs, error and log. Errors in the script appear with the line ("Line 7: …"); Go to line jumps there in the editor. A test run needs the Write permission on the script.
Limits and sandbox#
Every run starts in its own isolated environment (sandbox): without network, with a read-only file system, without credentials and with limited CPU, memory, number of processes and files. The script sees only its input values; LooT data and tokens never reach it. The server writes the outputs with the permissions of the person who created the binding.
| Limit | Value |
|---|---|
| Size of the source | at most 256 KiB |
| Ports | at most 64 per side (inputs/outputs) |
| Deadline per run | max-duration (1 second to 30 days), at most the operator's upper limit: by default 60 s for jobs and 24 h for reactive runs; longer values are cut |
| Memory per run | 128 MiB by default (operators can change it) |
| Input queue | 1000 values |
If a run exceeds the deadline or the limits, it ends with an error message. A script that cannot run because no runner is registered for its language or the sandbox does not start reports this as a run error; contact your operator in that case.
Permissions#
| Permission | Granted on | Allows |
|---|---|---|
| Create scripts | the tenant | creating scripts |
| Read | the script | seeing source, versions and ports |
| Write | the script | changing the source (new version), test runs |
| Manage | the script | granting permissions, deleting the script |
Whoever creates a script has all permissions on it; so do tenant admins and system admins. Running belongs to the component: someone who may only start a component using the script does not see the source. Name, ports and version of the script are visible to all members of the tenant in the library.
Delete script (Manage permission) removes the script with all versions and its test runs. While a component uses it or a run is active, the server rejects the deletion and names the number.
You can also manage scripts from the command line.