Script Engine
The Script Engine lets users run JavaScript to automate reactions to data, schedule periodic tasks, and extend the platform without modifying the core. Scripts are stored per project and executed in a sandboxed runtime.
Interface Screenshot

Captured from the live Smart Site system with the English UI. See the
zh-CNcounterpart for the Chinese-UI screenshot.
1. Core Concepts
- Script — A JavaScript snippet with metadata (name, trigger type, enabled flag)
- Execution — A single run of a script, recorded in
script_executionswith status, logs, and duration - Trigger — What starts an execution: cron schedule, event binding, or manual run
- ScriptAPI — The injected surface a script can call (read tags, write tags, emit events, log)
The engine is implemented in @ss-node/ss-script and integrates with the EventBus and the scheduler.
2. Script Editor
The built-in editor (System → Scripts) provides:
- Syntax highlighting — Monaco-based editor
- Auto-completion — for the ScriptAPI surface and known tag names
- Inline validation — syntax errors are flagged before save
- Test run — execute the script once with a mock context and view logs
- Version history — previous script bodies are kept for rollback
Scripts are saved as text in the scripts table along with their trigger configuration.
3. Trigger Types
3.1 Cron Scheduling
Scripts can run on a cron schedule using the @nestjs/schedule integration:
{
"trigger": "cron",
"cron": "*/5 * * * *"
}
Use cases: periodic reports, data rollups, health checks, cleanup tasks.
3.2 Event Binding
Scripts can subscribe to EventBus topics:
{
"trigger": "event",
"event": "alarm.raised"
}
The script receives the event payload as its first argument. Use cases: custom alarm handling, notification chaining, derived calculations.
3.3 Manual Run
Any script can be executed on demand from the editor's Run button or via POST /api/v1/scripts/:id/run. The result and logs are returned synchronously for short scripts.
4. Sandbox Execution
Scripts run inside a restricted sandbox that:
- Removes access to
require,process, and filesystem APIs - Injects a curated
ScriptAPIobject as the global context - Enforces a timeout (default 5 seconds) to prevent runaway scripts
- Captures
console.logoutput into the execution record
The sandbox prevents scripts from accidentally (or maliciously) affecting the host process while still allowing useful automation.
5. ScriptAPI
The injected API surface includes:
| Namespace | Methods |
|---|---|
tags |
read(tagId), readMany(tagIds[]), write(tagId, value) |
devices |
get(deviceId), list(), ping(deviceId) |
alarms |
ack(alarmId), list({ status }) |
events |
emit(eventName, payload), on(eventName, fn) |
http |
get(url), post(url, body) (allow-listed) |
storage |
get(key), set(key, value) (per-script KV) |
log |
info(), warn(), error() |
time |
now(), format(date, pattern) |
Example script:
// Auto-acknowledge low-severity alarms at night
const alarms = await alarms.list({ status: 'active' })
for (const a of alarms) {
if (a.severity === 'low' && time.now().getHours() < 6) {
await alarms.ack(a.id)
log.info(`Auto-acked alarm ${a.id}`)
}
}
6. Script Templates
The editor ships with starter templates:
- Threshold notification — read a tag and emit a custom event when it crosses a limit
- Daily report — query historical data and post to a webhook
- Alarm escalation — re-notify if an alarm is unacknowledged after N minutes
- Data rollup — compute hourly averages and store back as a synthetic tag
Templates are loaded from the script package and can be customized per project.
7. Operational Guidance
7.1 Debugging a Failing Script
- Open the script in the editor
- Click Test Run — the output panel shows logs and any thrown errors
- Add
log.info(...)statements to inspect intermediate values - Check the
script_executionstable for the latest run's status and duration - If the script times out, refactor to do less work per run or split into multiple scripts
7.2 Performance Considerations
- Keep scripts short — they block the scheduler slot while running
- Avoid
tags.readin a tight loop; usetags.readManyonce - Long HTTP calls should be moved to a webhook receiver, not polled from a script
8. FAQ
Q: My cron script did not run at the expected time.
A: Cron uses server local time. Verify the server timezone and the cron expression. Check the script_executions table for the last run timestamp.
Q: The script cannot call fetch.
A: Network access is restricted to the http namespace with an allow-list. Ask an administrator to add the target domain to the allow-list in system settings.
Q: A script exceeded the timeout. A: The default 5-second limit protects the scheduler. Optimize the script or request a longer timeout for that specific script via system config.
Q: Can scripts write to InfluxDB directly?
A: No. Use storage.set for per-script key-value data, or tags.write to update a synthetic tag that the pipeline will persist. Direct InfluxDB access is not exposed to scripts.
9. Related Links
../../06-alarm/README.md../../09-websocket/README.md../../16-report/README.md- Script Package