# 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:** 1\
**Showing post:** 3

<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:** [July 20, 2022, 11:27pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/3 "2022-07-20T23:27:40Z")

</div>

_Edited this solution to add the most common answers in this thread in one place, including template functions and labels with dots in the name - read through to the end for those_

welcome to the :grafana: community @baileyheit1!

This is a really common question and I’m still learning how to access the values out of the alert query too. I hope this is a helpful example - I tried to make it very detailed for the benefit of other community users, even though I can tell you already know how to do some of these steps 😉

With an alert query that looks like this - I created this with our sample TestDataDB datasource and manually added some labels that are similar to the ones you mentioned.

 ![Screen Shot 2022-07-20 at 4.13.43 PM](https://us1.discourse-cdn.com/grafana/original/3X/7/d/7d6c6f1b8afbd2eb35ecd4c299daf1c5aa0993f7.png)

Click on the **preview alert** button to see the labels and values in the map:

 ![Screen Shot 2022-07-20 at 5.01.27 PM](https://us1.discourse-cdn.com/grafana/original/3X/5/4/54ae8e15611efb576be94e5281f1231cd55da0b3.png)

To refer to labels and values from the query add a new annotation to the alert like this. Name the annotation **AlertValues**

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

```

Here it is in the context of the alert definition:

 ![Screen Shot 2022-07-20 at 4.58.46 PM](https://us1.discourse-cdn.com/grafana/original/3X/8/1/81ebc787fe08c8d86e135a5e86cd875a89aedecb.png)

Then you can add a message template called **myalert** to create a single alert message and print the annotation. Notice that you will reference the annotation by name (`.Annotations.AlertValues`) in the sample below:

```auto
{{ define "myalert" }}
  [{{.Status}}] {{ .Labels.alertname }} 
  {{ .Annotations.AlertValues }}
{{ end }}

```

Here it is in context of the UI:

 ![Screen Shot 2022-07-20 at 5.18.53 PM](https://us1.discourse-cdn.com/grafana/original/3X/a/9/a91b6cba14c6e1769ef5ba637a059c7cac99c414.png)

Next, you can add a message template for the full message, I called this one **mymessage**. Notice how the **mymessage** template references the **myalert** template that I created in the last step:

```auto

{{ define "mymessage" }}
  {{ if gt (len .Alerts.Firing) 0 }}
    {{ len .Alerts.Firing }} firing:
    {{ range .Alerts.Firing }} {{ template "myalert" .}} {{ end }}
  {{ end }}
  {{ if gt (len .Alerts.Resolved) 0 }}
    {{ len .Alerts.Resolved }} resolved:
    {{ range .Alerts.Resolved }} {{ template "myalert" .}} {{ end }}
  {{ end }}
{{ end }}

```

Lastly, you can add the **mymessage** template to the body of your slack contact point under **Optional Slack Settings** :

```auto
Alert summary:
{{ template "mymessage" . }}

```

In the UI, it looks like this:  
 ![Screen Shot 2022-07-20 at 5.22.40 PM](https://us1.discourse-cdn.com/grafana/original/3X/6/4/64bef16400bc2af234c22ed38b56c7cd0dc92047.png)

Now you should have an alert to your slack contact point that looks something like this:

 ![Screen Shot 2022-07-20 at 5.23.52 PM](https://us1.discourse-cdn.com/grafana/original/3X/5/5/552346562d3be654b94993eb8efad3675fe4aa30.png)

**A side note related to using Alert.Labels . . .**

Something that can get confusing is that `.Labels` mentioned in the [Template Data](https://grafana.com/docs/grafana/latest/alerting/contact-points/message-templating/template-data/) docs refer to the labels attached to the alert object NOT the query response labels. For those, we need to get into the `values` map in the exercise above:

 ![Screen Shot 2022-07-20 at 6.12.51 PM](https://us1.discourse-cdn.com/grafana/original/3X/f/3/f38009a1cc9f9b409dbaf870a4d8ee4bf8c55db0.png)

 ![Screen Shot 2022-07-20 at 6.07.22 PM](https://us1.discourse-cdn.com/grafana/original/3X/5/1/51bace857b297e7f60030ca3d8615f3a1a40e3e3.png)

**Extra helpful reference [table from the alerting docs](https://grafana.com/docs/grafana/latest/alerting/fundamentals/annotation-label/variables-label-annotation/)**

 ![Screen Shot 2022-10-12 at 4.35.48 PM](https://us1.discourse-cdn.com/grafana/original/3X/e/7/e71e18e417d3242b31b0bea03c2fd4044c8869fc.png)

**Examples for specific situations like . . .**

**When a label has dots in the name**

If you have a label like `netflow.ipv4_dst_addr` with a value of `192.168.1.1`, you might find that your alert template just shows the raw template (which is what happens if there’s an error in the evaluation).

If you’re using a classic condition, here’s an example of using the go index function:

{{ index $values.B0.Labels “netflow.ipv4\_dst\_addr” }}

[![Screen Shot 2022-08-15 at 6 15 21 PM](https://user-images.githubusercontent.com/18175287/184757325-4cf2869a-46c5-4fbc-afab-03033961fd06.png) ](https://user-images.githubusercontent.com/18175287/184757325-4cf2869a-46c5-4fbc-afab-03033961fd06.png)

if your alert has two or more conditions or an expression, then you can use the $labels variable:  
{{ index $labels “netflow.ipv4\_dst\_addr” }}

[![Screen Shot 2022-08-15 at 6 11 25 PM](https://user-images.githubusercontent.com/18175287/184757033-9bf2e545-0c5d-4fd9-9f59-69f93f0062d6.png) ](https://user-images.githubusercontent.com/18175287/184757033-9bf2e545-0c5d-4fd9-9f59-69f93f0062d6.png)

**How to use template functions like `humanizePercentage`**

To reference the value in $values.RefIdX.Value (ex: $values.B0.Value) with [template functions](https://grafana.com/docs/grafana/latest/alerting/fundamentals/annotation-label/example-template-functions/) use `.Value`:

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

```

---

_[View the full topic](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537)._
