Skip to content

Script examples

A screen Cycle event. When the logic is simpler than animation conditions, a script can colour the element itself.

var state = Tags("Pump1.State").Value;
var pump = Screen.Items("Pump1");
pump.Children("Label").Text = HMI.TextList("States", state);
pump.Children("Body").Fill = state === 2 ? "#D32F2F" : state === 1 ? "#3C9A5F" : "#F2F3F4";

Element names start with an area prefix.

Screen.All()
.filter(function (e) { return e.Name.indexOf("Area2.") === 0; })
.forEach(function (e) { e.Visible = false; });
Screen.All().forEach(function (e) {
if (!e.Has("Tag") || !e.Tag) return;
e.Transparency = e.Quality === "Good" ? 0 : 60;
});
var manual = Tags("Line.Mode").Value === 0;
["StartButton", "StopButton", "Setpoint"].forEach(function (name) {
Screen.Items(name).Enabled = manual;
});

The question comes first because a browser runs the script again after it.

if (!HMI.Confirm("Start the pump?")) return;
var fault = Tags("Pump1.Fault");
if (fault.Quality !== "Good" || fault.Value) {
HMI.ShowMessage("Start blocked: fault or no communication");
return;
}
Tags("Pump1.Start").Write(true);
Item.Text = "Starting…";
// Left press event
Item.Fill = "#2F6FB0";
// Left release event
Item.Reset();

One gauge shows the selected pump.

var pump = Screen.Items("Selector").Value;
Screen.Items("Gauge").Tag = "Pump" + pump + ".Speed";
Screen.Items("Label").Text = "Pump " + pump;
var pens = ["Tank1.Level", "Tank2.Level", "Tank3.Level"].map(function (tag) {
return { tag: tag };
});
var trend = Screen.Items("Trend");
trend.RangeMinutes = 480;
trend.Apply({ penList: pens });
HMI.RunServerScript("closeOrder", Item.Text);
Screen.Items("OrdersTable").Reload();
var box = Screen.Items("Diagram");
var events = box.Config.events || {};
events.Click = { functions: [{ function: "ActivateScreen", args: { Screen: "Details" } }] };
box.Apply({ events: events });

A Condition task: Pump1.Run = true.

var starts = Tags("Pump1.Starts").Value || 0;
Tags("Pump1.Starts").Write(starts + 1);
Log.Info("Start no. " + (starts + 1));

A Daily task at 08:00.

var total = Tags("Line.Count").Value;
Db.Execute("Journal", "INSERT INTO Shift(Time, Total) VALUES(?, ?)", [Server.Now, total]);
Tags("Line.Count").Write(0);
function loadRecipe(name) {
var rows = File.ReadCsv("recipes/" + name + ".csv");
rows.slice(1).forEach(function (row) {
Tags(row[0]).Write(Number(row[1]));
});
Log.Info(Trigger.User + " loaded recipe " + name);
}

A Tag change task on Tank1.Level.

if (Trigger.Value === null) return;
if (Trigger.Value > 9 && Tags("Pump1.Start").Value) {
Tags("Pump1.Start").Write(false);
Log.Warn("Pump stopped: high level");
}

A Set setpoint button asks for confirmation when the value differs a lot from the current one.

const sp = Tags("Tank1.Setpoint");
const wanted = Number(Screen.Items("SetpointInput").Text);
if (!Number.isFinite(wanted) || wanted < 0 || wanted > 10) {
HMI.ShowMessage("The setpoint must be between 0 and 10 m");
return;
}
if (Math.abs(wanted - sp.Value) > 2 && !HMI.Confirm(`Change the setpoint from ${sp.Value} to ${wanted} m?`)) return;
sp.Write(wanted);

Writes run in order, so the commands go out in the order they are written.

if (!HMI.Confirm("Start pumps 1–3?")) return;
["Pump1", "Pump2", "Pump3"]
.filter(p => !Tags(p + ".Fault").Value)
.forEach(p => Tags(p + ".Start").Write(true));

A screen Cycle event with a 1000 ms period.

const pumps = ["Pump1", "Pump2", "Pump3", "Pump4"];
const run = pumps.filter(p => Tags(p + ".Run").Value === true).length;
const fault = pumps.filter(p => Tags(p + ".Fault").Value === true).length;
const label = Screen.Items("Summary");
label.Text = `Running ${run} of ${pumps.length}` + (fault ? `, ${fault} in fault` : "");
label.Foreground = fault ? "#D32F2F" : "#1E2328";

Every pump on the screen is an instance of one genie. Clicking a genie remembers the choice and highlights its frame, and a side panel shows the selected pump’s tags.

// Click event inside the genie: Item is the element that was clicked
const pump = Item.Parent; // the genie instance
Screen.All()
.filter(e => e.Type === "Group" && e.Name.indexOf("Pump") === 0)
.forEach(g => g.Children("Frame").Visible = g.Name === pump.Name);
Screen.Items("PanelSpeed").Tag = pump.Name + ".Speed";
Screen.Items("PanelHours").Tag = pump.Name + ".Hours";

A Cycle event with a 500 ms period.

var blink = !blink; // a module global lasts between runs when declared in a module
const lamp = Screen.Items("Horn");
lamp.Visible = Tags("Alarm.Horn").Value ? blink : true;

The Appearance animation with its blink flag is usually more convenient, as it blinks all elements in step.

An Interval task every 0.5 s. The step lives in the internal tag Seq.Step with Retain set, so the sequence continues after a restart.

serverscripts/Sequence.js
function sequenceTick() {
const step = Tags("Seq.Step").Value || 0;
switch (step) {
case 0: // wait for a command
if (Tags("Seq.Start").Value) {
Tags("Seq.Start").Write(false);
Tags("Valve1.Open").Write(true);
Tags("Seq.Step").Write(1);
}
break;
case 1: // filling
if (Tags("Tank1.Level").Value >= Tags("Tank1.Setpoint").Value) {
Tags("Valve1.Open").Write(false);
Tags("Mixer.Run").Write(true);
Tags("Seq.Started").Write(Server.Now);
Tags("Seq.Step").Write(2);
}
break;
case 2: // mix for 60 s
if (Date.now() - Date.parse(Tags("Seq.Started").Value) > 60000) {
Tags("Mixer.Run").Write(false);
Tags("Seq.Step").Write(0);
Log.Info("Batch finished");
}
break;
}
}

Set the task’s action to Run script function with sequenceTick.

A Daily task at 08:00 acknowledges all alarms and logs it.

Server.SysFct("AcknowledgeAllAlarms");
Log.Info("Alarms acknowledged at shift change");

A function for a Load recipe button on a screen: HMI.RunServerScript("loadRecipeFromDb", "Juice").

function loadRecipeFromDb(name) {
const rows = Db.Query("Recipes", "SELECT Tag, Value FROM RecipeItems WHERE Recipe = ?", [name]);
if (rows.length === 0) throw new Error(`recipe ${name} not found`);
rows.forEach(r => Tags(r.Tag).Write(r.Value));
Db.Execute("Recipes", "INSERT INTO Loads(Time, Recipe, UserName) VALUES(?, ?, ?)", [Server.Now, name, Trigger.User]);
}