0

My Influxdb 2 measurement contains a boolean field. I would like to formulate a query that outputs all rows where the field's value is true. An if condition in a filter line does not work.

In detail: I have workout data that includes the boolean field "isIndoor". I want to visualize all values in my dashboard if that field is not true. (I can rename/filter/map values/fields in later steps.)

from(bucket: "mydata")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => if r._boolean == true then
    // use all the fields
  else
    // actually, there shouldn't be an 'else' but Flux insists on it 
  ) 
  // further treatment and filtering

How can this be accomplished?

New edit: I found out that by pivoting the data I can use a filter:

from(bucket: "mydata")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> filter(fn: (r) => r["isIndoor"] == true)

Regrettably, now Grafana no longer recognized the output as graphable. I am now considering to empty the bucket and fill it with my data again but use the boolean value as a tag.

PiEnthusiast
  • 314
  • 1
  • 4
  • 19
  • The latest version supports [experimental.unpivot()](https://docs.influxdata.com/flux/v0.x/stdlib/experimental/unpivot/) function, you could try to decompose pivoted data back to individual series. – alespour Oct 14 '22 at 09:00

1 Answers1

0

It's simple. If your condition == true then return true. In else case just return false

|> filter(fn: (r) => if myBool == true then true else false )

UPD:

|> filter(fn: (r) => r.isIndoor == true)
Amerousful
  • 2,292
  • 1
  • 12
  • 26
  • Thank you for your input. It is appreciated but I'm not sure I understand how this is helping me. I added more details to my questions. – PiEnthusiast Sep 30 '22 at 13:04
  • @PiEnthusiast Ok. Please check new update in the answer – Amerousful Sep 30 '22 at 13:16
  • I am afraid that still doesn't work. I get the field's content (either true or empty) with this `|> filter(fn: (r) =>r._field ="isIndoor")` but using its truthiness as a condition to output all matching rows fails. I get 0. I crosschecked it with "true" to make sure the field does not contain a string but that also doesn't give any results. but – PiEnthusiast Sep 30 '22 at 14:38
  • There is a typo in the above comment (`r._field ="isIndoor"` should have been `r._field =="isIndoor"`) but I had it right in my query. The following works but of course it only outputs the time and the boolean field and not the rest of the data: `|> filter(fn: (r) => r._field == "isIndoor" and r._value == true)` – PiEnthusiast Sep 30 '22 at 16:05