Homeassistant and InfluxDB 2.x

I am pretty late in switching from my own handcrafted Flask/file-based solution to HomeAssistant. For this to replace my old solution some problems need to be solved and I will try to write about them in future blog posts. This post is about saving everything into InfluxDB 2.7 which was not as well documented as I hoped.

There are different ways to run HomeAssistant and I chose the systemd/virtualenv one. My host system is a shuttle PC with ArchLinux installed and systemd based services is what I run most. So InfluxDB is a systemd process too installed via pacman (influxdb and influxdb-cli). After that the documentation is vague, contains links to old (InfluxDB) documentation and is not helpful anymore. The ArchLinux package is InfluxDB is version 2.7.1 so the config cannot be based on version 1.7.x.

So the first thing after starting the influxdb service was running setup (on purpose interactive, to see options) to get a user, an org, a bucket and finally a token:

$ influx setup
> Welcome to InfluxDB 2.0!
? Please type your primary username hass
? Please type your password **************
? Please type your password again **************
? Please type your primary organization name hass
? Please type your primary bucket name home_assistant
? Please type your retention period in hours, or 0 for infinite 0
? Setup with these parameters?
  Username:          hass
  Organization:      hass
  Bucket:            home_assistant
  Retention Period:  infinite
 Yes
User    Organization    Bucket
hass    hass            home_assistant

We need to use the organization id and the bucket id later, so lets get them:

$ influx org list | grep hass
5f842d46302a9d7d        hass

$ influx bucket list | grep home_assistant
62302b4f139a4971        home_assistant  infinite        168h0m0s                5f842d46302a9d7d        implicit

And we need a token to read/write to the bucket:

$ influx auth create --org hass --write-bucket 62302b4f139a4971 --read-bucket 62302b4f139a4971
ID                      Description     Token          User Name       User ID                 Permissions
0bd0f47f9dae6000                        <TOKEN>        hass            0bd0f0b490ae6000        [read:orgs/5f842d46302a9d7d/buckets/62302b4f139a4971 write:orgs/5f842d46302a9d7d/buckets/62302b4f139a4971]

Add the token to secrets.yaml and create a influxdb.yaml in your configuration folder. Mine looks like this:

api_version: 2
ssl: false
host: localhost
port: 8086
token: !secret influxdb_token
organization: 5f842d46302a9d7d
bucket: home_assistant
exclude:
  entities:
    - zone.home
  domains:
    - persistent_notification
    - person

And the file is included in the configuration.yaml like this

influxdb: !include influxdb.yaml

After restarting homeassistant and waiting a bit I checked if there is data in influxdb using the API and some very minimal Flux.

curl --request POST "localhost:8086/api/v2/query?org=hass" --header "Authorization: Token <TOKEN>" --header "Content-Type: application/vnd.flux" --data 'from(bucket: "home_assistant") |> range(start: -5m)'

This returns datasets added to the home_assistant bucket in the last 5 minutes.