Naming graphs using flux2

Hey all!

I am all new to grafana and flux. I’ve set up my homeassistant to log data into influxdb and have set up a few graphs. While it’s certainly more difficult to find examples for flux than for sql I’ve gotten most of my stuff set up and and I’m just going with the learning curve.

One thing I have so far failed to find anything about - probably due to a lack of knowledge about which terms to use - is the name of graphs. For instance, if I have 3 graphs on one chart for different temperatures in my rooms, the naming of the graphs is like temperature {domain="sensor", entity_id="livingroomsensor_temperature"}
I would really like this to be just Livingroom Temperature or whatever custom string I’d like to choose :slight_smile: I am quite certain this is a very simple task but I struggle to find a simple answer.
Below is my query:

from(bucket: "home")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["domain"] == "sensor")
  |> filter(fn: (r) => r["entity_id"] == "livingroomsensor_temperature")
  |> filter(fn: (r) => r["_field"] == "temperature")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

thank you!

Did you find a solution for this? I’m also trying to work out how to name different data captures on a single graph (legend labels).

no i did not. I am also surprised that there was not a single reply. I’d actually think that this was a common task to do, also surprised how little activity there is in this forum …

Did you try the “Display name” field override?

Oh, no I have not. Unfortunately having no experience with neither grafana nor flux I find this documentation very uncomprehensible. How would I implement this in the above example? I’m sure once I see it it’s very obvious …

Hard to explain without creating a series of screenshots, but basically (assuming you’re on Grafana 7.x):

  1. Open the panel editor and click on the “Overrides” tab on the right
  2. Click the “Add an override for” button; select “Field with name”. In the drop-down select the current name of your field (as yielded by the query)
  3. Click “Add an override property” and select “Display name”
  4. Enter the name you would like to see

There is actually another (maybe preferable) way to do this inside the query itself - which also works for Grafana 6.x. See discussion here, which is basically a discussion of the issue you’re having. This approach involves adding a line like

|> map(fn: (r) => ({_value: r._value, _time: r._time, _field: "My Temperature"}))

before the yield() line, with the name you want. Check the discussion I linked to if you need to do this dynamically for multiple series.

1 Like

Both ways work great! Thank you very much! A real pity the process to getting here was surprisingly counter intuitive. But makes me even happier I finally got this sorted :rocket:
thank you!

1 Like

Thanks for great explanation, but I cannot figure out how to deal with flux code and map function if we have more entities than 1. This is my example code, could you give some hint, thanks:

from(bucket: "ha_bucket")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "°C")
  |> filter(fn: (r) => r["_field"] == "value")
  |> filter(fn: (r) => r["domain"] == "sensor")
  |> filter(fn: (r) => r["entity_id"] == "bathroom_temperature" or r["entity_id"] == "kitchen_temperature" or r["entity_id"] == "living_room_temperature" or r["entity_id"] == "openweathermap_feels_like_temperature" or r["entity_id"] == "openweathermap_forecast_temperature" or r["entity_id"] == "openweathermap_temperature")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

For the Overrides tab in Grafana, it is perfectly clear and working for me - one can add as much want fields to override their values with custom display name, but I would like to learn to do the same from your example in flux.

Thank you.

EDIT: Sorry for bump, I just noticed this post is 3 years old.

Welcome @stiw47 to the Grafana forum.

Are you just wanting to rename the legend values to be more intuitive / less cluttered? Using your above query, what does the graph look like now?

Hi @grant2 , thanks for showing interest in my question!

Yes, I want just to rename legend values to be human readable.
This is how graph looks with above code:

I already fixed them all from Grafana Overrides tab in right panel of graph editing, and this is ok:

But I would like to learn to do this in flux language. To apply map function (sorry if this is not a function, first time installed InfluxDB yesterday :grin:) as @svetb suggested above, but in case when I have multiple fields. @svetb example is working for case when we have one filed, but I cannot figure out the syntax and how to do this for multiple fields.

Thank you.

P.S. I am aware this question is more for some InfluxDB/flux forum or so, but since I saw it is already this topic here, i.e. Google search landed me here :grin:

So this may be possible to do via the map function in Flux, but I have never done it that way. Instead, when the legend labels look bad, I use Rename by Regex transformation in Grafana.

Simple example, as generated by query:

With transformation:

But in your case, I see you are using another language (vs the English language field names). Instead of using kitchen_temperature, etc. in your database, could you use kuhinja_temperatura?

I’m getting your point, didn’t know for this. These values are pulled from my Home Assistant, and I want to have it in English in my home, so unfortunately no go. On the other hand, dashboard is for some local showcase, so I named the fields in local (Serbian) language. Never mind, I will stick with Overrides in Grafana as for now, regex was also dark hole for me whole my life :grin: (but it would be worthy to play little bit and learn it). Thank you for your help, this dashboard is really for one time use, so all good.

EDIT: ChatGPT and me, after several queries, had success to generate a regex for this certain use case :grin::
^value\s{domain="sensor",\sentity_id="(.*)(_humidity")}

image

1 Like