You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Grafana与InfluxDB:如何批量绘制所有可插拔传感器的时序数据?

Dynamic Sensor Time Series Visualization in Grafana (No Manual Enumeration)

Absolutely! You don’t need to manually list out every sensor ID—Grafana’s built-in tools are perfect for handling dynamic, growing sensor fleets seamlessly. Here’s a step-by-step breakdown of how to set this up:

1. Use Template Variables to Automatically Fetch Sensor IDs

Template variables are the core of this solution—they dynamically pull all existing sensor IDs from your database and update automatically as sensors are added or removed.

  • Create a sensor ID template variable:

    1. Go to your dashboard settings → Variables → Add variable.
    2. Set the variable type to Query.
    3. Configure the query based on your database type:
      • For time-series databases like InfluxDB: SHOW TAG VALUES WITH KEY = "sensor_id" (assuming sensor_id is a tag on your metrics)
      • For SQL databases like PostgreSQL: SELECT DISTINCT sensor_id FROM sensor_data
      • For Prometheus: label_values(sensor_metric, sensor_id)
    4. Under Options, enable the Include All option (so you can select all sensors at once) and set a refresh interval (e.g., "On Dashboard Load" or 5 minutes) to pick up new sensors automatically.
  • Use the variable in your panel queries:
    In your panel’s query editor, reference the variable (e.g., $sensor_id) to fetch data for all selected sensors. Examples:

    • InfluxDB: SELECT "value" FROM "sensor_readings" WHERE "sensor_id" =~ /$sensor_id/ AND $timeFilter
    • PostgreSQL: SELECT time, value FROM sensor_data WHERE sensor_id IN ($sensor_id) AND time BETWEEN $__timeFrom() AND $__timeTo()

    When you select "All" in the variable dropdown, Grafana will automatically plot every sensor’s time series on the same panel.

2. Use Repeat Panels for Per-Sensor Individual Views

If you prefer separate panels for each sensor instead of overlapping lines, use the repeat panel feature:

  • Create a single panel with a query that uses your $sensor_id variable (ensure the variable is set to select a single value by default).
  • Open the panel’s settings → Repeat.
  • Select your sensor_id variable as the repeat source, and choose a direction (horizontal or vertical).
  • Grafana will automatically generate a copy of the panel for every sensor ID. When sensors are added or removed, the dashboard will update the number of panels without manual work.

Key Tips

  • Optimize your data model: Ensure sensor_id is stored as a tag (for time-series DBs) or indexed column (for SQL DBs)—this ensures fast queries and accurate template variable results.
  • Filtering flexibility: The template variable lets users manually select specific sensors to view, so you don’t lose control when going dynamic.
  • Auto-refresh: Set your dashboard or variable to auto-refresh at intervals that match your sensor data update frequency, so new sensors show up without manual intervention.

内容的提问来源于stack exchange,提问作者jdevelop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:53:50