Need Help with Flux Query Errors in InfluxDB

Hello InfluxData Community,

I am working on a web application aimed at visualizing sensor data, similar to how it is displayed in a Grafana dashboard. I am encountering issues specifically with executing Flux queries through my application’s backend using Express.js, intended to dynamically display data based on user-selected sensor IDs from a dropdown menu.

Here’s a breakdown of the functionality and issues:

  • Functionality: In Grafana, I have a dashboard where users can select a sensor ID from a dropdown, and the data (timestamps and presence) are visualized in graphs, while all associated original hex values are displayed in a table.
  • Objective: I aim to replicate this functionality in my own web app, where upon selecting a sensor ID, the app should display corresponding graphs and a table with hex data.
  • Problem: When I try to fetch data through our API for the selected sensor ID, I receive HTTP 400 errors with the message: “compilation failed: error @1:106-1:107: invalid expression.”

Example of the problematic Flux Query:
from(bucket: “Seria Daten”) |> range(start: -7d) |> filter(fn: (r) => r._measurement == “sensor_data” && r.sensor_id == “${sensorId}”) |> pivot(rowKey:[“_time”], columnKey: [“_field”], valueColumn: “_value”)

Here, ${sensorId} is dynamically inserted based on user input from a dropdown menu.

Additional Details:

Using InfluxDB Cloud hosted on AWS.
Errors occur when attempting to pivot data for visualization, similar to what is automatically handled in Grafana.

Could anyone provide insights on how to properly structure this query or handle dynamic insertion of sensor IDs without causing syntax errors? Also, any general advice on replicating Grafana’s dynamic dashboard features in a custom web app would be greatly appreciated.

Thank you in advance for your support and suggestions!

Best regards,
Khaled

@khaled_sehem The error is caused by && which isn’t a valid Flux operator. Use and:

from(bucket: "Seria Daten")
    |> range(start: -7d)
    |> filter(fn: (r) => r._measurement == "sensor_data" and r.sensor_id == "${sensorId}")
    |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")

This will solve your problem. You’re going about this the correct way.

1 Like