How to: Calculate Arithmetic Mean

The guide demonstrates how to create an expression that calculates the arithmetic mean between two tags. Before starting, create a Modbus device and two numeric tags named source_tag1 and source_tag2 respectively.

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

// Getting the value of the first tag
var tag1 = context.data['tag1_trig'].state.value;

// Getting the value of the second tag
var tag2 = context.data['tag2_trig'].state.value;

return (tag1 + tag2) / 2;

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

Go to the Triggers tab and click Add Data.... Select the tag created earlier (source_tag1) as the data source for the trigger. Enter the name (key) tag1_trig for the trigger. In the same way, create a data trigger for the second tag.

In order for the changes to take effect on the server, click the Sync button. Now, each time one of the tags is updated, the expression will be evaluated. You can see the result of the expression in the Go Online mode or read it as an OPC UA variable.

Last updated