---
title: "Advanced templates | Grafana Cloud documentation"
description: "Advanced Jinja2 template techniques for alert customization in Grafana IRM."
---

# Advanced alert template configuration

Grafana IRM uses the [Jinja templating language](http://jinja.pocoo.org/docs/2.10/) to format alert groups for various platforms such as the Web, Slack, phone calls, SMS messages, and more. This allows you to customize the presentation and content of alerts when they are triggered.

Jinja2 offers a range of functionalities, including loops, conditions, and functions, which can be used to enhance alert template customization. Every alert from a monitoring system is received in a key/value format, which Grafana IRM maps to specific fields, such as: `title`, `message`, `image`, `grouping`, and `auto-resolve`.

To learn more about mapping your alert payload to Grafana IRM fields, refer to [map payloads to Grafana IRM fields](/docs/grafana-cloud/alerting-and-irm/irm/escalation-and-routing/customize-alert-templates/#mapping-payloads-to-grafana-irm-fields).

## Loops

Monitoring systems can send an array of values. Use Jinja to iterate and format the alert payloads. For example:

.jinja2 ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```jinja2
*Values:*
 {% for evalMatch in payload.evalMatches -%}
 `{{ evalMatch['metric'] }}: '{{ evalMatch['value'] -}}'`{{ " " }}
 {%- endfor %}
```

## Conditions

Add conditional instructions based on specific alert rules. For instance, to provide instructions when an alert comes from a specific Grafana alert rule:

jinja2 ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

````jinja2
{% if  payload.ruleId == '1' -%}
*Alert TODOs*
1. Get access to the container
    ```
        kubectl port-forward service/example 3000:80
    ```
2. Check for the exception.
3. Open the container and reload caches.
4. Click Custom Button `Send to Jira`
{%- endif -%}
````

## Built-in Jinja functions

Jinja2 includes various built-in functions that can be used in Grafana OnCall. For example, to prettify JSON:

.jinja2 ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```jinja2
{{ payload | tojson_pretty }}
```

Some commonly used built-in functions include:

- `abs`
- `capitalize`
- `trim`

For a full list of Jinja built-in functions, see the [Jinja documentation on GitHub](https://github.com/pallets/jinja/blob/3915eb5c2a7e2e4d49ebdf0ecb167ea9c21c60b2/src/jinja2/filters.py#L1307)

## Variables added by Grafana IRM

There are a few extra variables available in the template context of an alert group notification:

- `grafana_oncall_alert_group_id`: The ID of the alert group
- `grafana_oncall_link`: The absolute URL to the alert group
- `integration_name`: The name of the integration where the alert group was received
- `source_link`: Source link as defined by the corresponding integration template
- `external_id`: If alert group is connected to an external resource (e.g. ServiceNow incident), this would be the ID of that resource in the external service
- `external_link`: The link to the external resource the alert group is connected to
- `labels`: Labels (key, value) assigned to the alert group

## Functions added by Grafana IRM

Grafana IRM enhances Jinja with additional functions:

- `time`: Current time
- `tojson`: Dumps a structure to JSON
- `tojson_pretty`: Same as `tojson`, but prettified
- `iso8601_to_time`: Converts ISO8601 time (`2015-02-17T18:30:20.000Z`) to datetime
- `iso8601_to_timestamp`: Converts ISO8601 time (`2015-02-17T18:30:20.000Z`) to Unix timestamp (e.g., `1713796200`). Note: microseconds are not included in the output. If you need microsecond precision, use `iso8601_to_time` instead: `{{ ("2015-02-17T18:30:20.123Z" | iso8601_to_time).timestamp() }}` should return `1424197820.123`.
- `datetimeformat`: Converts datetime to string according to strftime format codes (`%H:%M / %d-%m-%Y` by default)
- `datetimeformat_as_timezone`: Converts datetime to string with timezone conversion (`UTC` by default)
  
  - Usage example: `{{ payload.alerts.startsAt | iso8601_to_time | datetimeformat_as_timezone('%Y-%m-%dT%H:%M:%S%z', 'America/Chicago') }}`
- `datetimeparse`: Converts string to datetime according to strftime format codes (`%H:%M / %d-%m-%Y` by default)
- `timedeltaparse`: Converts a time range (e.g., `5s`, `2m`, `6h`, `3d`) to a timedelta that can be added to or subtracted from a datetime
  
  - Usage example: `{% set delta = alert.window | timedeltaparse %}{{ alert.startsAt | iso8601_to_time - delta | datetimeformat }}`
- `timestamp_to_datetime`: Converts a Unix/Epoch time to a datetime object
- `regex_replace`: Performs a regex find and replace
- `regex_match`: Performs a regex match, returns `True` or `False`
  
  - Usage example: `{{ payload.ruleName | regex_match(".*") }}`
- `regex_search`: Performs a regex search, returns `True` or `False`
  
  - Usage example: `{{ payload.message | regex_search("Severity: (High|Critical)") }}`
- `b64decode`: Performs a base64 string decode
  
  - Usage example: `{{ payload.data | b64decode }}`
- `parse_json`: Parses a JSON string to an object
  
  - Usage example: `{{ (payload.data | b64decode | parse_json).name }}`
