How to: Writing to Tag

The guide demonstrates how to create an expression that writes the control signal if the threshold value is exceeded or decreased. Before starting, create a Modbus device and two tags named monitoring_tag (WORD, 16-bit unsigned integer) and control_tag (BOOLEAN).

Open Monokot Server Administrator and double-click Scripts on the Server Explorer pane. Click New Expression and set the name Writer for the expression that appears. Insert the following code into the expression code editor:

// Declaring a limit as a constant
const LIMIT = 100;

// Getting the tag value
var value =['monitoring_tag_trig'].state.value;

if (value > LIMIT)
  return MosUtils.writeTagValueSync('control_tag', true);
  return MosUtils.writeTagValueSync('control_tag', false);

Press Ctrl + Enter to apply the change in the code editor.

Go to the Triggers tab and click Add Data.... Select the tag monitoring_tag created earlier as the data source for the trigger. Enter the name (key) monitoring_tag_trig for the trigger.

In order for the changes to take effect on the server, click the Sync button. Thus, the expression will be executed each time the monitoring_tag is changed. If monitoring_tag exceeds 100, control_tag will be set to True, otherwise False.

You can read more about the MosUtils object here.

Last updated