Server script examples
These examples show the usual pattern of a server script: read field signals, decide, and write the result to an internal memory signal. Alarms, screens and the history then use that internal signal like any other: acknowledgement, the alarm list and the event list work without any script code.
Before you begin
- You know how to create and save a server script; see Server scripts.
- The field signals of the examples exist (
Motor1.Temperature;Pump1.InletPressure,Pump1.Running), or you replace them with your own.
Steps
1. Create the internal signals
In the configuration editor, create internal memory signals of the type on/off: Motor1.TempHigh and
Motor1.TempVeryHigh for the first example, Pump1.LowPressureLock for the second. A script may always write to
internal memory signals; no command permission is needed.
2. Motor temperature warning
The script runs whenever the temperature changes, ignores values of bad quality and sets two levels:
defineScript({
tags: ["Motor1.Temperature"],
onChange(ctx, path) {
if (ctx.quality(path) !== "good") {
ctx.warn("quality not good: " + path + ", skipped");
return;
}
const v = Number(ctx.value(path));
const veryHigh = v > 85;
const high = !veryHigh && v > 70; // high only for 70 < v <= 85
ctx.setTag("Motor1.TempVeryHigh", veryHigh);
ctx.setTag("Motor1.TempHigh", high);
if (veryHigh) ctx.log("Temperature CRITICAL: " + v + " °C");
else if (high) ctx.log("Temperature HIGH: " + v + " °C");
},
});
3. Define the alarms
Define an alarm on Motor1.TempHigh (for example priority high) and on Motor1.TempVeryHigh (priority critical), as
for any on/off signal; see Define alarms. The script decides, the alarm engine reports.
4. Pump low-pressure interlock
The script waits until both inputs have a value and sets the lock while the pump runs with too low inlet pressure:
const PRESSURE = "Pump1.InletPressure";
const RUNNING = "Pump1.Running";
defineScript({
tags: [PRESSURE, RUNNING],
onChange(ctx) {
const p = ctx.value(PRESSURE);
const run = ctx.value(RUNNING);
if (p === undefined || p === null || run === undefined || run === null) return; // both inputs needed
const lock = Number(run) > 0.5 && Number(p) < 1.5;
ctx.setTag("Pump1.LowPressureLock", lock);
if (lock) ctx.warn("Pump running with low inlet pressure: " + p + " bar");
},
});
Use Pump1.LowPressureLock in an alarm, on the screen, or as a condition for the pump command. If the script should stop
the pump itself, it needs Can command field points with a narrow target pattern such as Pump1.Stop; see
Writing.
5. Try and activate
For each script, use Run now / try with the trigger onChange: the result lists the values the script would write, without writing them. Then switch Active on and click Compile and save.
Result
The internal signals follow the field values; the alarms come and go with them. The Log and status tab of each script shows its log lines and its run and error counts.
Good to know
- Keep the decision in the script and the reaction in the alarm definition: the alarm then has its texts, priority, acknowledgement and history without extra code.
- To load these scripts into several projects at once, save them as
.jsfiles and use script import.