How to map values to percentages?

I’m running Grafana 11.2 on a Raspberry Pi 4B

I want to have a % display of how full my water tank is.
the sensor measures in ohms from 0 to 246.

On a dashboard i would like to see 0% (empty) through to 100% (full) at 1 % intervals.
I want to show this on a gauge, and also on a timeline so we can see how much water is being used when.
I assumed i would be able to use something in Value Mapping, but i cant see how i could make that work there… is there something within the query that needs to be added?

Current query is below, that shows the ohms value

from(bucket: "db1")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "tanks.fuel.fuel starboard.senderResistance")
  |> filter(fn: (r) => r["context"] == "vessels.urn:mrn:signalk:uuid:ea14781e-5714-4706-8f81-24b2a995e8a1")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> yield(name: "last")

thanks
Colin

Add this line below |> aggregateWindow and above |> yield

|> map(fn: (r) => ({ r with "Percentage ( % )" : float(v: r.column01) / float(v: r.column02) * 100.0 }))

thanks for the super fast response. I tried what you said but im not getting the correct value. The value in the dashboard is 2.48.
The current ohm value is 2.478027344
It looks like the output is a truncated version of the ohm value?

query:

from(bucket: "db1")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "tanks.fuel.fuel starboard.senderResistance")
  |> filter(fn: (r) => r["context"] == "vessels.urn:mrn:signalk:uuid:ea14781e-5714-4706-8f81-24b2a995e8a1")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> map(fn: (r) => ({ r with "Percentage ( % )" : float(v: r.column01) / float(v: r.column02) * 100.0 }))
  |> yield(name: "last")```

thanks

np :slight_smile:

ps. You do have to change r.column01 and r.column02 with your own fields/colums :wink:
I wasn’t sure for what values you wanted to calc. percentage so I used those for the example

So in your case something like?

|> map(fn: (r) => ({ r with "Percentage ( % )" : float(v: r["_field"]) / float(v: r["_measurement"]) * 100.0 }))

and other wise r._field instead of r["_field"]

Maybe you have to add “override” to the new mapped column to get the correct amount of decimals.

Hi, I don’t think it’s a decimal issue.
The original (ohm) value is 2.48, the percentage for this should be around 8% i think, given the full range is 0-246.

I’m not getting the 8% though. Did I amend the query correctly?

@colinb6d1
Hmmm is that approach correct?
If the full range is 246 meaning that is 100% , then 1% == 2.46.
2.48 / 246 * 100 = 1.008130081300813 %

So if it should be closer to 8% something in your query is off.

aha i missed this msg :frowning:

i’m still confused with the query statement itself.

i know my minimum is 0 and my max is 245.
i know i want to map 0 - 0 and 265 - 100 and everything in between.

In the query you suggested, what specifies that the max is 245?

i used this:

from(bucket: "db1")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "tanks.fuel.fuel starboard.senderResistance")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> map(fn: (r) => ({
      r with _value: (r._value / 246.0) * 100.0
  }))
  |> yield(name: "last")

which gave me the outcome i was looking for :slight_smile: thanks for your help though.