# How To Use Alert Message Templates in Grafana

**URL:** <https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537>\
**Category:** Alerting\
**Tags:** grot-thought, templating\
**Created:** [June 22, 2022, 12:01am UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537 "2022-06-22T00:01:21Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![melori.arellano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/melori.arellano/32/23041_2.png) [@melori.arellano](https://community.grafana.com/u/melori.arellano)\
**Post date:** [September 7, 2022, 5:48pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/23 "2022-09-07T17:48:41Z")

</div>

@jlu1 the dot is needed to provide the context.

Are you getting any notifications to slack, even a blank one?

---

<div class="post-metadata">

**Author:** ![melori.arellano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/melori.arellano/32/23041_2.png) [@melori.arellano](https://community.grafana.com/u/melori.arellano)\
**Post date:** [September 7, 2022, 5:51pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/24 "2022-09-07T17:51:05Z")

</div>

Check out my post at the top of this thread, if you see your label shown in the alert preview output, then you can use it in the alert by following the steps there.

---

<div class="post-metadata">

**Author:** ![jlu1](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@jlu1](https://community.grafana.com/u/jlu1)\
**Post date:** [September 8, 2022, 7:33am UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/25 "2022-09-08T07:33:18Z")

</div>

Yes, I receive a blank one if I use the dot.  
 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/2/e28d06d0050a89e3fc96afc79e2dcfa1e3f10458.png)

But I couldn’t find a way of retrieving any data.

---

<div class="post-metadata">

**Author:** ![dselena](https://avatars.discourse-cdn.com/v4/letter/d/7feea3/32.png) [@dselena](https://community.grafana.com/u/dselena)\
**Post date:** [September 8, 2022, 9:02am UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/26 "2022-09-08T09:02:38Z")

</div>

Hello,

This explanation was very helpful, thanks @melori.arellano !

I would also want to ask is there a relation between - the threshold which we set inside alert rules and the panels inside a dashboard. I am asking because I would like to display the same threshold on both graphs.

---

<div class="post-metadata">

**Author:** ![bhozar](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/bhozar/32/8922_2.png) [@bhozar](https://community.grafana.com/u/bhozar)\
**Post date:** [September 8, 2022, 9:46am UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/27 "2022-09-08T09:46:22Z")

</div>

Thanks for the guide @melori.arellano, it has got me most the way but now stuck again. How are you handling long value strings or conversions to % when using these templates?

I have an AlertValue that works, but gives me a very long number value and I was trying to trim it down using one of the _humanize_ [template functions](https://grafana.com/docs/grafana/latest/alerting/fundamentals/annotation-label/example-template-functions/), but whenever I put the function in the AlertValue it breaks the alert.

```auto
{{ with $values }}
{{ range $k, $v := . }}
   <b>Deployment:</b> {{$v.Labels.deployment}} 
   <b>Success Rate (%):</b> {{ $v | humanizePercentage }}
{{ end }}
{{ end }}

```

I have also tried it as:

```auto
{{ humanizePercentage $v }}

```

Also seems like the latest Grafana release has removed/broken HTML support in the annotations.

---

<div class="post-metadata">

**Author:** ![melori.arellano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/melori.arellano/32/23041_2.png) [@melori.arellano](https://community.grafana.com/u/melori.arellano)\
**Post date:** [September 8, 2022, 10:14pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/28 "2022-09-08T22:14:02Z")

</div>

@jlu1 The problem could live in two places:

1. The alert is not generating any values to add into your message template

Check the output of the alert by expanding the alert information on the alerting page, it should show the values that have been captured from the alert. Notice that it shows the annotation configuration and then when you expand the nested alert you’ll see the created labels and the expected value for the annotation:

 ![Screen Shot 2022-09-08 at 4.10.26 PM](https://us1.discourse-cdn.com/grafana/original/3X/4/3/434931d0f47dc2c4643f8345e3b5a6759aecf219.png)

1. The message template configuration isn’t capturing the right values from the annotation in your alert. Then you’ll want to try testing the message template itself to make sure that it is properly passed to slack.

---

<div class="post-metadata">

**Author:** ![jlu1](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@jlu1](https://community.grafana.com/u/jlu1)\
**Post date:** [September 9, 2022, 7:29am UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/29 "2022-09-09T07:29:29Z")

</div>

Hi @melori.arellano, you are right about the first case - I’m not receiving any data in my “check” Annotation.

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/8/a/8a042bfd17a46d340324d7afcc53fbeb267542e4.png)

Could you please help me on how to extract the “hostname.keyword” and “remoteEmail.keyword” from the Value field? Is this even possible? What would be the way of extracting those values in the case I have an array of these set of values as an output? For instance:  
`[metric='value' labels={hostname.keyword=a, remoteEmail.keyword=aaa@aaa.com} value=9999], [metric='value' labels={hostname.keyword=b, remoteEmail.keyword=bbb@bbb.com} value=1111]`

Thank you very much for for help.

---

<div class="post-metadata">

**Author:** ![melori.arellano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/melori.arellano/32/23041_2.png) [@melori.arellano](https://community.grafana.com/u/melori.arellano)\
**Post date:** [September 19, 2022, 10:48pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/30 "2022-09-19T22:48:02Z")

</div>

I tested this out on my instance using the sample data in our docs [Examples of template functions](https://grafana.com/docs/grafana/latest/alerting/fundamentals/annotation-label/example-template-functions/#humanizepercentage)

### humanizePercentage

**TemplateString** `{ humanizePercentage $value }`. ← this didn’t work, because in the alert template, the value was in $values.B.Value

From my alert preview, I can see that the value is in $values.B.Value

![Screen Shot 2022-09-19 at 4.45.18 PM](https://us1.discourse-cdn.com/grafana/optimized/3X/8/c/8c0976d5ea9cbb98e5a0c3dd1fbb195f13466eac_2_690x55.png)

This worked for me:

`{{$values.B.Value | humanizePercentage }}`

 ![Screen Shot 2022-09-19 at 4.44.33 PM](https://us1.discourse-cdn.com/grafana/original/3X/1/0/105c6a373f31feb323cfe19e66c7d0d89c04e51b.png)

---

<div class="post-metadata">

**Author:** ![melori.arellano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/melori.arellano/32/23041_2.png) [@melori.arellano](https://community.grafana.com/u/melori.arellano)\
**Post date:** [September 19, 2022, 11:01pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/31 "2022-09-19T23:01:05Z")

</div>

@dselena this is a good question. With the new unified alerting in Grafana 8+ alerts are decoupled from dashboards so the thresholds don’t transfer over automatically.

---

<div class="post-metadata">

**Author:** ![bhozar](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/bhozar/32/8922_2.png) [@bhozar](https://community.grafana.com/u/bhozar)\
**Post date:** [September 23, 2022, 3:41pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/32 "2022-09-23T15:41:09Z")

</div>

How does it work with multipe values returned? I have multipe values returned which is why your initial template was so helpful. So of the queries return dozens of results e.g.

> [var=‘ **B0** ’ metric=‘server1:9198 / learnapplogs’ labels={instance=server1:9198, job=logstash\_exporter, pipeline=logs} value=0.2932612419621986],  
> [var=‘ **B1** ’ metric=‘server2:9198 / learnapplogs’ labels={instance=server2:9198, job=logstash\_exporter, pipeline=logs} value=0.2856948269581056]

Ideally this would somehow work:

> {{ with $values. **B**.Value | humanize }}  
> {{ range $k, $v := . }}  
> Instance: {{$v.Labels.instance}}  
> Pipeline: {{$v.Labels.pipeline}}  
> Message Count: {{ $v }}  
> {{ end }}  
> {{ end }}

---

<div class="post-metadata">

**Author:** ![dselena](https://avatars.discourse-cdn.com/v4/letter/d/7feea3/32.png) [@dselena](https://community.grafana.com/u/dselena)\
**Post date:** [September 26, 2022, 8:47am UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/33 "2022-09-26T08:47:23Z")

</div>

Dear @melori.arellano,

Thank you for your response! For manual set, you mean to create alert and with the same configuration for visual threshold on the panel or there is another way to connect both thresholds manually?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![yassirmakloul](https://avatars.discourse-cdn.com/v4/letter/y/5e9695/32.png) [@yassirmakloul](https://community.grafana.com/u/yassirmakloul)\
**Post date:** [September 26, 2022, 1:14pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/34 "2022-09-26T13:14:58Z")

</div>

So my alert Preview looks something like the following:

> [var=‘B0’ **metric** =‘17145’ labels={} value=4], [var=‘B1’ **metric** =‘10040’ labels={} value=4], [var=‘B2’ **metric** =‘Machine2’ labels={} value=4],

And I want to access the metric variable in the dollar sings area. How would that be possible?

> {{ with $values }}  
> {{ range $k, $v := . }}  
> $$$$$$$  
> Alerting value: {{ $v }}  
> {{ end }}  
> {{ end }}

---

<div class="post-metadata">

**Author:** ![fhejazi](https://avatars.discourse-cdn.com/v4/letter/f/fbc32d/32.png) [@fhejazi](https://community.grafana.com/u/fhejazi)\
**Post date:** [September 26, 2022, 5:00pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/35 "2022-09-26T17:00:17Z")

</div>

Hello @melori.arellano

Using custom labels break the HTML formatting for the email notifications in Grafana alerting. This is apparently a known issue discussed [here](https://github.com/grafana/grafana/issues/42946).

Would you by any chance have any suggestions as to a potential workaround, other than what is discussed in the link above (editing the HTML template on Grafana server).

The problem we’re having is that we have data we want to suppress in the email message, such as:

```auto
Value: [var='B' labels={instance=<XXXX> } value=53.46666666679084], [var='C' labels={instance=<XXXX>} value=1]

```

Thanks!

---

<div class="post-metadata">

**Author:** ![bhozar](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/bhozar/32/8922_2.png) [@bhozar](https://community.grafana.com/u/bhozar)\
**Post date:** [October 6, 2022, 12:43pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/36 "2022-10-06T12:43:28Z")

</div>

Hi @melori.arellano is there any more help you might be able to offer here please? I’m now completely stuck on it and it feels so close to working.

---

<div class="post-metadata">

**Author:** ![shikharmahajan](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@shikharmahajan](https://community.grafana.com/u/shikharmahajan)\
**Post date:** [October 6, 2022, 1:10pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/37 "2022-10-06T13:10:38Z")

</div>

I also have the same requirement. Tried all the possible combination but cant fetch the metric value

---

<div class="post-metadata">

**Author:** ![milinpatel13298](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/milinpatel13298/32/26506_2.png) [@milinpatel13298](https://community.grafana.com/u/milinpatel13298)\
**Post date:** [October 6, 2022, 8:22pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/38 "2022-10-06T20:22:38Z")

</div>

Hi @melori.arellano ! Is this possible for email alerts yet or is it still an issue?  
If it is still a issue, is it not possible to see the values in the alert rules UI as well or just in the email body?

---

<div class="post-metadata">

**Author:** ![melori.arellano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/melori.arellano/32/23041_2.png) [@melori.arellano](https://community.grafana.com/u/melori.arellano)\
**Post date:** [October 7, 2022, 7:47pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/40 "2022-10-07T19:47:34Z")

</div>

> [@melori.arellano](#):
>
> humanizePercentage

I found it! I was looking through tests and notice they use `.Value`

```auto
{{ with $values }}
{{ range $k, $v := . }}
   Location: 
{{ $v.Labels.location }}
   Alerting value: 
{{ humanizePercentage .Value}}
{{ end }}
{{ end }}

```

This worked for me, I got:

 ![Screen Shot 2022-10-07 at 1.47.12 PM](https://us1.discourse-cdn.com/grafana/original/3X/8/2/82851820537175887aaff56d2fa523f7dc4de7c2.png)

---

<div class="post-metadata">

**Author:** ![melori.arellano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/melori.arellano/32/23041_2.png) [@melori.arellano](https://community.grafana.com/u/melori.arellano)\
**Post date:** [October 7, 2022, 7:59pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/41 "2022-10-07T19:59:37Z")

</div>

@milinpatel13298 this thread is specific to slack, but message templates are supported for the email contact point as well.

---

<div class="post-metadata">

**Author:** ![melori.arellano](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/melori.arellano/32/23041_2.png) [@melori.arellano](https://community.grafana.com/u/melori.arellano)\
**Post date:** [October 7, 2022, 8:01pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/42 "2022-10-07T20:01:23Z")

</div>

Yes, you’d just want to create the alert using the same threshold that you provided in the dashboard.

---

<div class="post-metadata">

**Author:** ![milinpatel13298](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/milinpatel13298/32/26506_2.png) [@milinpatel13298](https://community.grafana.com/u/milinpatel13298)\
**Post date:** [October 7, 2022, 9:40pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/43 "2022-10-07T21:40:53Z")

</div>

Hi @melori.arellano! Thank you for your reply.  
Would you be able to help me get started with that please?  
Here’s what I have tried so far, none of these seem to work.

{{ $values.last\_process\_count.process.instance }}  
{{ $values.last\_process\_count.Value }}  
{{ $values.last\_process\_count.server.instance }}  
{{ $values }}  
{{ $values.server\_health\_condition.Value }}  
{{ $values }}  
{{ $values.server\_health\_condition.value }}  
{{ $values.last\_process\_count.value }}  
{{ .Values }}  
{{ .ValueString }}  
{{ $value }}

The values don’t populate as expected in AlertValues annotation that I manually created AND in the Description field. I can’t seem to get the values populated in the Alert Rules UI page AND the actual alert email I receive.

Here are the labels generated when I click on Preview Alerts  
[var=‘last\_process\_count’ labels={process=process1, server=SANDBOX} value=7], [var=‘server\_health\_condition’ labels={process=process1, server=SANDBOX} value=0]

[var=‘last\_process\_count’ labels={process=process2, server=SANDBOX} value=7], [var=‘server\_health\_condition’ labels={process=process2, server=SANDBOX} value=0]

last\_process\_count - Reduce expression  
server\_health\_condition - Math expression

[Previous page](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537.md?page=1)

[Next page](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537.md?page=3)
