Skip to content

Script API

Script API schedules / unschedules other scripts, runs them on demand, queries their status, and shares data between scripts. The shared data layer is Redis-backed and project-scoped.

Returns a script’s definition — RepeatableScriptResponseDto.

var s = ins.getScript("HourlyReport");
ins.consoleLog(s.getName() + " type=" + s.getType() + " period=" + s.getPeriod() + "ms");

Fields:

MethodTypeDescription
getName() / getDsc()StringScript name and description
getCode()StringJavaScript source
getType()ScheduleTypePERIODIC / DAILY / ONCE / NONE
getPeriod()IntegerPeriod (ms) for periodic scripts
getOffset()IntegerOffset (ms) for periodic scripts
getDelay()IntegerDelay (ms) for once-type scripts
getTime()DateTime for daily scripts
getLog()BooleanWhether execution logging is on
getProjectId()StringProject ID

ins.getScriptStatus(name) / (projectName, name)

Section titled “ins.getScriptStatus(name) / (projectName, name)”

Returns a ScriptStatus enum — two values:

ValueMeaning
"Scheduled"Attached to the scheduler
"Not Scheduled"Not attached (can be run manually)
if (ins.getScriptStatus("HourlyReport") == "Not Scheduled") {
ins.scheduleScript("HourlyReport");
}

ins.scheduleScript(name) / (projectName, name)

Section titled “ins.scheduleScript(name) / (projectName, name)”

Adds the script to the scheduler according to its ScheduleType. Has no effect on scripts with type=NONE.

ins.scheduleScript("HourlyReport");

ins.cancelScript(name) / (projectName, name)

Section titled “ins.cancelScript(name) / (projectName, name)”

Removes the script from the scheduler — future triggers are cancelled, but an execution that is currently running is not interrupted (it runs to completion).

ins.cancelScript("HourlyReport");

ins.executeScript(name) / (projectName, name)

Section titled “ins.executeScript(name) / (projectName, name)”

Runs the script immediately and returns its return value. Synchronous — blocks until the script finishes.

var dailyTotal = ins.executeScript("Calculate_DailyTotal");
ins.setVariableValue("DailyProduction_kWh", { value: dailyTotal });

Scripts are isolated from each other (no shared memory). To bridge this, setGlobalObject / getGlobalObject provide a Redis-backed key-value store. Keys are project-scoped (project:<projectId>:global-object:<name>).

Stores the object indefinitely (no TTL).

ins.setGlobalObject("daily_counter", 42);
ins.setGlobalObject("shift_data", {
shift: "A",
count: 150,
startTime: ins.now().toString()
});

Stores the object with a TTL — auto-deleted after ms milliseconds (Redis SET PX).

// Cache that vanishes after 60 seconds
ins.setGlobalObject("temp_cache", { value: 99 }, 60000);

Returns the object, or null if missing. Does not touch the TTL.

var counter = ins.getGlobalObject("daily_counter");
// → 42 or null

Returns the object and resets the TTL to ms (sliding TTL — Redis PEXPIRE). Typical use: “Each read keeps the object alive.”

// Session-like data: each read extends liveness by 5 minutes
var session = ins.getGlobalObject("user_session", 5 * 60 * 1000);
// Backend script (every 10 s):
function main() {
ins.setGlobalObject("dashboard_summary", {
power: ins.getVariableValue("ActivePower_kW").value,
voltage: ins.getVariableValue("Voltage_V").value,
updatedAt: ins.now().getTime()
}, 30000); // 30s TTL — don't let it go stale
}
main();

A custom HTML widget on the UI side can read the same key via client-side Inscada.*.

function main() {
var last = ins.getGlobalObject("report_last_run");
var now = ins.now().getTime();
if (last && (now - last) < 3600000) {
ins.consoleLog("Less than 1 hour, skipping");
return;
}
ins.executeScript("generate_hourly_report");
ins.setGlobalObject("report_last_run", now);
}
main();
function main() {
if (ins.getGlobalObject("maintenance_mode")) {
ins.consoleLog("Maintenance mode on — script skipped");
return;
}
// normal work...
}
main();