InfluxDB
The InfluxDB integrationIntegrations connect and integrate Home Assistant with your devices, services, and more. [Learn more] lets you transfer all state changes to an external InfluxDB database. This integration supports:
- InfluxDB 3 Core and InfluxDB 3 Enterprise – The latest InfluxDB with v1 and v2 write API compatibility. Sensors query using InfluxQL. Use external tools for SQL.
- InfluxDB 2.x – Including InfluxDB Cloud. Sensors query using Flux.
- InfluxDB 1.x – Sensors query using InfluxQL.
For more information on configuration of InfluxDB, refer to the InfluxDB configuration section below.
There is currently support for the following device types within Home Assistant:
The influxdb database integration runs parallel to the Home Assistant database. It does not replace it.
Configuration
To add the InfluxDB integration to your Home Assistant instance, use this My button:
Manual configuration steps
If the above My button doesn’t work, you can also perform the following steps manually:
-
Browse to your Home Assistant instance.
-
In the bottom right corner, select the
Add Integration button. -
From the list, select InfluxDB.
-
Follow the instructions on screen to complete the setup.
If you have an existing YAML configuration, it will be imported automatically as a config entry. Additional options such as entity filters, tags, and measurement attributes are configured via YAML. For details, refer to the options section below.
InfluxDB 1.x configuration options
InfluxDB 2.x and 3.x configuration options
Organization ID. For InfluxDB 2.x, find this in your installation URL after /orgs. For InfluxDB 3, this value is required but not validated—use any value.
Additional options
Additional options can be configured via YAML.
After changing the configuration.yamlThe configuration.yaml file is the main configuration file for Home Assistant. It lists the integrations to be loaded and their specific configurations. In some cases, the configuration needs to be edited manually directly in the configuration.yaml file. Most integrations can be configured in the UI. [Learn more] file, restart Home Assistant to apply the changes. The integration is now shown on the integrations page under Settings > Devices & services. Its entities are listed on the integration card itself and on the Entities tab.
Configuration Variables
Set this to allow the integration to retry if there was a network error when transmitting data.
Set this to specify the time precision sent to influxdb. Setting a coarser precision allows InfluxDb to compress your data better. If not set, defaults to ns.
State object attribute(s) to use as measurement name. Possible values: unit_of_measurement, domain__device_class or entity_id.
Measurement name to use when the measurement_attr state attribute does not exist, e.g. when an entity doesn’t have a unit.
uses the entity id of the entity
Measurement name to use instead of measurement_attr or default measurement. This will store all data points in a single measurement.
Configure which integrations should be excluded from recording to InfluxDB. (Configure Filter)
Configure which integrations should be included in recordings to InfluxDB. If set, all other entities will not be recorded to InfluxDB. (Configure Filter)
The list of attribute names which should be reported as tags and not fields to InfluxDB. For example, if set to friendly_name, it will be possible to group by entities’ friendly names as well, in addition to their ids.
The list of attribute names to ignore when reporting to InfluxDB. This can be used to filter out attributes that either don’t change or don’t matter to you in order to reduce the amount of data stored in InfluxDB. Please be aware of the underlying InfluxDB mechanism that converts non-string attributes to strings and adds a _str suffix to the attribute name in this case. It means that when you want to ignore, for example, the icon_str attribute that shows in your InfluxDB instance, you need to provide icon to ignore_attributes.
This attribute contains integration-specific override values. See Customizing devices and services for format.
Measurement name to use instead of a unit or default measurement. This will store all data points in a single measurement.
This attribute contains domain-specific integration override values. See Customizing devices and services for format.
Measurement name to use instead of a unit or default measurement. This will store all data points in a single measurement.
This attribute contains integration-specific override values. See Customizing devices and services for format.
Measurement name to use instead of unit or default measurement. This will store all data points in a single measurement.
Configure filter
By default, no entity will be excluded. To limit which entities are being exposed to InfluxDB, you can use the include and exclude parameters.
# Example filter to include specified domains and exclude specified entities
influxdb:
include:
domains:
- alarm_control_panel
- light
entity_globs:
- binary_sensor.*_occupancy
exclude:
entities:
- light.kitchen_light
Filters are applied as follows:
- No filter
- All entities included
- Only includes
- Entity listed in entities include: include
- Otherwise, entity matches domain include: include
- Otherwise, entity matches glob include: include
- Otherwise: exclude
- Only excludes
- Entity listed in exclude: exclude
- Otherwise, entity matches domain exclude: exclude
- Otherwise, entity matches glob exclude: exclude
- Otherwise: include
- Domain and/or glob includes (may also have excludes)
- Entity listed in entities include: include
- Otherwise, entity listed in entities exclude: exclude
- Otherwise, entity matches glob include: include
- Otherwise, entity matches glob exclude: exclude
- Otherwise, entity matches domain include: include
- Otherwise: exclude
- Domain and/or glob excludes (no domain and/or glob includes)
- Entity listed in entities include: include
- Otherwise, entity listed in exclude: exclude
- Otherwise, entity matches glob exclude: exclude
- Otherwise, entity matches domain exclude: exclude
- Otherwise: include
- No Domain and/or glob includes or excludes
- Entity listed in entities include: include
- Otherwise: exclude
The following characters can be used in entity globs:
* - The asterisk represents zero, one, or multiple characters
? - The question mark represents zero or one character
Examples
Example 1
influxdb:
max_retries: 3
default_measurement: state
exclude:
entities:
- entity.id1
- entity.id2
domains:
- automation
include:
entities:
- entity.id3
- entity.id4
tags:
instance: prod
source: hass
Example 2
influxdb:
tags:
source: HA
tags_attributes:
- friendly_name
default_measurement: units
exclude:
entities:
- zone.home
domains:
- persistent_notification
- person
include:
domains:
- sensor
- binary_sensor
- sun
entities:
- weather.home
InfluxDB configuration
Authentication
- InfluxDB 3: Authentication optional (enabled by default). Token can be generated via InfluxDB.
- InfluxDB 2.x: Requires authentication. Token can be generated via InfluxDB.
- InfluxDB 1.x: Authentication optional (disabled by default). If running on the same host with default settings, leave user and password empty.
For InfluxDB 1.x, you must first create a database named home_assistant. InfluxDB 2.x and 3.x create buckets/databases automatically.
InfluxDB 3 (Core and Enterprise)
See how to get started using InfluxDB 3:
- InfluxDB 3 Core: Free, open-source, optimized for recent data queries.
- InfluxDB 3 Enterprise: Adds compaction for historical queries. Includes a free At-Home license for non-commercial use.
Write API compatibility
InfluxDB 3 Core and Enterprise provide InfluxDB v1 and v2 write API compatibility, allowing you to write data using api_version: 2.
Query API compatibility
InfluxDB 3 supports the v1 query API (InfluxQL) and v3 query API (SQL and InfluxQL). The v2 query API (Flux) is not supported.
Tools for querying: Query InfluxDB 3 using SQL or InfluxQL with external tools such as InfluxDB 3 Explorer or Grafana.
Generate tokens using the influxdb3 CLI or InfluxDB 3 Explorer.
Sensor
The influxdb sensor lets you query values from an InfluxDB database to populate a sensor state. Use this to present statistics as Home Assistant sensors from the influxdb history integration or an external data source.
InfluxDB 3 sensor support: InfluxDB 3 supports the v1 query API (InfluxQL), so 1.x sensors using queries: may work. The v2 query API (Flux) is not supported—queries_flux: sensors don’t work with InfluxDB 3.
Tools for querying: Query InfluxDB 3 using SQL or InfluxQL with external tools such as InfluxDB 3 Explorer or Grafana.
You must configure the influxdb history integration to create influxdb sensors. To create sensors for an external InfluxDB database without writing data to it, exclude all entities:
influxdb:
exclude:
entity_globs: "*"
Sensor configuration
Define the sensor connection variables and queries in your configuration.yamlThe configuration.yaml file is the main configuration file for Home Assistant. It lists the integrations to be loaded and their specific configurations. In some cases, the configuration needs to be edited manually directly in the configuration.yaml file. Most integrations can be configured in the UI. [Learn more] file. A sensor is created for each query.
InfluxDB 1.x sensors (InfluxQL)
sensor:
- platform: influxdb
queries:
- name: mean value of foo
where: '"name" = ''foo'''
measurement: '"°C"'
InfluxDB 2.x sensors (Flux)
InfluxDB 2.x requires queries in Flux. Use the query builder in the InfluxDB UI to construct queries:
sensor:
- platform: influxdb
api_version: 2
organization: RANDOM_16_DIGIT_HEX_ID
token: GENERATED_AUTH_TOKEN
queries_flux:
- group_function: mean
imports:
- strings
name: "Mean humidity reported from past day"
query: >
filter(fn: (r) => r._field == "value" and r.domain == "sensor" and strings.containsStr(v: r.entity_id, substr: "humidity"))
|> keep(columns: ["_value"])
range_start: "-1d"
Sensor configuration variables
Configuration Variables
Use HTTPS instead of HTTP. Defaults to true for 2.x, false for 1.x.
IP address or domain of your database host. For InfluxDB Cloud, defaults to us-west-2-1.aws.cloud2.influxdata.com.
1.x only - Database name. Individual sensors can override this.
1.x only - Verify SSL certificate. For 2.x, SSL verification is always enabled.
2.x only - Organization ID. Find this in your InfluxDB URL after /orgs.
2.x only - Bucket name. Individual sensors can override this.
1.x only - List of sensors using InfluxQL queries.
The unique ID for this query. This allows changing the name, icon and entity_id from the web interface.
Defines the measurement name in InfluxDB (the FROM clause of the query).
Defines the data selection clause (the where clause of the query). This supports templates.
2.x only - List of sensors using Flux queries.
The unique ID for this query. This allows changing the name, icon and entity_id from the web interface.
Duration or time value to start range from. All Flux queries require a range filter, one is automatically added to the beginning of your Flux query in the form of range(start: {range_start}, stop: {range_stop}).
Duration or time value to stop range at. See range_start above for how this is used in query.
One or more flux filters used to get to the data you want. These should limit resultset to one table, or any beyond the first will be ignored. Your query should not begin or end with a pipe (|>). This supports templates.
The group function to be used. If provided, adds {group_function}(column: "_value") to your query. Unlike 1.x queries, this does not default to mean. If omitted, limit(n: 1) is added instead.
Defines a template to extract a value from the payload. Note that value will be set to the value of the _value field in your query output.
Name of the bucket within your Organization to read from.
Examples
Full configuration for InfluxDB 1.x
The example configuration entry below creates two requests to your local InfluxDB instance, one to the database db1, the other to db2:
select last(value) as value from "°C" where "name" = "foo"select min(tmp) as value from "%" where "entity_id" = ''salon'' and time > now() - 1h
sensor:
- platform: influxdb
host: localhost
username: home-assistant
password: password
queries:
- name: last value of foo
unit_of_measurement: °C
value_template: '{{ value | round(1) }}'
group_function: last
where: '"name" = ''foo'''
measurement: '"°C"'
field: value
database: db1
- name: Min for last hour
unit_of_measurement: "%"
value_template: '{{ value | round(1) }}'
group_function: min
where: '"entity_id" = ''salon'' and time > now() - 1h'
measurement: '"%"'
field: tmp
database: db2
Full configuration for InfluxDB 2.x
sensor:
- platform: influxdb
api_version: 2
token: GENERATED_AUTH_TOKEN
organization: RANDOM_16_DIGIT_HEX_ID
bucket: BUCKET_NAME
queries_flux:
- range_start: "-1d"
name: "How long have I been here"
query: >
filter(fn: (r) => r._domain == "person" and r._entity_id == "me" and r._value != "{{ states('person.me') }}")
|> map(fn: (r) => ({ _value: r._time }))
value_template: "{{ relative_time(strptime(value, '%Y-%m-%d %H:%M:%S %Z')) }}"
- range_start: "-1d"
name: "Cost of my house today across all power sensor"
query: >
filter(fn: (r) => r.domain == "sensor" and r._field == "value" and regexp.matchRegexpString(r: /_power$/, v: r.entity_id))
|> keep(columns: ["_value", "_time"])
|> sort(columns: ["_time"], desc: false)
|> integral(unit: 5s, column: "_value")
imports: regexp
value_template: "{{ value|float / 24.0 / 1000.0 * states('sensor.current_cost_per_kwh')|float }}"
- range_start: "-1d"
bucket: Glances Bucket
name: "Average CPU temp today"
query: "filter(fn: (r) => r._field == \"value\" and r.entity_id == \"glances_cpu_temperature\")"
group_function: mean
Note that when working with Flux queries, the resultset is broken into tables, you can see how this works in the Data Explorer of the UI. If you are operating on data created by the InfluxDB history integration, this means by default, you will have a table for each entity and each attribute of each entity (other than unit_of_measurement and any others you promoted to tags).
This is more tables compared to 1.x queries, where you have one table per unit_of_measurement across all entities. You can still create aggregate metrics across multiple sensors. As shown above, use the keep or drop filters. When you remove key columns, InfluxDB merges tables that share a schema for _value into one.
Querying your data in Influx
Sensors
For sensors with a unit of measurement defined, the unit of measurement is used as the measurement name and entries are tagged with the second part of the entity_id. Therefore you need to add a WHERE clause to the query to filter out values.
For example a query on a % battery for sensor.multi_sensor_battery_level:
SELECT * FROM "%" WHERE time > now() - 12h AND "entity_id" = 'multi_sensor_battery_level';
Or for temperatures represented in °C:
SELECT * FROM "°C" WHERE time > now() - 1h;
Everything else
Everything else can be queried using the entity_id as its measurement name.
SELECT * FROM "binary_sensor.front_doorbell" WHERE time > now() - 24h;
SELECT "temperature" FROM "climate.kitchen" WHERE time > now() - 24h;