This how-to will take you through setting up Home Assistant (HA) to send logged data to InfluxDB and then access it in Grafana. This will give you access to longer term data logging, statisics and analytics, which allows you to see long term visualizations and trends.
This setup will have a separate instance of Grafana and InfluxDB setup outside of Home Assistant, which provides greater flexibility to the InfluxDB setup.
Prerequisites
This guide assumes you have Home Assistant setup either as a VM or on dedicated hardware and Docker setup on a separate host. If HA is on a dedicated vLAN, the docker host will need an interface connected to that vLAN.
Guide
- Setup InfluxDB in Docker
- Under sources, follow “Install InfluxDB using Docker Compose”
- Change “DOCKER_INFLUXDB_INIT_ORG” to what you would like your organization name to be.
- Change “DOCKER_INFLUXDB_INIT_BUCKET” to “home_assistant”
- Create a bucket to store data in
This is only needed if you do not want to use the bucket automatically created in the previous step or you already have an InfluxDB instance running.- Log into InfluxDB
- On the left, go to Load Data > Buckets
- On the right, click on Create Bucket
- Enter a name and select a retention period
Consider retention periods carefully, as the data and database size can grow quickly, depending on setup.
- Create an API token for Home Assistant with write access to the bucket
Best practices is to create a dedicated API token for each service accessing the bucket with restricted permissions- On the left, go to Load Data > API Tokens
- On the right, click on ”+ Generate API Token”
- Give the token a meaningful description (Home Assistant Access)
- Under “Buckets” find the bucket you will store HA data in, then check off “Write”
- Click “Generate” at the bottom
- Copy the API token, once the box is closed, you will not be able to access it again.
- Send data from HA to InfluxDB
- Log into Home Assistant with an account that has admin permissions
- If you do not already have it, install the Studio Code Server add-on
- In Studio Code Server, open Config > Configuration.yaml
- Add the InfluxDB section (example below) substituting the details for your setup
- Host: IP of the docker host on the same vlan as HA
- Token: This is the API token generated in the previous step
- Organization ID: This can be found in the URL of InfluxDB, at
/orgs/RANDOM_16_DIGIT_HEX_ID
- Bucket: This is the bucket created above.
- Setup Grafana in Docker
- Under sources, follow “Run Grafana Docker image”
- Change the image to “grafana/grafana-oss” if you do not have an enterprise license
- Add the depends on section from the example below (optional)
- Create an API token for Grafana with read access
Best practices is to create a dedicated API token for each service accessing the bucket with restricted permissions
- Go back to InfluxDB
- On the left, go to Load Data > API Tokens
- On the right, click on + Generate API Token > Custom API Token
- Give the token a meaningful description (Grafana)
- Under “Buckets” find the bucket you will want to display graph data from, then check off “Read”
Alternatively, check off Read next to “All Buckets” - Click “Generate” at the bottom
- Copy the API token, once the box is closed, you will not be able to access it again.
- Add InfluxDB to Grafana
- Log into Grafana
- On the left, go to Connections > Add new connection
- Filter by “InfluxDB” and click on it
- At the top, change the query language to Flux
- Fill in the details for your setup
- HTTP:
- URL: http://CONTAINER_NAME:8086
- Auth: Uncheck all of them
- InfluxDB Details:
- Organization: The name of your organization (not the hex ID rom above)
- Token: The token generated in the previous step
- Default Bucket: Leave blank to pull all buckets, specify a bucket to read data from
- HTTP:
- Click “Save & Test” at the bottom to confirm your setup works.
You should now be ready to start building dashboards in Grafana with data from Home Assistant.
Config files
Docker config.yml
services:
influxdb2:
container_name: influxdb
image: influxdb:2
ports:
- 8086:8086
environment:
DOCKER_INFLUXDB_INIT_MODE: setup
DOCKER_INFLUXDB_INIT_USERNAME_FILE: /run/secrets/influxdb2-admin-username
DOCKER_INFLUXDB_INIT_PASSWORD_FILE: /run/secrets/influxdb2-admin-password
DOCKER_INFLUXDB_INIT_ADMIN_TOKEN_FILE: /run/secrets/influxdb2-admin-token
DOCKER_INFLUXDB_INIT_ORG: orion_tech
DOCKER_INFLUXDB_INIT_BUCKET: home_assistant
secrets:
- influxdb2-admin-username
- influxdb2-admin-password
- influxdb2-admin-token
volumes:
- type: volume
source: influxdb2-data
target: /var/lib/influxdb2
- type: volume
source: influxdb2-config
target: /etc/influxdb2
grafana:
image: grafana/grafana-oss
container_name: grafana
restart: unless-stopped
ports:
- "3000:3000"
volumes:
- grafana-storage:/var/lib/grafana
depends_on:
- influxdb2
secrets:
# these files are in the root account's home folder
influxdb2-admin-username:
file: ~/.env.influxdb2-admin-username
influxdb2-admin-password:
file: ~/.env.influxdb2-admin-password
influxdb2-admin-token:
file: ~/.env.influxdb2-admin-token
volumes:
influxdb2-data:
influxdb2-config:
grafana-storage:
Home Assistant configuration.yaml
influxdb:
api_version: 2
ssl: false
host: DOCKER_HOST_IP
port: 8086
token: GENERATED_AUTH_TOKEN
organization: RANDOM_16_DIGIT_HEX_ID
bucket: home_assistant
tags_attributes:
- friendly_name