Influxdb Flux - Change or alias legend label from "temp {host=XX, topic=YY}" just to "YY"

I configured Flux for my grafana mqtt temperature sensor (gathered via zigbee2mtqq > mosquitto > telegraf > influxdb2).

I have this simple flux query that generally does work:

from(bucket: "homeassistant")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
|> filter(fn: (r) => r["_field"] == "temperature")
|> filter(fn: (r) => r["topic"] == "zigbee2mqtt/Arbeitszimmer/SENSOR" or r["topic"] == "zigbee2mqtt/Schlafzimmer/SENSOR")
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")

However my legend contains these entries:

temperature {host="192.168.0.42", topic="zigbee2mqtt/Schlafzimmer/SENSOR"}
temperature {host="192.168.0.42", topic="zigbee2mqtt/Arbeitszimmer/SENSOR"}

I spend a lot of time to search this forum and google but could not find a way on how to specify the actual label content. I would like them to just read “Schlafzimmer” and “Arbeitszimmer”.

I could see that in InfluxQL you are able to do that with ALIAS / GROUP BY statements. But I am unable to find how to do that using Flux in grafana.

Any help much appreciated!

I actually found out by trial and error later that “Field Overrides” provide the behavior I want.

Adding a field override, then choosing “Field with name” and entering the verbose name, I can apply “Standard options > Display name” to that field and enter the “friendly” name. That can be done for each sensor I have.

Hope it helps someone also looking for this solution.

2 Likes

I’ve been looking for a solution for this and while yours will work it’s not dynamic.

What worked for me was to drop the extra columns from the query and Grafana will be nice enough to use what is available. In your case you can add |> drop(columns: ["_field", "host"]). This won’t give you the exact result you wanted but it might be a step forward for dynamic solution.

In my case I have the device name as a tag and it’s what I wanted as the label so the above solution worked perfectly for me.

2 Likes

I realize that this thread is almost a year old. However, I thought, I might add one more solution that I just found.
I used the “Transform” feature in the Grafana query editor and more specifically the “Rename by regex” function. (See Transformation functions | Grafana documentation)

For example:
I’d like to plot temperature values as well and I my data is returned like this:
temperature {domain="sensor", entity_id="temperatursensor_arbeitszimmer_temperature"}
Using the regex .*temperatursensor_(.+)_temperature.* (not exactly elegant, I know) I’m able to extract the value that I want.

I just got into InfluxDB and have no idea about the possibilities of the Flux query language yet. It’s quite likely that there is an elegant way to do this in Flux…

2 Likes

Hello,
I catch this thread because I have a question to it:

Of course I can manually put in everywhere a friendly name BUT my dashboard uses variables and I can take several options and my dashboards fill in the responsible data.
E.g. I can choose dataset A, B or C and then the graphs & stats are filled.

If I have to manually do this for all datapoints that is a lot of work.
So i am looking for an option to put also a variable in the displayName field ?
I have all the NAMES available in a datapoint → is there a way to fill out this name automatically depending on which datapoint is set ?