【问题标题】:Select String value as Integer in Grafana with Influxdb使用 Influxdb 在 Grafana 中选择字符串值作为整数
【发布时间】:2020-03-25 15:45:22
【问题描述】:

我想在时间序列图中将字符串值显示为布尔值或 int 值(数据来自家庭助理)

我有什么:

> select temperature, hvac_action_str from state where entity_id = 'my_entity_id'
name: state
time                temperature hvac_action_str
----                ----------- ---------------
1574189734402651904 20          idle
1574189824323437056 19          idle
1574190158807940864 19          heating
1574190462736049920 19          heating
1574190766798977024 19          idle

我想做这样的事情:

> select temperature, (hvac_action_str == 'heating' ? 1 : 0) as isHeating from state where entity_id = 'my_entity_id'
name: state
time                temperature isHeating
----                ----------- ---------------
1574189734402651904 20          0
1574189824323437056 19          0
1574190158807940864 19          1
1574190462736049920 19          1
1574190766798977024 19          0

这可能吗?

主要目标是创建一个类似this的图表

编辑

我没有设法让它与 InfluxDb 一起使用。我将 homeassistant 的主数据库更改为 PostgreSQL。 使用 PostgreSQL 作为 Grafana 中的数据源,我确实设法使用以下查询获得了所需的图形:

> SELECT
  created AS "time", 
  CAST(attributes::json->>'current_temperature' AS float) AS "current_temperature",
  CAST(attributes::json->>'temperature' AS float) AS "temperature",
  CASE WHEN attributes::json->>'hvac_action' = 'heating' THEN CAST(attributes::json->>'current_temperature' AS float) ELSE 0 END AS "isHeating",
  state AS "state"
FROM states 
WHERE
  entity_id = 'my_thermostat' AND
  $__timeFilter(created)
ORDER BY created;

【问题讨论】:

  • 你有没有得到这个工作?显然,我们都在尝试在格拉法纳重建家庭辅助气候图表。
  • @JohanHenkens,我更新了主帖

标签: grafana influxdb home-automation


【解决方案1】:

不,这是不可能的(至少对于 InfluxQL)。您可以使用 Flux(InfluxDB 的下一代查询语言),您可以在其中尝试strings.countStr() function,例如:

import "strings"

data
  |> map(fn: (r) => ({
      r with
      _value: strings.countStr(v: r.hvac_action_str, substr: "heating")
    })
  )

【讨论】:

【解决方案2】:

如果有人感兴趣,我创建了一个类似的 grafana 查询来读取和显示带有 MySQL 后端的 Home Assistant 的气候恒温器:

SELECT
  created AS "time",
  CASE WHEN JSON_UNQUOTE(JSON_EXTRACT(attributes, '$.hvac_action')) = 'heating' THEN 1 ELSE 0 END AS "isHeating",
  CAST(JSON_UNQUOTE(JSON_EXTRACT(attributes, '$.current_temperature')) AS DECIMAL(3,1)) AS "current_temperature",
  CAST(JSON_UNQUOTE(JSON_EXTRACT(attributes, '$.temperature')) AS DECIMAL(3,1)) AS "target_temperature"
FROM states
WHERE
  entity_id = "climate.main_thermostat" AND
  state <> "unavailable" AND
  $__timeFilter(created)
ORDER BY created

Home Assistant configuration.yaml 登录 MySQL:

recorder:
  #purge_keep_days: 5
  db_url: mysql://dbusername:dbpassword@core-mariadb:3306/dbname?charset=utf8
  exclude:
    domains:
      - automation
      - updater
    entity_globs:
      - sensor.weather_*
    entities:
      - sun.sun # Don't record sun data
      - sensor.last_boot # Comes from 'systemmonitor' sensor platform
      - sensor.date
    event_types:
      - call_service # Don't record service calls

【讨论】:

    【解决方案3】:

    通过计算给定字符串值的结果数量,我让它在 Grafana/Influxdb 中工作: SELECT count(result) AS "result" FROM /.*-.*/ WHERE $timeFilter AND result = 'FAIL' GROUP BY time(10s) fill(null)

    因此,就您的目的而言,它将类似于: select count(hvac_action_str) from state where entity_id = 'my_entity_id' and hvac_action_str != 'idle' GROUP BY time(10s) fill(null)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 2014-04-04
      • 1970-01-01
      • 2020-03-26
      • 2012-04-11
      • 2012-02-26
      • 2017-07-12
      • 2021-09-09
      相关资源
      最近更新 更多