# AverageTimeCal

## `AverageTimeCal` (FB)

FUNCTION\_BLOCK AverageTimeCal

### Short description

> Calculation of the average of an analog value over four different time periods\
> Averaging: Weighted average (see formula below)\
> Typical application: Temporal averaging of the outside temperature

### Representation

<figure><img src="https://2592874069-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQR4vCFzt6ixgsqlR3pdR%2Fuploads%2Fgit-blob-b771e5b648e33999caf8de1820d3f4a07d64e219%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

### Interfaces

#### Inputs

| Name  | Data type | Value range | Initial value | Function           |
| ----- | --------- | ----------- | ------------- | ------------------ |
| *rIn* | REAL      |             |               | Analog value input |

#### Outputs

| Name             | Data type       | Value range | Initial value | Function                                  |
| ---------------- | --------------- | ----------- | ------------- | ----------------------------------------- |
| *rOutCH0*        | REAL            |             |               | Average value period 1                    |
| *rOutCH1*        | REAL            |             |               | Average value period 2                    |
| *rOutCH2*        | REAL            |             |               | Average value period 3                    |
| *rOutCH3*        | REAL            |             |               | Average value period 4                    |
| *rOutMin*        | REAL            |             |               | Minimum of all measurements               |
| *rOutMax*        | REAL            |             |               | Maximum of all measurements               |
| *dtLastNewValue* | DATE\_AND\_TIME |             |               | Storage timestamp of the last measurement |
| *sErr*           | STRING          |             |               | Current error text                        |

#### Setpoints / parameters

| Name              | Data type | Value range | Initial value    | Function                                                                                                             |
| ----------------- | --------- | ----------- | ---------------- | -------------------------------------------------------------------------------------------------------------------- |
| **xCalculation**  | BOOL      |             | TRUE             | Enable / disable data recording                                                                                      |
| **xResetOnInput** | BOOL      |             | FALSE            | Initialization of the measurement storage and the average value outputs with *rIn*                                   |
| **rInitValue**    | REAL      |             | 10               | Initialization value for the measurement storage and the average value outputs when **eInitial** = eInit2.InitValue. |
| **tInitDelay**    | TIME      |             | 5s               | Delay time for measurement storage when **eInitial** = eInit2.InputValueDelayed.                                     |
| **eInitial**      | eInit2    |             | eInit2.LastValue | Specification of the type of initialization                                                                          |
| **wCH3Time**      | WORD      |             | 48               | Definition of period 4 in hours                                                                                      |
| **wCH2Time**      | WORD      |             | 36               | Definition of period 3 in hours                                                                                      |
| **wCH1Time**      | WORD      |             | 24               | Definition of period 2 in hours                                                                                      |
| **wCH0Time**      | WORD      |             | 12               | Definition of period 1 in hours                                                                                      |
| **wInterval**     | WORD      |             | 15               | Definition of the recording interval in minutes                                                                      |

### Function description

#### General

Averaging\
\
The value at the analog input *rIn* is captured recording interval **iInterval** (Default = 15 minutes) is recorded.\
To calculate the average of a period X (X = 0 … 3), three factors are determined for each period.\
\
\
Factor 1 - Period X: Total number of measured values to be considered\
\
Factor 1 = ( **iCHXTime** \* 60 ) / **iInterval**\
\
\
Factor 2 - Period X: Factor for the stored average\
\
Factor 2 = ( Factor 1 - 1 ) / Factor 1\
\
\
Factor 3 - Period X: Factor for the current value at the analog input *rIn*\
\
Factor 3 = 1 - Factor 2\
\
\
The average of each period is calculated at the recording interval **iInterval** according to the following formula and transferred to the average value outputs:\
\
\
Average = (Old average \* Factor 2) + (*rIn* \* Factor 3)\
\
\
Legend: X = 0 … 3

> **Example**
>
> **iInterval** = 15 minutes, **wCH0Time** = 12 hours, Old average = 20.0, *rIn* = 22.0\
> \
> Factor 1 = 48\
> \
> Factor 2 = 47 / 48\
> \
> Factor 3 = 1 / 48\
> \
> Average = 20.041

#### Average value outputs *rOutCHX* (X = 0 … 3)

At the average value output *rOutCHX* (X = 0 … 3) the correct arithmetic mean of the\
corresponding entries in the measurement value storage is available for the first time after the duration **iCHXTime** the average of the captured input values is available.

#### Minimum value output *rOutMin*

At the minimum value output *rOutMin* is available for the first time after one recording interval **iInterval** the absolute minimum of all captured input values is available.

#### Maximum value output *rOutMax*

At the maximum value output *rOutMax* is available for the first time after one recording interval **iInterval** the absolute maximum of all captured input values is available.

#### Timestamp *dtLastNewValue*

At the timestamp output *dtLastNewValue* is available for the first time after one recording interval **iInterval** the timestamp of the last entry in the measured value memory is available.

#### Error text *sErr*

At the output for the current error text *sErr* a descriptive text is available in case of error. If there is no error, an empty string is output.

#### Type of initialization **eInitial**

This setpoint / parameter determines the type of initialization of the measurement storage and the average value outputs. The following options are possible:

| Type                     | Initialization value                                                   | Note                       |
| ------------------------ | ---------------------------------------------------------------------- | -------------------------- |
| eInit2.InitValue         | **rInitValue**                                                         | Configurable initial value |
| eInit2.InputValue        | *rIn*                                                                  | Input value                |
| eInit2.InputValueDelayed | *rIn*                                                                  | Delayed input value        |
| eInit2.LastValue         | Average - measurement value storage (measurement value storage filled) | Last value                 |
| eInit2.LastValue         | *rIn* (measurement value storage empty)                                | Last value                 |

#### Enable / disable data recording **xCalculation**

This setpoint / parameter enables (= TRUE) or disables (= FALSE) data recording

#### Reset of the measurement value storage / the average value outputs **xResetOnInput**

With this setpoint / parameter the measured value memory / the average value outputs are set to the value **rIn** reset (= TRUE).

### CODESYS

InOut:

| Scope       | Name             | Type     | Initial          |
| ----------- | ---------------- | -------- | ---------------- |
| Input       | `rIn`            | `REAL`   |                  |
| Input Const | `xCalculation`   | `BOOL`   | TRUE             |
| Input Const | `xResetOnInput`  | `BOOL`   | FALSE            |
| Input Const | `rInitValue`     | `REAL`   | 10               |
| Input Const | `tInitDelay`     | `TIME`   | TIME#5s0ms       |
| Input Const | `eInitial`       | eInit2   | eInit2.LastValue |
| Input Const | `wCH3Time`       | `WORD`   | 48               |
| Input Const | `wCH2Time`       | `WORD`   | 36               |
| Input Const | `wCH1Time`       | `WORD`   | 24               |
| Input Const | `wCH0Time`       | `WORD`   | 12               |
| Input Const | `wInterval`      | `WORD`   | 15               |
| Output      | `rOutCH0`        | `REAL`   |                  |
| Output      | `rOutCH1`        | `REAL`   |                  |
| Output      | `rOutCH2`        | `REAL`   |                  |
| Output      | `rOutCH3`        | `REAL`   |                  |
| Output      | `rOutMin`        | `REAL`   |                  |
| Output      | `rOutMax`        | `REAL`   |                  |
| Output      | `dtLastNewValue` | `DT`     |                  |
| Output      | `sErr`           | `STRING` |                  |
