Skip to content

How to add MariaDB database size sensor to Home Assistant

Introduction

How to add a sensor to Home Assistent to monitor the size of the MariaDB database with a scan interval of one hour.

Requirements

Make sure you have:

  • MariaDB running.
  • Home Assistant configured. The secrets.yaml file must be created with the db_url setting for use with MariaDB.

How To

  1. Add sensors.yaml to configuration.yaml

    Go to the config folder of Home Assistant and add the following to configuration.yaml:

    homeassistant/config/configuration.yaml
    # To edit use your text editor application, for example Nano
    sensor: !include sensors.yaml
  2. Add the sensor to sensors.yaml

    Add the following to sensors.yaml:

    homeassistant/config/sensors.yaml
    # To edit use your text editor application, for example Nano
    - platform: sql
    db_url: !secret db_url
    scan_interval: 3600
    queries:
    - name: MariaDB size
    query: 'SELECT table_schema AS "database", ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) "value" FROM information_schema.tables WHERE table_schema="homeassistant" GROUP BY table_schema;'
    column: "value"
    unit_of_measurement: MB
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Optional Replace <3600> with the scan interval in seconds. In this example the scan interval is one hour
    • Required Replace homeassistant with the name of your database if needed
  3. Check the results

    Now go to the Home Assistant UI and check your configuration via Configuration > Settings > Server Controls > Check Configuration (the button). When the configuration is valid restart the server and then you can use the sensor.

    The sensor is also added to the Overview dashboard if it is still maintained by Home Assistant (which is default).

Favorites

Comments

    No comments found for this note.

    Join the discussion for this note on Github. Comments appear on this page instantly.

    Copyright 2021- Fiction Becomes Fact