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="", topic="zigbee2mqtt/Schlafzimmer/SENSOR"}
temperature {host="", 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.

1 Like

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.

1 Like