Complete guide on setting up Grafana/InfluxDB with Home assistant using official Docker images

I know there are already a few tutorials on setting up InfluxDB and Grafana with Home Assistant, but they did not meet my requirements. Specifically, I did not want to use third-party images, which may not be maintained, and not use panel iframes to display the plots. Moreover, many of the tutorials still show the InfluxDB admin GUI, which is no longer available. So, here’s my version of how to get everything set up with the official images.

  1. Run the following command to start the InfluxDB container (change /volume1/docker/influxdb/ to the desired path):

    docker run -d \
    --name="influxdb" \
    --restart always \
    -p 8086:8086 \
    -p 8083:8083 \
    -v /volume1/docker/influxdb/:/var/lib/influxdb \
    influxdb
    
  2. Now that the InfluxDB web UI is deprecated, run the Influx client using:

    docker exec -it influxdb influx
    
  3. Create a new database for Home Assistant and exit the Influx client:

    CREATE DATABASE home_assistant
    exit
    
  4. Start Grafana container (change /volume1/docker/grafana to the desired path):

    docker run -d -p 3000:3000 \
    --name="grafana" \
        -v /volume1/docker/grafana:/var/lib/grafana \
        grafana/grafana
    

    image

  5. Add InfluxDB to Home Assistant and restart Home Assistant. I am using the default settings, but feel free to change them. I am adding only two entities to start off.

    influxdb:
      host: 192.168.2.113
      include:
        entities:
           - sensor.temperature_158d0001ab3b2b
           - sensor.humidity_158d0001ab3b2b
    
  6. Now that Home Assistant is communicating with InfluxDB (check for errors, if any, in Home Assistant logs), we can configure Grafana. Navigate to http://IP_ADDRESS:3000 and you should see the Grafana login screen. Login with root:root.

  7. Next, configure InfluxDB as the data source in Grafana. If you have followed along, here’s how it should look like (assuming no password)

  8. If you have not made any mistakes, you should see Data source is working when you click Add.

  9. Now you are ready to add your dashboard and create your first chart. Since there are several tutorials on the web for the same, I am not going to discuss adding charts in Grafana here.

  10. After you have created your charts, we now want to include them in Home Assistant. You can use Panel iframes as discussed here. Alternately ( and this is the method that I prefer), we can export the plots as PNG images and use the local_file camera component to display them inside Home Assistant. Thanks @fgabriel for the inspiration.

  11. You can follow the tutorial here to obtain the API key and the URLs for our plots. Basically, obtain the API key from the Grafana dashboard
    image
    Obtain the Direct link rendered image URL for the plots that you want to be included by selecting the graph in a dashboard and clicking on Share.


    Next, get the the Direct link rendered image URL

  12. Once you have the API key and the URL, you can add the following command_line sensor (thanks @skalavala for the tip) to periodically download the PNG image from Grafana and save it under /home/homeassistant/.homeassistant/downloads/temperature.png. You can change the URL and its parameters to configure the plot. For example, you can use from=now-30d&to=now to obtain the plot for the last 30 days.

      sensor:
        - platform: command_line
          name: "Download Grafana"
          command: curl -s -H "Authorization: Bearer API_KEY" "http://192.168.2.113:3000/render/dashboard-solo/db/home-assistant?orgId=1&panelId=1&width=1000&height=500&tz=UTC-05%3A00" > /home/homeassistant/.homeassistant/downloads/temperature.png
    
  13. Next, you can add a local_file camera to Home Assistant to display the images.

      camera:
        - platform: local_file
          name: Grafana_temperature
          file_path: /home/homeassistant/.homeassistant/downloads/temperature.png
    
  14. It should now appear as a camera in HA.

Other notes:

  1. You can check the version of your InfluxDB container by running docker exec -it influxdb influx.
    image
  2. For Grafana, you can navigate to any page go to any page that is not a dashboard (Data Sources page for example) and look at the footer:
    image
    or you can use grafana-cli -v after entering the Grafana container using docker exec -it grafana /bin/bash.
    image
  3. To update images, a) stop the respective Docker containers using docker stop grafana or docker stop influxdb; b) pull the latest image using docker pull grafana/grafana or docker pull influxdb; and c) restart Docker containers.
  4. You can use the same graphana-cli to install plugins. For example, to install Pie Chart plugin, just run the required command grafana-cli plugins install grafana-piechart-panel in the Grafana container and restart the container.
  5. @amelchio just pointed out that we can also use generic camera component that does not require API keys. Just create a user in Grafana with viewer rights and add the following with the Direct link rendered image URL from step 11 above.
    camera:
      - platform: generic
        name: Grafana_2
        still_image_url: 'http://192.168.2.113:3000/render/dashboard-solo/db/home-assistant?orgId=1&panelId=1&width=1000&height=500&tz=UTC-05%3A00'
        username: !secret grafana_user
        password: !secret grafana_pass
    

Let me know if something is missing or not clear and I will be sure to modify the instructions.

79 Likes

Great article, @arsaboo!

1 Like

I agree! Thank you for sharing, @arsaboo !

I had the same requirements (and the same observation about outdated tutorials on the web) a while back when I set up InfluxDB and Grafana. As it was my first real encounter with Docker, it took some trial and error to get it right. On top of that, I used docker-compose to put it all together.

As I’m not good at putting together guides/tutorials like this one, I’ll just share my docker-compose.yml file for anyone who might be interested:

version: '2'

services:
  influxdb:
    image: influxdb:latest
    container_name: influxdb
    network_mode: host
    volumes:
      - ./influxdb:/var/lib/influxdb
      - ./influxdb/influxdb.conf:/etc/influxdb/influxdb.conf:ro
    command: -config /etc/influxdb/influxdb.conf
    restart: always
  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    network_mode: host
    volumes:
      - ./grafana:/var/lib/grafana
      - ./grafana/grafana.ini:/etc/grafana/grafana.ini
    command: --config /etc/grafana/grafana.ini
    depends_on:
      - influxdb
    restart: always
  speedtest:
    image: atribe/speedtest-for-influxdb-and-grafana:latest
    container_name: speedtest
    network_mode: host
    volumes:
      - ./speedtest/config.ini:/src/config.ini
    depends_on:
      - influxdb
    restart: always

A few notes about this file:

  • It is my own preference to use network_mode: host rather than specifying ports in a domestic setup.
  • My host volume folders are relative to where my docker-compose.yml file is located.
  • Included is a 3rd container (Speedtest) which can obviously be left out if so desired. I use it instead of the Speedtest component within Home Assistant to avoid flooding my Raspberry Pi 3’s ethernet connection every time the test would run.
7 Likes

Also depending upon the version of Grafana, the default user name and password could be admin and admin.

1 Like

Thanks for sharing the docker-compose.yaml. I like using the compose file, as it all comes together nicely the way I want and its all in one place.

2 Likes

You’re welcome, and same here. Plus it’s an easy way for me to keep track of how I put together my containers. The alternative would be to paste the commands I used in a notepad or something, which seems cumbersome to me.

1 Like

I do this but it says invalid username or password … ?

EDIT: correct is admin:admin

2 Likes

how can we do this in HASSIO?

What is not working? downloader? If that does not work with Hassio, you can always use the generic camera method described in the notes.

No image is downloaded, will check note number 5

I succesfully managed to get to this point. But the graphs is static (always the same), how to do that it is updated …?

Do I need to change something in the link?

camera:
  - platform: generic
    name: Grafana Power
    still_image_url: 'http://192.168.1.12:3000/render/dashboard-solo/db/power?orgId=1&from=1520495590598&to=1520535919212&panelId=1&width=1000&height=500&tz=UTC%2B01%3A00'
    username: !secret grafana_user
    password: !secret grafana_pass

The graphs refresh automatically (like any other camera). Let it run for a while and you will see the difference.

Mine did not change for 24 hours, maybe I have to take out the FROM TO portion of the url?

Oh yes…for sure. Look at my URL.

Why do you prefer camera over panel iframes? Just curious.

Cameras can be used in existing tabs.

Camera does not update…

Did you add a command line sensor to update the image?

Nope, do you mind sharing how to

Look at #12 from original post. I think that you have to have that in your confg for the graph to update the image periodically…?