When I completed my SmartThings to Home Assistant migration I forgot one little thing – my InfluxDB integration. Last summer I wrote about sending data from my temperature sensors into InfluxDB. Now my sensors are being managed by Home Assistant instead of SmartThings I need to send the data from Home Assistant.
It’s really, really easy because HA supports InfluxDB out of the box. Once you’ve installed InfluxDB and got it running, all you need to do is create a database using the influx command to get to the InfluxDB command line:
> create database home_assistant
If you use the default name of home_assistant for the database, haven’t set any security and are running the Influx database on the same host as your HA instance, all you then need to do is add the following to your configuration.yaml:
There are parameters you can set in your configuration if you’re running on another host or using authentication, and you can also manage what data is sent to InfluxDB by using the exclude and include options. Both parameters allow you to list entities and domains – i.e. the individual entity IDs that you want to include or exclude, or entire domains, such as sensors and lights.
For example, if you want to send data only for sensors:
influxdb: include: - sensors
If you specify the include option then only things that you include will be sent to InfluxDB.
If you specify the exclude option then all data except that for the excluded entities or domains will be sent to InfluxDB. This can work alongside include – for example, you could include the sensors domain and then exclude individual sensors.
There are some oddities. The measurement name is the unit which means that temperature measurements are literally named “°C”. This leads to some strange – and hard to type – queries:
select time, friendly_name_str, value from "°C" where entity_id = 'bedroom_temperature'
Not such a problem in tools such as Grafana where you can select the name from a list. You’ll need to set up a datasource pointing to your home_assistant InfluxDB database:
You can then select your measurement:
Other measurements include “%”, which you’ll see used for things like luminance and cpu usage. If there is no unit for a state, the measurement name will be the entity ID.
There are two options to change the measurement names. The first overrides all measurement names:
influxdb: override_measurement: state
All measurements will be called “state”. That’s actually a reasonable option because the entity name alone is usually enough to tell what the state value represents.
So, for example, instead of selecting from °C, you can write:
select time, friendly_name_str, value from state where entity_id = 'bedroom_temperature'
The second option only overrides the measurement name when there is no unit:
influxdb: default_measurement: state
With this option, you’ll still get measurements named after the units but states without a unit will be recorded under the measurement “state” instead of the entity_id.
This is really a matter of preference – I marginally prefer using “state” for all measurements largely because it feels weird to use names such as “°C”.
You’ll need to decide how much data to feed into your InfluxDB database. Do you take everything or, for example, just data from sensors? Should you restrict it further to specific entities?
It really depends on how you want to use it and how many entities you have. I’d recommend starting off with everything so that you can explore the data that’s captured and see what you really need. You can also keep an eye on the size of your database.
Once you’ve decided what data you need it’s probably worth clearing down your database and recreating it to clear out all the junk, again at the influx CLI:
> drop database home_assistant > create database_home_assistant
At this point you’ll probably want to set up a default retention policy to automatically delete data over a certain age. For example:
> create retention policy "year" on "home_assistant" duration 52w replication 1 default
You may also want to look into continuous queries to downsample your data, but that’s beyond the scope of this blog.
In my next post I’ll be looking at how you can get more control over the data you send to InfluxDB using a custom component in Home Assistant.in Home Automation