# Template Variables in Grafana managed alert

**URL:** <https://community.grafana.com/t/template-variables-in-grafana-managed-alert/54622>\
**Category:** Alerting\
**Tags:** templating, alerting\
**Created:** [October 12, 2021, 10:58am UTC](https://community.grafana.com/t/template-variables-in-grafana-managed-alert/54622 "2021-10-12T10:58:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankbo](https://avatars.discourse-cdn.com/v4/letter/f/dc4da7/32.png) [@frankbo](https://community.grafana.com/u/frankbo)\
**Post date:** [October 12, 2021, 10:58am UTC](https://community.grafana.com/t/template-variables-in-grafana-managed-alert/54622/1 "2021-10-12T10:58:48Z")

</div>

Hi,  
I am new to Grafana and I am trying Grafana managed alerts right now. I have halfway understood the setup of the alerts, but now I want to customize the text in the emails. For this I want to use Template Variables, but all the instructions I found I either didn’t understand or it just doesn’t work.

- What Grafana version and what operating system are you using?  
Grafana Version 8.0.6 / Ubuntu (latest Version, only for my personal test environment)

- **What** are you trying to achieve?  
I would like to enrich Grafana managed alerts with content from template variables.

- **How** are you trying to achieve it?  
I created a simple alert rule (grafana managed alert):

A - query:  
node\_cpu\_seconds\_total{mode=“system”,cpu=“0”}

B - expression:  
Reduce / Max / A

C - Expression:  
Math / $B \> 2140

Alert details:  
Summary  
{{ $labels.mode }}  
{{ $labels.job }}  
{{ $values.A }}  
{{ $values.B }}  
{{ $values.C }}

{{ $values.A.value }}  
{{ $labels.A.cpu }}  
{{ $labels.A.mode }}

{{ $values.B.value }}  
{{ $labels.B.cpu }}  
{{ $labels.B.mode }}

Custom Labels  
mymode = {{ $labels.mode }}  
myjob = {{ $labels.job }}  
mybvalue = {{ $values.B }}  
mybvaluevalue = {{ $values.B.value }}

- What happened?  
Only the two variables mymode and myjob are resolved correctly and are displayed in the alarm under labels. All other values appear only as strings.

- What did you **expect** to happen?  
All variables should be resolved, especially in the summary.

- Can you copy/paste the configuration(s) that you are having problems with?  
see above

- Did you receive any errors in the Grafana UI or in related logs? If so, please tell us **exactly** what they were.  
no. No errors.

- Did you follow any online instructions? If so, what is the URL?  
[Create Grafana managed alert rule | Grafana documentation](https://grafana.com/docs/grafana/latest/alerting/unified-alerting/alerting-rules/create-grafana-managed-rule/)

Alarm email content:  
Firing: 1 alertname

Firing Alert Prom 02

summary: {{ $labels.mode }} {{ $labels.job }} {{ $values.A }} {{ $values.B }} {{ $values.C }} {{ $values.A.value }} {{ $labels.A.cpu }} {{ $labels.A.mode }} {{ $values.B.value }} {{ $labels.B.cpu }} {{ $labels.B.mode }}  
Labels:

```
alertname: Alert Prom 02
cpu: 0
instance: xyz.abc.de:9100
job: node_exporter
mode: system
mybvalue: {{ $values.B }}
mybvaluevalue: {{ $values.B.value }}
myjob: node_exporter
mymode: system

```

Can someone explain to me exactly how the syntax must be or explain to me what I am misunderstanding or doing wrong?  
Are there any further instructions for Grafana managed alerts and template variables?

---

<div class="post-metadata">

**Author:** ![ehuggins](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@ehuggins](https://community.grafana.com/u/ehuggins)\
**Post date:** [October 12, 2021, 3:32pm UTC](https://community.grafana.com/t/template-variables-in-grafana-managed-alert/54622/2 "2021-10-12T15:32:26Z")

</div>

I have a very similar question:

- What Grafana version and what operating system are you using?  
Grafana Version 8.2.0 Grafana Pro Online; New Alerting System Enabled

- **What** are you trying to achieve?  
I would like to enrich Grafana managed alerts with content from labels using Alert Templates

- **How** are you trying to achieve it?  
I created a simple alert rule (grafana managed alert):

- **Alert Source:** Grafana

- **Email Template:**  
{{ define “rrzTestMessageAlertWithData” }}  
Labels:  
{{ range .Labels.SortedPairs }}  
{{ .Name }}: {{ .Value }}  
{{ end }}  
{{ if gt (len .Alerts.Firing) 0 }}  
{{ len .Alerts.Firing }} firing:  
{{ range .Alerts.Firing }} {{ template “alert” .}} {{ end }}  
{{ end }}  
Annotations:  
{{ if gt (len .Annotations) 0 }}  
Annotations:  
{{ range .Annotations.SortedPairs }}  
{{ .Name }}: {{ .Value }}  
{{ end }}  
{{ end }}  
{{ if gt (len .Alerts.Resolved) 0 }}  
{{ len .Alerts.Resolved }} resolved:  
{{ range .Alerts.Resolved }} {{ template “alert” .}} {{ end }}  
{{ end }}  
{{ end }}

- **Data Source:** Azure Logs/LogAnalytics

- **Example Labels**  

- **Q: How do I properly reference the label data: FaultingApplicationPath in the AlertTemplate?**

---

<div class="post-metadata">

**Author:** ![xmicky24git](https://avatars.discourse-cdn.com/v4/letter/x/a4c791/32.png) [@xmicky24git](https://community.grafana.com/u/xmicky24git)\
**Post date:** [December 13, 2021, 4:36pm UTC](https://community.grafana.com/t/template-variables-in-grafana-managed-alert/54622/3 "2021-12-13T16:36:30Z")

</div>

I have the same problem.  
Only {{ $labels.alertname }} and {{ $labels.datasource\_uid }} are resolved.

---

<div class="post-metadata">

**Author:** ![raveenabeywickrama](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@raveenabeywickrama](https://community.grafana.com/u/raveenabeywickrama)\
**Post date:** [September 27, 2022, 9:03am UTC](https://community.grafana.com/t/template-variables-in-grafana-managed-alert/54622/4 "2022-09-27T09:03:29Z")

</div>

Did you find a solution?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/grafana/original/2X/e/ef5308cc58c9e5a13187b5012eb99f406928462f.png) [@system](https://community.grafana.com/u/system)\
**Post date:** [October 12, 2022, 10:58am UTC](https://community.grafana.com/t/template-variables-in-grafana-managed-alert/54622/5 "2022-10-12T10:58:50Z")

</div>

Closed on October 12, 2022, 10:58am UTC

This topic was automatically closed after 365 days. New replies are no longer allowed.
