Alert message template

I want to configure the notification message of an alert. I have a query that returns an error type, if there’s an error the alert message should say the error type in the same way a value mapping in Grafana translates numbers to text following this rule

When configuring the alert rule, the raw value of this variables is given by this {{$values.B}} which works correctly. Reading about the templating language syntax, I thought of using this

{{ if (eq $values.B 1) -}}
  OverTemperature
{{ else if (eq $values.B 2) -}}
  ThermocoupleFail
{{ else if (eq $values.B 3) -}}
  NoFlame
{{ else if (eq $values.B 4) -}}
  GasWhileDrying
{{ else if (eq $values.B 5) -}}
  GasNoAir
{{ else if (eq $values.B 6) -}}
  GasNotFiring
{{ else -}}
  None
{{- end }}

Adding this code on the notification description or in a label doesn’t work. Instead I see raw template code in the notification when an alarm is triggered. I am going a bit crazy over this and already checked out the docs Alerting template language | Grafana documentation

Ultimately I’d like to have a concise alarm notification, specially when using the oncall integration. This is what I get at the moment and is full of clutter

Any advice or resources to help me achieve this are greatly appreciated!
Thanks.

Hi,

try using 1.0, 2.0, etc instead of 1, 2. Worked for me in a similar case, though I’m not sure why :smile:

I am using Grafana cloud…

Yeah, but alert rule template is still configured by you? Instead of

{{ if (eq $values.B 1) -}}

use

{{ if (eq $values.B 1.0) -}}

Yes the alert rule is still configured by me. I don’t know why I thought you meant to change Grafana version, but now I understand you.

I tried using 1.0 instead, but still it’s not working. I even added this test label just to see if grafana templating language works correctly:

Test = {{ if (eq 0 0) -}} Working {{- end }}

And it does work. I even simplified the whole alert rule template, but as can be seen in the screenshot, it’s like the templating language can’t do a comparison with the referenced value $values.B

Raw error = {{$values.B}} does convert properly to Raw error = 0, but using the conditional {{ if (eq 0.0 $values.B) -}} doesn’t work correctly. This is confusing

I think you should also use $values.B.value or $values.B.Value (probably the latter one)

Thank you so much, using $values.B.Value worked!