Gill-Bates
Gill-Bates

Reputation: 679

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

Upvotes: 1

Views: 659

Answers (2)

pascati
pascati

Reputation: 9

this is what I used to transform 0 and 1 to "night" and "day" The toString() was the solution for me, it didn't like to use Integer type for the condition in the map. I needed also to select under 'value options -> Fields' the 'Tarif' field, which will also solve your other issue about string vs int in the panel.

    from(bucket: "p1_meter")
  |> range(start: -15m)
  |> filter(fn: (r) =>
    r._measurement == "P1_measurements" and
    r._field == "tarif"
  )
  |> last()
  |> toString()
  |> map(fn: (r) => ({r with _value: if r._value == "0" then "night" else "day" }))

You could use this to your analogy as well:

...
|> last()
|> toString()
|> map(fn: (r) => ({r with _value: if r._value == "0" then "IDLE" else _value }))

panel value options

Upvotes: 0

Munin
Munin

Reputation: 1649

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}))

Upvotes: 4

Related Questions