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

如何通过Telegraf从Tibber API获取电价并导入InfluxDB?

配置Telegraf从Tibber API获取实时电价

核心配置片段

在Telegraf的配置文件中添加如下[[inputs.http]]模块,实现从Tibber API拉取并解析电价数据:

[[inputs.http]]
  # Tibber GraphQL API端点
  urls = ["https://api.tibber.com/v1-beta/gql"]
  # 使用POST请求提交GraphQL查询
  method = "POST"
  # 身份验证与内容类型头配置
  headers = {
    "Authorization" = "Bearer YOUR_TIBBER_API_TOKEN",
    "Content-Type" = "application/json"
  }
  # GraphQL查询:获取当前、当日及次日电价数据
  body = '''
  {
    "query": "{ viewer { homes { id currentSubscription { priceInfo { current { total startsAt } today { total startsAt } tomorrow { total startsAt } } } } } }"
  }
  '''
  # 指定数据格式为JSON
  data_format = "json"
  # 解析并转换响应数据至InfluxDB兼容格式
  [[inputs.http.json_query]]
    tag_keys = ["homeId"]
    field_keys = ["current_price", "current_start_time"]
    # 用jq语法提取目标数据
    query = '''
    .data.viewer.homes[] | {
      homeId: .id,
      current_price: .currentSubscription.priceInfo.current.total,
      current_start_time: .currentSubscription.priceInfo.current.startsAt
    }
    '''

关键注意事项与设置建议

  • API令牌替换: 登录Tibber账户生成专属API令牌,替换配置中的YOUR_TIBBER_API_TOKEN,确保令牌拥有数据读取权限。
  • 查询按需精简: 根据实际需求修改GraphQL查询内容,比如仅保留current节点减少数据量,避免不必要的传输。
  • 轮询间隔配置: 在[[inputs.http]]模块中添加interval = "1h"(或更短如15m),注意不要超过Tibber API的请求频率限制。
  • jq语法适配: 若Tibber API响应结构更新,需同步调整json_query中的jq语句,保证数据正确提取。
  • 标签字段规划: 合理设置tag_keys(如homeId区分不同住宅)和field_keys,便于后续在InfluxDB中做维度分析。
  • 错误监控: 开启Telegraf日志功能,及时排查请求失败问题(如令牌过期、网络中断等)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 20:17:19