InfluxDB2.0: database size sensor

To anyone who stumbles into this post to find a solution for tracking InfluxD|B storage use (perhaps someone can point this/me to the InfluxDB integration page as this thread REALLY should either be linked or feel free to lift my version of the code here to save someone else a day of testing and struggles)…

The basis for this was this was Aeiou’s previous post. Format for executing a command_line (bash) command has changed and likewise there are now much bette/easier ways to setup/maintain the value templet/sensor so without further ado…

Requirements:

  1. Access to host’s NATIVE file system to run ‘du’ command (and of course add said command to system’s cron for update(s)
  2. Access to files HA’s config directory (though in theory you can store this file ANYWHERE HA can read. ‘/config’ is the default location/home directory where command line jobs.

First add simple cron job on the host running your HA instance. In my case I use HA core running in Docker but regardless the only thing needed is access to the ACTUAL host’s file system and location where your HA config directory lies. I chose to update the file every 10 min. Remember to restart & reload the cron task as needed for your environment.

*/10 * * * * /usr/bin/du -sb  "<<path_to_persistent_Influx_storage>>/Influx/engine/data/<<Your_actual database_Directory>>/" | cut -f -1 > "<<path_to_persistent_HA_data>>/HomeAssistant/config/influxdb2size"

Next you need to add the sensor. I choose to do this old skool by simply cat’ing the file. You MAY be able to also do this by using the file read primitive in HA but since there are many other possible applications of bash-based sensors I went this route.

As mentioned the format for command_line has changed (no longer being platform based). I also added a unique_ID so you can set area and see it by default in Lovelace. I sent both Device_Class AND unit_of_measure to you can change the units and precision as you wish all from the UI without having to deal with the complications of customizing the value_templet. I did leave the orignal rounding in, though it’s no longer needed.

command_line:
  - sensor:                 #-----InfluxDB size --- See cronjob for du command (writes file size in bytes every 10 min)
      name: HA InfluxDB Size
      command: "cat /config/influxdb2size"       # read local data from du cronjob
      value_template: "{{ (value.split('\t')[0]|int)|round(1) }}"   
      unit_of_measurement: 'B'
      device_class: data_size
      icon: mdi:database
      scan_interval: 300                                       # refresh sensor every 5 min from file                     
      unique_id: <xxxxxx>                                      # https://www.uuidgenerator.net

Then restart. And find your sensor and config in UI as desired.
SIMPLE!

As I said hopefully this helps someone… Now to clean up may MariaDB sensor…