influxqlchronograf

How to transform Numbers to strings in InfluxDB 1.x Dashboards?


The visualization templates I have for creating a dashboard in Chronograf always expect numbers (except table). How can I make it so that I get text instead of a number in certain cases?

For instance:

The logic I want implement is:

if result eq 0 then return "Idle" else return value

That's my Influx-Query, which results into 0.

SELECT last("DischargingBattery") AS "DischargingBattery" FROM "wallbox"."autogen"."battery"

This ends up into this result on the Dashboard:

enter image description here

When this query result into 0, I want to see the string "Idle" on my Dashboard instead of a number Zero.

That is what I would like to achieve:

enter image description here


Solution

  • Using InfuxQL might not be able to help you there.

    You could try Flux which is InfluxData’s new functional data scripting language designed for querying, analyzing, and acting on data. See flux documentation: https://docs.influxdata.com/flux/v0.x/

    Map function: https://docs.influxdata.com/flux/v0.x/stdlib/universe/map

    Step 1: enable Flux in 1.X via this doc: https://docs.influxdata.com/influxdb/v1.8/flux/installation/ Step 2: Change your query similar to following:

    from(bucket: "wallbox/autogen")
    |> range(start: "2022-02-22T20:22:02Z", stop: now())
    |> filter(fn: (r) => r._measurement == "battery")
    |> filter(fn: (r) => r._field == "DischargingBattery")
    |> last()
    |> map(fn: (r) => ({r with _value: if r._value > 0 then "Idle" else _value}))