# How to chain conditions with AND/OR

**URL:** <https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802>\
**Category:** Prometheus\
**Tags:** alerting\
**Created:** [November 21, 2024, 11:24am UTC](https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802 "2024-11-21T11:24:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![benshajisequoiaat](https://avatars.discourse-cdn.com/v4/letter/b/dfb087/32.png) [@benshajisequoiaat](https://community.grafana.com/u/benshajisequoiaat)\
**Post date:** [November 21, 2024, 11:24am UTC](https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802/1 "2024-11-21T11:24:06Z")

</div>

I have a question to see if i can set alert condition by chaining multiple `value_name` from below Prometheus data.

- Data source: Prometheus  
Data format as below:

```auto
equipment_value{eqp_id="19986",value="18.11",value_datetime="2024-11-21T10:00:00Z",value_name="Amps A"} 18.11
equipment_value{eqp_id="19986",value="0.0",value_datetime="2024-11-21T10:00:00Z",value_name="Overspeed"} 0.0
equipment_value{eqp_id="19986",value="0.0",value_datetime="2024-11-21T10:00:00Z",value_name="Emergency Stop"} 0.0
equipment_value{eqp_id="19986",value="-1.0",value_datetime="2024-11-21T10:00:00Z",value_name="Power Factor"} -1.0
equipment_value{eqp_id="19986",value="58.91",value_datetime="2024-11-21T10:00:00Z",value_name="Frequency"} 58.91
equipment_value{eqp_id="19986",value="-0.14",value_datetime="2024-11-21T10:00:00Z",value_name="Oil Pressure"} -0.14
equipment_value{eqp_id="19986",value="3534.0",value_datetime="2024-11-21T10:00:00Z",value_name="RPM"} 3534.0
equipment_value{eqp_id="19986",value="0.0",value_datetime="2024-11-21T10:00:00Z",value_name="Coolant Temp"} 0.0

```

I was able to create individual alert (see screenshot below)

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

While this works, I want something else as below: `Amps A = 0 and Emergency Stop = 1`

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

From my readings, i’ve to use some _transformers_ in Grafana, which i’m still unclear about. Appreciate if anyone can comment on this.  
Thanks!

---

<div class="post-metadata">

**Author:** ![dawiddebowski](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dawiddebowski/32/49435_2.png) [@dawiddebowski](https://community.grafana.com/u/dawiddebowski)\
**Post date:** [November 21, 2024, 5:21pm UTC](https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802/2 "2024-11-21T17:21:23Z")

</div>

Hi,

So, you want the alert to fire if

```auto
equipment_value{eqp_id="19986",value="18.11",value_datetime="2024-11-21T10:00:00Z",value_name="Amps A"} 0

```

and (at the same time)

```auto
equipment_value{eqp_id="19986",value="0.0",value_datetime="2024-11-21T10:00:00Z",value_name="Emergency Stop"} 1

```

Right? If so, I can think of two ways you can do that:

#### 1. Prometheus query

You _should_ be able to achieve that in single prometheus query (I’m more familiar with Victoria Metrics, so the query might be a bit inaccurate) with query:

```auto
equipment_value{eqp_id="19986",value_name="Amps A"} == 0 and on(eqp_id) 
equipment_value{eqp_id="19986",value_name="Emergency Stop"} == 1

```

(I think that should work, but I’m not 100% sure).

With that (I’m not sure you can achieve that in Builder editor) you receive data from prometheus directly. You can use instant query (in query options) and threshold (without reduce). It’s less readable but operations are performed on prometheus, not grafana, so it’s more effective. Be aware that it might not produce data, so set the settings accordingly.

#### 2. Within Grafana

You can create two queries:

```auto
equipment_value{eqp_id="19986",value_name="Amps A"}

```

and

```auto
equipment_value{eqp_id="19986",value_name="Emergency Stop"}

```

and then:

##### 2.1 Use Classic Condition

like this:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/c/3/c3c6ae672eed18803d26139a21d5713640b82dfb.png)  
(you can add `AND` and `OR` conditions with the plus button).

OR

##### 2.2 Use math calculation

something like that:

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/5/4/549dd28c0fdcd23e411d103a336902fa8bf50211.png)

BTW transformations work only in dashboard, they won’t work in alerts.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![benshajisequoiaat](https://avatars.discourse-cdn.com/v4/letter/b/dfb087/32.png) [@benshajisequoiaat](https://community.grafana.com/u/benshajisequoiaat)\
**Post date:** [January 21, 2025, 6:25am UTC](https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802/3 "2025-01-21T06:25:22Z")

</div>

Thanks for the reply.

To confirm, you’ve created 2 queries.

- Amps A
- Emergency Stop  
And then, you’ve added 2 expressions
- Reduce for Amps A
- Reduce for Emergency Stop  
where Reduce will return the last value of both.

Finally, we write a combined math expression to check if both are above 1.

Thanks

---

<div class="post-metadata">

**Author:** ![dawiddebowski](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dawiddebowski/32/49435_2.png) [@dawiddebowski](https://community.grafana.com/u/dawiddebowski)\
**Post date:** [January 21, 2025, 6:48am UTC](https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802/4 "2025-01-21T06:48:31Z")

</div>

Just to be clear:

- math is also an expression
- math checks if reduced (last) value of ApmsA is higher than 0 and if reduced (last) value of Emergency Stop is equal to 1

---

<div class="post-metadata">

**Author:** ![benshajisequoiaat](https://avatars.discourse-cdn.com/v4/letter/b/dfb087/32.png) [@benshajisequoiaat](https://community.grafana.com/u/benshajisequoiaat)\
**Post date:** [January 21, 2025, 9:36am UTC](https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802/5 "2025-01-21T09:36:10Z")

</div>

Hi, thanks for the answer again.

The main question that we have now is something like this. Lets say i want to monitor my _Fuel Level_. In mind mind, below are the conditions when i should be getting alerts

- 0-20: Very low fuel
- 20-70: Medium fuel
- 70-90: Good fuel
- 90-100: Refueled

What would be an ideal way to set these conditions? I’m currently having three different alert conditions for these three conditions. But its quickly becoming un-maintainable to write N number of alerts for N number of conditions.

Do you have any suggestions?

Thanks

---

<div class="post-metadata">

**Author:** ![dawiddebowski](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dawiddebowski/32/49435_2.png) [@dawiddebowski](https://community.grafana.com/u/dawiddebowski)\
**Post date:** [January 21, 2025, 2:31pm UTC](https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802/6 "2025-01-21T14:31:40Z")

</div>

It can be hard to make, since Grafana doesn’t really support something like that - the threshold of the alert is somewhat constant. The question is - do you need to have an alert for `Medium/Good` fuel? In my usage, we do 2 alerts - one warning, one critical in such cases. You could try some promql logic that would set the label based on the query result but it would be troublesome, if possible at all.

---

<div class="post-metadata">

**Author:** ![benshajisequoiaat](https://avatars.discourse-cdn.com/v4/letter/b/dfb087/32.png) [@benshajisequoiaat](https://community.grafana.com/u/benshajisequoiaat)\
**Post date:** [January 23, 2025, 5:55am UTC](https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802/7 "2025-01-23T05:55:27Z")

</div>

Hi, thanks for the reply.

> do you need to have an alert for `Medium/Good` fuel?

Yes, we have currently **four alerts for four conditions**

- 0-20: Very low fuel
- 20-70: Medium fuel
- 70-90: Good fuel
- 90-100: Refueled

We did try to do some promql query to do everything in one query, but it was not working as expected. In short, we currently have N alerts for N conditions.

---

<div class="post-metadata">

**Author:** ![dawiddebowski](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dawiddebowski/32/49435_2.png) [@dawiddebowski](https://community.grafana.com/u/dawiddebowski)\
**Post date:** [January 29, 2025, 1:25pm UTC](https://community.grafana.com/t/how-to-chain-conditions-with-and-or/136802/8 "2025-01-29T13:25:12Z")

</div>

So you’ll have an alert that would fire constatnly only changing the description? Theoretically you could do that but I’m not sure Grafana supports something like that - alerts are grouped by title, the title doesn’t change and I’m not sure how it will behave.
