# Examples

## SUBSETS

Subsets are often used to select a certain time period from a  timeseries.

For example, we want to send a message if the <mark style="background-color:purple;">minimum</mark> of the <mark style="background-color:blue;">newest two entries</mark> of a <mark style="background-color:orange;">time series of 5 days in Grid Power Consumption</mark> is greater than the <mark style="background-color:purple;">minimum</mark> of the <mark style="background-color:blue;">remaining time series</mark>.

[As described on the SUBSET chapter of the Functions page](https://doc.moost.io/platform-manual/rules/functions#subset-vector-less-than-any-greater-than-scalar-less-than-number-greater-than), we simply can add the <mark style="background-color:blue;">number of entries</mark> we want to extract from a vector, as long as they are at the beginning or end of the vector. As we want the <mark style="background-color:blue;">most recent entries</mark>, we must precede that value with a <mark style="background-color:blue;">minus</mark> sign.

```java
MIN(SUBSET($GridPowerConsumption5d, -2))
```

We now have a snippet of the two newest entries from the original time series. By adding the [<mark style="background-color:purple;">MIN</mark> function](https://doc.moost.io/platform-manual/rules/functions#min-vector-less-than-number-event-greater-than), we end up with the lower value of those two.

Alternatively, we can also take the newest entries in a certain time period. Here we want to get the minimum of all registered Grid Power Consumption events from the last 30 minutes until now.

[Now is always defined as the newest entry and is included in the subset, but the start time is excluded.](https://doc.moost.io/platform-manual/rules/functions#subset-vector-less-than-event-greater-than-scalar-less-than-time-greater-than-scalar-less-than-time) So we would need to add an additional minute (or at least some seconds) to include the event that occurred exactly 30 minutes ago.&#x20;

```java
MIN(SUBSET($GridPowerConsumption5d, now-31min, now)) 
```

Let us continue with the first version. We now want to compare this minimum value with the <mark style="background-color:purple;">minimum</mark> of the rest of the <mark style="background-color:orange;">time series</mark>.

The beginning of this snippet is therefore the <mark style="background-color:blue;">first value of the vector at position 0</mark> up to and <mark style="background-color:blue;">including the third most recent event</mark>. As before, we can get the <mark style="background-color:purple;">minimum</mark> by applying a <mark style="background-color:purple;">MIN</mark> function to the resulting subset.

```java
MIN(SUBSET($GridPowerConsumption5d, 0, -3))
```

Finally, we simply have to combine the two parts for our condition by adding a [Greater operator](https://doc.moost.io/platform-manual/rules/operations#greater-greater-than) between them:

```java
MIN(SUBSET($GridPowerConsumption5d, -2)) > MIN(SUBSET($GridPowerConsumption5d, 0, -3))
```
