Read external InfluxDB in Home Assistant as a sensor

Published Updated 5 years ago

Setting up the environment

During a current project I looked into HomeAssistant as a central component / hub for home automation. When starting out with HomeAssistant it is recommended to install the software either as a OS distribution (HASS OS) or run a supervised installation, which is just a couple of fancy bash scripts that orchestrate multiple docker containers.

I decided to go with neither approach, as I already have a Intel NUC running as a home server and I don’t want any third party scripts to interfere with my system and other containers. So I ran Home Assistant (core) as a dedicated docker container with the official image with a docker-compose file.

  homeassistant:
    container_name: homeassistant
    image: homeassistant/home-assistant:2021.2.1
    volumes:
      - /media/enc/docker-volumes/home-assistant/:/config
    environment:
      - TZ=Europe/Berlin
    restart: always
    network_mode: host

It’s necessary to run Home Assistant in the host network, as it needs access to devices and the network for auto discovery (if this is something you want to use).

Addons without Supervisor

When running the supervised (or HASS OS) variant of Home Assistant you will find an addon store on the web ui. This entry is missing when running the core container image of Home Assistant. This is due to a design decision, because addons are just more docker containers which are pulled and started by the supervisor (the fancy script). So there is a InfluxDB addon (in the community addon store), but this addon just starts up a InfluxDB container. It is not needed to use the integration of InfluxDB. This is not easy to understand when starting out with Home Assistant, because the documentation is not very clear about this.

Adding the InfluxDB sensor

As I already had an InfluxDB (version 1) running, into which some devices write data, I have searched for a way to pull this data into Home Assistant. My first idea was to render the graphs based in Grafana with an external renderer and display the image in Home Assistant, which works fine, but I like the design of the embedded graphs as well.

As mentioned the integration of InfluxDB works without the need for an addon. So in your configuration.yaml you do have to setup the InfluxDB integration. If you just add the integration without an exclude, Home Assistant will begin to write data into InfluxDB.

influxdb:
  host: 172.23.0.2
  verify_ssl: false
  exclude:
    entity_globs: "*"

The host IP is the IP of the running InfluxDB container. As Home Assistant runs on the host network (and my InfluxDB container does not) name resolution is not working. So it is necessary to enter the current IP of the container. To avoid having to re-enter a changing IP, when the container restarts, I will probably publish the port of InfluxDB only to localhost and enter the port with localhost in the configuration.yaml. I may publish another blog post in the future about that. The page of the InfluxDB integration has some information on how to use it as a sensor (which is what I’m doing here). However the information given there seems not to be correct or difficult to understand.

There is an example which references the property measurement in this way: measurement: '"°C"' However measurement is documented to: Defines the measurement name in InfluxDB (the FROM clause of the query). These are entirely different things, as the example implies this is the type of data, not the name of it.

I can highly advise using Chronograf to get the working queries to add them to the sensor in the configuration.yaml of Home Assistant. In my example I queried 2 entries, one for a temperature and one for a humidity level. My sensor looks like follows.

sensor:
  - platform: influxdb
    queries:
      - name: ESP32-Temperature
        field: temperature
        where: 'time > now() - 1d'
        group_function: last
        database: sensors
        measurement: '"sensors"."autogen"."home"'
      - name: ESP32-Humidity
        field: humidity
        where: 'time > now() - 1d'
        group_function: last
        database: sensors
        measurement: '"sensors"."autogen"."home"'

With Chronograf it’s easy to see which are the parts of the query to use for the measurement, as depicted below.

For my first try to get the data into Home Assistant I decided to use the group_function: last which always retrieves the last known entry from the result set. In this setting I imagine it’s not necessary to include a where clause (as it is not necessary when trying it out in Chronograf) but the sensor / Home Assistant will display errors, when this is not added.

After setting up the configuration.yaml you do have to restart Home Assistant for the changes to be applied. Add a new sensor and choose the entity as added in the configuration.yaml.

The data displayed will be just a straight line, as you only get a a single measurement (the latest) at this time. After some time you will see changes in the graph, as it has more data to display.

This approach will query only the latest data and not a specific timeframe to display it as a sensor. I’m sure this is possible as well, but in my first try I did not get it to work the way I want it to. If I decide to change it, I will update this post.

Comments

Comments are hosted on comments.itobey.dev. Loading them sets cookies and shares your IP address with that service.