Using the official docs, I can use the following to get DB Size in MB
SELECT table_schema "database", Round(Sum(data_length + index_length) / POWER(1024,2), 1) "value" FROM information_schema.tables WHERE table_schema="homeassistant" GROUP BY table_schema;
so I wanted to use the following to get in bytes:
SELECT table_schema AS "Database", SUM(data_length + index_length) AS "db_size" FROM information_schema.TABLES WHERE table_schema = 'homeassistant' GROUP BY table_schema;
and then use the following value_template to convert it to B/MB/GB/etc
{% set bytes = 1024 %} {# You can adjust this based on the unit you want to convert from #}
{% set value_in_bytes = value | float %}
{% set kilobytes = value_in_bytes / bytes %}
{% set megabytes = kilobytes / bytes %}
{% set gigabytes = megabytes / bytes %}
{% set terabytes = gigabytes / bytes %}
{% set petabytes = terabytes / bytes %}
{% if petabytes >= 1 %}
{{ '%.2f' | format(petabytes) }} TB
{% elif terabytes >= 1 %}
{{ '%.2f' | format(terabytes) }} GB
{% elif gigabytes >= 1 %}
{{ '%.2f' | format(gigabytes) }} GB
{% elif megabytes >= 1 %}
{{ '%.2f' | format(megabytes) }} MB
{% else %}
{{ '%.2f' | format(kilobytes) }} KB
{% endif %}
but it always says “unavailable” does the SQL sensor not take advanced templating like this, or am i just wrong about it?
2 posts - 2 participants