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");}