1

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

Gill-Bates
  • 559
  • 8
  • 22

2 Answers2

3

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}))
Munin
  • 1,576
  • 2
  • 19
  • Fixed a little typo in the last line: ``|> map(fn: (r) => ({r with _value: if r._value > 0 then "Idle" else _r.value}))`` – Gill-Bates Mar 09 '23 at 15:03
  • Thank you, this helped me. But now I am facing the issue, that Chronograf does not support Strings in Single-Stats panels: https://github.com/influxdata/influxdb/issues/13124 – Gill-Bates Mar 09 '23 at 15:03
0

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

pascati
  • 9
  • 2