Script examples
Colour and label by state
Section titled “Colour and label by state”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";Hide all equipment in an area
Section titled “Hide all equipment in an area”Element names start with an area prefix.
Screen.All() .filter(function (e) { return e.Name.indexOf("Area2.") === 0; }) .forEach(function (e) { e.Visible = false; });Highlight elements with no communication
Section titled “Highlight elements with no communication”Screen.All().forEach(function (e) { if (!e.Has("Tag") || !e.Tag) return; e.Transparency = e.Quality === "Good" ? 0 : 60;});Lock buttons by mode
Section titled “Lock buttons by mode”var manual = Tags("Line.Mode").Value === 0;["StartButton", "StopButton", "Setpoint"].forEach(function (name) { Screen.Items(name).Enabled = manual;});A button with confirmation and checks
Section titled “A button with confirmation and checks”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…";Press feedback
Section titled “Press feedback”// Left press eventItem.Fill = "#2F6FB0";// Left release eventItem.Reset();Switch a widget’s tag
Section titled “Switch a widget’s tag”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;Configure a trend from a script
Section titled “Configure a trend from a script”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 });Refresh a table after writing
Section titled “Refresh a table after writing”HMI.RunServerScript("closeOrder", Item.Text);Screen.Items("OrdersTable").Reload();Add an event while running
Section titled “Add an event while running”var box = Screen.Items("Diagram");var events = box.Config.events || {};events.Click = { functions: [{ function: "ActivateScreen", args: { Screen: "Details" } }] };box.Apply({ events: events });Server: start counter
Section titled “Server: start counter”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));Server: shift log in SQLite
Section titled “Server: shift log in SQLite”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);Server: load a recipe from CSV
Section titled “Server: load a recipe from CSV”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);}Server: interlock
Section titled “Server: interlock”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");}Setpoint input with a check
Section titled “Setpoint input with a check”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);Start several pumps in turn
Section titled “Start several pumps in turn”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));An area summary in one label
Section titled “An area summary in one label”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";Select equipment with a click
Section titled “Select equipment with a click”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 clickedconst pump = Item.Parent; // the genie instanceScreen.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";Blinking without an animation
Section titled “Blinking without an animation”A Cycle event with a 500 ms period.
var blink = !blink; // a module global lasts between runs when declared in a moduleconst 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.
Server: a state-machine sequence
Section titled “Server: a state-machine sequence”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.
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.
Server: acknowledge at shift change
Section titled “Server: acknowledge at shift change”A Daily task at 08:00 acknowledges all alarms and logs it.
Server.SysFct("AcknowledgeAllAlarms");Log.Info("Alarms acknowledged at shift change");Server: recipes from a database
Section titled “Server: recipes from a database”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]);}