Hi everyone,
In this post we will dive in to integrating Home Assistant with InfluxDB V2, and Grafana to store sensor data and create beautiful, real-time dashboards.
This step-by-step guide is perfect for home automation enthusiasts and IoT developers looking to monitor and visualize sensor data efficiently.
Prerequisites
Before we begin, ensure you have the following set up:
- A working Home Assistant instance.
- InfluxDB v2 installed and accessible (local or cloud-based).
- Grafana installed and running (local or cloud-hosted).
- Basic knowledge of YAML configuration for Home Assistant.
- Access to your InfluxDB and Grafana admin interfaces.
Architecture
To achieve real-time dashboards, we need a time-series database to store sensor data from Home Assistant, which Grafana can then use as a data source. InfluxDB is an open-source, high-performance time-series database, making it ideal for this use case. Home Assistant has a built-in integration for InfluxDB, allowing sensor data to be sent to a specified InfluxDB bucket. Grafana then connects to InfluxDB to create visually appealing dashboards for data visualization.
Below is a high-level architecture diagram:
Step 1 Setup home assistant with InfluxDB integration
The InfluxDB integration in Home Assistant is a legacy integration, meaning it must be configured manually via the configuration file (configuration.yaml
) rather than the UI.
Below, we’ll walk through the steps to set it up.
Refer InfluxDB integration for more details.
Gather InfluxDB Details
You’ll need the following information from your InfluxDB v2 instance:
- Hostname or IP address of your InfluxDB server.
- Port (default is 8086 for InfluxDB v2).
- API Token for authentication.
- Organization ID.
- Bucket Name for storing sensor data.
Finding Your Organization ID
Log in to your InfluxDB v2 web interface. Click your profile in the top-left corner. Select the About tab to view your Organization ID.
Creating Bucket
In the InfluxDB UI, navigate to the Load Data > Buckets section.
Click Create Bucket.
Provide a Bucket Name (e.g., home_assistant_data).
Optionally, set a Retention Period (e.g., 30 days) to manage data storage.
Creating API Token
Go to the Load Data > Tokens section in the InfluxDB UI.
Click Generate API Token > Custom API Token.
Provide a descriptive name (e.g., Home_Assistant_Write).
Assign Write permissions to the bucket you just created. This adheres to the least privilege principle for enhanced security.
Now, update your Home Assistant configuration.yaml
file with the InfluxDB integration settings.
Below is an example configuration that includes specific entities using entity_globs to filter data.
For example, we’ll include Sonoff smart switches and Zigbee2MQTT temperature sensors.
You can customize this to include other entities or entire domains as needed.
influxdb:
api_version: 2
ssl: false
host: <INFLUXDB_IP>
port: <POST default - 8086>
token: <API_TOKEN>
organization: <ORGANIZATION_ID>
bucket: <BUCKET_NAME>
tags:
source: HA
tags_attributes:
- friendly_name
default_measurement: units
include:
entity_globs:
- sensor.sonoff*
- sensor.l1_tah_sensor*
- sensor.l2_tah_sensor*
Replace <INFLUXDB_IP>
, <API_TOKEN>
, <ORGANIZATION_ID>
, and <BUCKET_NAME>
with your InfluxDB details.
Make sure api_version: 2
since we use InfluxDB V2.
The entity_globs section filters specific entities. Adjust this based on your sensors.
The ssl: false
setting assumes a non-SSL connection. Set to true
if your InfluxDB instance uses SSL.
After updating the configuration, restart Home Assistant to apply the changes.
Verify Data in InfluxDB
Log in to the InfluxDB UI.
Navigate to the Data Explorer section.
Select your bucket and filter by the entities you configured (e.g., sensor.sonoff*).
You should see sensor data populating over time.
If data isn’t appearing, verify your configuration, ensure the sensors are sending data, and confirm the API token has the correct permissions.
If you change home assistant integration configurations, you must restart home assistant to take them into effect.
Step 2 Setup InfluxDB integration with Grafana
With data flowing into InfluxDB, it’s time to connect it to Grafana as a data source.
Log in to your Grafana instance.
Navigate to Configuration > Data Sources > Add Data Source.
Select InfluxDB from the list.
Configure the data source with the following details:
- Name: A descriptive name (e.g., Home_Assistant_InfluxDB).
- Query Language: Select Flux (since we use InfluxDB v2).
- URL: Enter your InfluxDB URL (e.g., http://INFLUXDB_IP:8086).
- Organization: Your InfluxDB Organization name.
- Token: Generate a new API token in InfluxDB with Read permissions for your bucket (follow same steps we did earlier. make sure to only provide read permissions).
Click Save & Test. You should see a success message confirming the connection.
Security Tip: Use separate API tokens for Home Assistant (write) and Grafana (read) adhering to least privileges to maintain secure access control.
Step 3 Create Stunning Grafana Dashboards
Now comes the exciting part, building your real-time dashboards in Grafana!
In Grafana, go to Dashboards > Create Dashboard
Add Visualization.
Select your newly created InfluxDB data source.
Write a Flux query to fetch your sensor data. If you’re unsure how to write Flux queries, use the InfluxDB Data Explorer to build and test queries visually.
In InfluxDB’s Data Explorer, select your bucket and desired entities.
Filter and aggregate data as needed (e.g., mean temperature over time).
Click script editor to switch to the Script Editor to view the generated Flux query.
Copy the Flux query from InfluxDB and paste it into Grafana’s query editor.
Click Refresh to visualize the data.
Customize your dashboard:
- Adjust the visualization type (e.g., line graph, gauge, or heatmap).
- Add multiple panels for different sensors (e.g., temperature, humidity, or power usage).
- Use Grafana’s styling options to enhance the look and feel.
Pro Tip: Experiment with Grafana’s Thresholds and Alerts to highlight critical values or receive notifications when sensors report unusual data.
Final Thoughts
Congratulations! 🥳 You’ve successfully integrated Home Assistant with InfluxDB and Grafana to create stunning, real-time dashboards.
From here, your imagination is the only limit build dashboards to monitor energy usage, track indoor climate, or visualize smart home trends over time.
Additional Tips
Optimize Data Retention: Set appropriate retention policies in InfluxDB to manage storage efficiently.
Secure Your Setup: Use SSL for InfluxDB and Grafana in production environments.
Explore Grafana Plugins: Enhance your dashboards with plugins like the Worldmap Panel for geolocation data.
Backup Configurations: Regularly back up your Home Assistant and Grafana configurations to avoid data loss.
For further details, check the official documentation:
Top comments (0)