Prometheus
The prometheus
integration集成将 Home Assistant 与您的设备、服务等连接和集成。 [Learn more] exposes metrics in a format which Prometheus
Configuration
To use the prometheus
integration集成将 Home Assistant 与您的设备、服务等连接和集成。 [Learn more] in your installation, add the following to your configuration.yaml
configuration.yaml 文件是 Home Assistant 的主要配置文件。它列出了要加载的集成及其特定配置。在某些情况下,需要直接在 configuration.yaml 文件中手动编辑配置。大多数集成可以在 UI 中配置。 [Learn more] file.
在更改了configuration.yaml
configuration.yaml 文件是 Home Assistant 的主要配置文件。它列出了要加载的集成及其特定配置。在某些情况下,需要直接在 configuration.yaml 文件中手动编辑配置。大多数集成可以在 UI 中配置。 [Learn more] 文件后,重启 Home Assistant 以应用更改。
# Example configuration.yaml entry
prometheus:
Configuration Variables
The “namespace” that will be assigned to all the Prometheus metrics. This is the prefix of the metric name. E.g., having myhass
as the namespace will cause the device tracker metrics to be myhass_device_tracker_state
, the switch metrics to be myhass_switch_state
and so on. The default is to not add any prefix to the metrics name. (available in version 0.73.0 and later)
Filtering directives for the integrations which should be included or excluded from recording. (Configure Filter)
Exclude all entities matching a listed pattern (e.g., sensor.weather_*
).
The list of entity ids to be included from recordings. If set, all other entities will not be recorded. Values set by the exclude_* option will prevail.
Include all entities matching a listed pattern (e.g., sensor.weather_*
). If set, all other entities will not be recorded. Values set by the exclude_* option will prevail.
Metric name to use when an entity doesn’t have a unit.
uses the entity id of the entity
Metric name to use instead of unit or default metric. This will store all data points in a single metric.
This attribute contains integration-specific override values. See Customizing devices and services for format.
This attribute contains domain-specific integration override values. See Customizing devices and services for format.
This attribute contains integration-specific override values. See Customizing devices and services for format.
Configure Filter
By default, no entity will be excluded. To limit which entities are being exposed to Prometheus
, you can use the filter
parameter.
# Example filter to include specified domains and exclude specified entities
prometheus:
filter:
include_domains:
- alarm_control_panel
- light
include_entity_globs:
- binary_sensor.*_occupancy
exclude_entities:
- light.kitchen_light
Filters are applied as follows:
- 不使用过滤器
- 包含所有实体
- 仅包含
- 实体列表中的实体包含:include
- 否则,实体匹配域包含:include
- 否则,实体匹配通配符包含:include
- 否则:排除
- 仅排除
- 实体列表中的实体排除:exclude
- 否则,实体匹配域排除:exclude
- 否则,实体匹配通配符排除:exclude
- 否则:包含
- 域和/或通配符包含(还可能有排除)
- 实体列表中的实体包含:include
- 否则,实体列表中的排除:exclude
- 否则,实体匹配通配符包含:include
- 否则,实体匹配通配符排除:exclude
- 否则,实体匹配域包含:include
- 否则:排除
- 域和/或通配符排除(没有域和/或通配符包含)
- 实体列表中的实体包含:include
- 否则,实体列表中的排除:exclude
- 否则,实体匹配通配符排除:exclude
- 否则,实体匹配域排除:exclude
- 否则:包含
- 没有域和/或通配符的包含或排除
- 实体列表中的实体包含:include
- 否则:排除
以下字符可用于实体通配符:
*
- 星号表示零个、一个或多个字符
?
- 问号表示零个或一个字符
Full Example
Advanced configuration example:
# Advanced configuration.yaml entry
prometheus:
namespace: hass
component_config_glob:
sensor.*_hum:
override_metric: humidity_percent
sensor.*_temp:
override_metric: temperature_c
sensor.temperature*:
override_metric: temperature_c
sensor.*_bat:
override_metric: battery_percent
filter:
include_domains:
- sensor
exclude_entity_globs:
- sensor.weather_*
You can then configure Prometheus to fetch metrics from Home Assistant by adding to its scrape_configs
configuration.
# Example Prometheus scrape_configs entry
- job_name: "hass"
scrape_interval: 60s
metrics_path: /api/prometheus
# Legacy api password
params:
api_password: ['PASSWORD']
# Long-Lived Access Token
bearer_token: "your.longlived.token"
scheme: https
static_configs:
- targets: ['HOSTNAME:8123']
Replace your.longlived.token
with a Home Assistant generated token.
The format to configure the bearer token has changed in Prometheus 2.26, so if you have a newer version, you can use this configuration sample:
# Example Prometheus scrape_configs entry (For version 2.26+
- job_name: "hass"
scrape_interval: 60s
metrics_path: /api/prometheus
# Long-Lived Access Token
authorization:
credentials: "your.longlived.token"
scheme: https
static_configs:
- targets: ['HOSTNAME:8123']
When looking into the metrics on the Prometheus side, there will be:
- All Home Assistant domains, which can be easily found through the common namespace prefix, if defined.
- The client library
provided metrics, which are a bunch of process_* and also a single pseudo-metric python_info which contains (not as value but as labels) information about the Python version of the client, i.e., the Home Assistant Python interpreter.
Typically, you will only be interested in the first set of metrics.
Metrics in unavailable or unknown states
When the Prometheus exporter starts (typically when Home Assistant starts), all non-excluded entities in an unavailable or unknown state are not exported until they are available and known.
If an available entity goes into state unavailable or unknown, then it will automatically be unexported and return again automatically when available and known.
To filter out these stale values, entity_available
could be used in a query or recording rule. For example:
- record: "known_temperature_c"
expr: "temperature_c unless entity_available == 0"
This use of unless
(which can be slow to compute) is no longer necessary, but will continue to work.
Supported metrics
Metrics are exported only for the following domains:
alarm_control_panel
, automation
, binary_sensor
, climate
, cover
, counter
, device_tracker
, fan
, humidifier
, input_boolean
, input_number
, light
, lock
, number
, person
, sensor
, switch
, update