# Get label's value as query value

**URL:** https://discuss.prometheus.io/t/get-labels-value-as-query-value/1311
**Category:** PromQL
**Created:** [November 21, 2022, 4:49pm UTC](https://discuss.prometheus.io/t/get-labels-value-as-query-value/1311 "2022-11-21T16:49:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![unlucio](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/unlucio/32/567_2.png) [@unlucio](https://discuss.prometheus.io/u/unlucio)
#### Post date: [November 21, 2022, 4:49pm UTC](https://discuss.prometheus.io/t/get-labels-value-as-query-value/1311/1 "2022-11-21T16:49:00Z")

</div>

Hello,  
I’m looking for a way to get a label’s value as query result rather than just a 1 or a 0  
I’ve been knocking my head all around the web for hours but I’ve yet to find a definitive working answer. I’ve found the topic been asked on different websites (including but not limited to stackoverflow and github of course) and tried everything suggested without success.  
Even on this very forum I’ve found someone with the same need:

> [@Key Value -\> prometheus value](https://discuss.prometheus.io/t/key-value-prometheus-value/787):
>
> Hi All, I’ve been searching around on the web, but have been unsuccessful in getting what I want to work. I have a metric that is appearing like this - its got lots of labels with useful information, but they all have a value of “1” custom\_app {some\_value=xyz, other\_label=aaa, license\_expiry=12345} 1 I want to extract out the license\_expiry = 12345, so I can create a query on it in alert manager - and also see it as a graph in Prometheus I’ve looked at label\_replace - but I couldn’t…

But nobody ever answered the poor chap, si I’,m trying my luck and see.

I’ve the following query:

```auto
homeassistant_climate_mode{entity="climate.livingroom",mode=~"cool|heat|off"}

```

Instead of 3 entities representing the boolean state for each possible value of `mode` at a given time, I need a single entity returning the string value for `mode` at the given time.

For instance instead of:

```auto
homeassistant_climate_mode{domain="climate", entity="climate.livingroom", friendly_name="Livingroom", instance="10.6.9.3:8123", job="homeassistant", mode="cool"} --> 0
homeassistant_climate_mode{domain="climate", entity="climate.livingroom", friendly_name="Livingroom", instance="10.6.9.3:8123", job="homeassistant", mode="heat"} --> 1
homeassistant_climate_mode{domain="climate", entity="climate.livingroom", friendly_name="Livingroom", instance="10.6.9.3:8123", job="homeassistant", mode="off"} --> 0

```

I need

```auto
homeassistant_climate_mode{domain="climate", entity="climate.livingroom", friendly_name="Livingroom", instance="10.6.9.3:8123", job="homeassistant"} --> heat

```

and rather than plotting 3 entities I’m looking to produce a graph where the X axis represent time and the Y axis represent the possible values of `mode`:

 ![Screenshot_2022-11-21_at_16_43_36_1_](https://europe1.discourse-cdn.com/flex017/uploads/prometheus/original/1X/b42b202aa3f264b87b69c4b50f2c81c53d74f1c9.png)

**Please note:** My goal is to graph this stuff on grafana using the discrete graph, in my example I used the screenshot I took from prometheus only because it was faster to edit into a visual sample of what I need. I know prometheus’ graph probably cannot place text labels on its axis, but that’s not the point of my question.

---

<div class="post-metadata">

### Author: ![stuart](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/stuart/32/18_2.png) [@stuart](https://discuss.prometheus.io/u/stuart)
#### Post date: [November 21, 2022, 5:53pm UTC](https://discuss.prometheus.io/t/get-labels-value-as-query-value/1311/2 "2022-11-21T17:53:37Z")

</div>

Prometheus only handles float values, not strings.

Label values can be whatever you want, but are only used for metric searching, not for calculations.

The standard option for producing your bottom graph is to use an “enum” type metric, where different values represent different states (e.g. 0 = off, 1 = red, 2 = blue, 3 = cold) and then use a graphing tool that can replace the 0-3 values with their meaning strings. You can also produce such an enum type from individual 0/1 metrics via multiplication and addition.

---

<div class="post-metadata">

### Author: ![unlucio](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/unlucio/32/567_2.png) [@unlucio](https://discuss.prometheus.io/u/unlucio)
#### Post date: [November 21, 2022, 7:04pm UTC](https://discuss.prometheus.io/t/get-labels-value-as-query-value/1311/3 "2022-11-21T19:04:50Z")

</div>

Omg I added the “ **Please note** ” because I was afraid someone would have focused on the picture and miss my point 😂. I guess i failed 😆

All good 🙂

As said the representation on the graph is not the point of my question. The tool I’m gonna use to draw the graph is fine with strings.

What I need is a single result instead of N (3 in my case) returning as value the label’s value.

My question is more generic regarding how to query the database, the examples are just examples but they’re not the only case I need this for.

Admittedly I didn’t use the best piece of data for my question, tho the question I linked is asking the same thing on a label that contains a number value.

I considered the enum option as well, but it didn’t help.  
Using a map such as:  
off → 0  
cool → 1  
heat -\>2

for a query at a given moment in time the result is:

```auto
homeassistant_climate_mode{domain="climate", entity="climate.livingroom", friendly_name="Livingroom", instance="10.6.9.3:8123", job="homeassistant", mode="1"} --> 0
homeassistant_climate_mode{domain="climate", entity="climate.livingroom", friendly_name="Livingroom", instance="10.6.9.3:8123", job="homeassistant", mode="2"} --> 1
homeassistant_climate_mode{domain="climate", entity="climate.livingroom", friendly_name="Livingroom", instance="10.6.9.3:8123", job="homeassistant", mode="0"} --> 0

```

so 3 results each returning a boolean value

instead of

```auto
homeassistant_climate_mode{domain="climate", entity="climate.livingroom", friendly_name="Livingroom", instance="10.6.9.3:8123", job="homeassistant"} --> 2

```

the single result I actually need, with the label’s value as value

I indeed understand that the labels’ main job is for searching, but once ran my search and obtained my record, for example

````auto
metric{label1="123", label2="456"} --> 1
```
is there a way to tell to prometheus that the value I actually care about, and I actually want, is contained in `label2` and thus to present me that value instead of a bool? Obtaining:

````

metric{label1=“123”, label2=“456”} → 456

```auto

```

---

<div class="post-metadata">

### Author: ![stuart](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.prometheus.io/stuart/32/18_2.png) [@stuart](https://discuss.prometheus.io/u/stuart)
#### Post date: [November 22, 2022, 7:18am UTC](https://discuss.prometheus.io/t/get-labels-value-as-query-value/1311/4 "2022-11-22T07:18:39Z")

</div>

To create the “enum” you could do something like:

```auto
(homeassistant_climate_mode{entity="climate.livingroom", mode="cool"} * 1) + (homeassistant_climate_mode{entity="climate.livingroom", mode="heat"} * 2) + (homeassistant_climate_mode{entity="climate.livingroom", mode="red"} * 3)

```

Which would give the value of 1 for cool, 2 for heat and 3 for red.
