# 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](/platform-manual/rules/rule-language/functions.md#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](/platform-manual/rules/rule-language/functions.md#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.](/platform-manual/rules/rule-language/functions.md#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](/platform-manual/rules/rule-language/operations.md#greater-greater-than) between them:

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


---

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

```
GET https://doc.moost.io/platform-manual/rules/rule-language/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
