> For the complete documentation index, see [llms.txt](https://docs.monokot.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.monokot.io/scripts/programming-examples/how-to-calculate-arithmetic-mean.md).

# 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:

```javascript
// 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.

![](/files/qxE89KEwz7WPBTMQqrHp)

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.

![](/files/rd47UJRw0ujQ95jUNVkE)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.monokot.io/scripts/programming-examples/how-to-calculate-arithmetic-mean.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
