Get label's value as query value

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:

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

I’ve the following query:

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:

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

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:

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.

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.

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

All good :slight_smile:

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:

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

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

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

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

(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.