# 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:** 129

<div class="post-metadata">

**Author:** ![ldrascic](https://avatars.discourse-cdn.com/v4/letter/l/aca169/32.png) [@ldrascic](https://community.grafana.com/u/ldrascic)\
**Post date:** [January 23, 2023, 11:07pm UTC](https://community.grafana.com/t/how-to-use-alert-message-templates-in-grafana/67537/129 "2023-01-23T23:07:08Z")

</div>

Hi @rodrigoparavano,  
If you want to get label value from query then you can do with:

```auto
{{ $values.<queryname>.Labels.<tag> }}

```

For example:

```auto
On server {{ $values.B.Labels.host }} port {{ $values.B.Labels.port }} is closed (not listening).

```

would print:

```auto
On server monitoringserver port 8282 is closed (not listening).

```

> **Alert configuration and output**
>
> ![Grafana prometheus label matching query](https://us1.discourse-cdn.com/grafana/original/3X/7/0/706158fc7637855f8a674f514e16f35262cd9f79.png)
> 
> ![Grafana prometheus label matching](https://us1.discourse-cdn.com/grafana/original/3X/3/7/3768de3d4ccfa9790f04fe645b6cabd48bc72438.png)

* * *

**Bonus:**

> **Dynamic Severity labels**
>
> You can also do more advanced stuff like dynamic label value setting based on query values. For example you can set custom label Severity which would have next values:
> 
> > Fatal for values \>= 98  
> > Critical for values \>= 95 AND \< 98  
> > Warning for values \>= 90 AND \< 95  
> > NoData if $value.B does not exist
> 
> Severity label formula:
> 
> ```auto
> {{ if $values.result }}{{ if (ge $values.result.Value 98.0) }}Fatal{{ else if and (lt $values.result.Value 98.0) (ge $values.result.Value 90.0) }}Critical{{ else if and (lt $values.result.Value 90.0) (ge $values.result.Value 85.0) }}Warning{{ else }}None{{ end }}{{ else }}NoData{{ end }}
> 
> ```
> 
> ![Grafana alert with dynamic severity](https://us1.discourse-cdn.com/grafana/original/3X/7/8/783682acc0d5cf4a695521c71338ffc74f2537c9.png)
> 
> Note that here my query in grafana alert is called result. Be aware that every time new severity label is created (for example Severity=Critical) a new alert check will be created and the alert with old Severity label (e.g. Severity=Warning) will be resolved with `"grafana_state_reason": "MissingSeries"`. Newly created check would first go to pending (if you set it) and then to alerting state.
> 
> In order to avoid quick changes of Severity label (and thus avoiding alerts being in pending mode) I usually use mean function in my query with time range of 5 minutes (mean of values over larger time range provides more stable number but it is slower at alerting). Also, I always first round numbers (with Math expression) and then perform Threshold expression over rounded numbers. Reason behind is also to avoid fast label changing.
> 
> Last thing to mention is part of formula which checks if data in result variable exists:
> 
> ```auto
> {{ if $values.result }}........{{ else }}NoData{{ end }}
> 
> ```
> 
> This is used so that when data is missing (e.g. agent has crashed or somebody stopped it) you get Severity=NoData instead of broken formula like on next picture:
> 
> ![Grafana alert broken formula](https://us1.discourse-cdn.com/grafana/original/3X/6/f/6fceef52ea77896a41f212db59de836b66d47b8f.png)

Hope this helps. 🙂

Best regards,  
ldrascic

---

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