# How can I calculate these percentages?

First of all, I am in the need to say thank you to everyone for creating this amazing community. Let’s get straight to the point, I am new to home assistant coding and I can’t figure out how to create simple usage percentage. I have a NAS with 3 volumes (V1, V2 and V3), 1 sensor per volume, each sensor outputs a usage percentage. I want to add all three percentages together and calculate a total usage percentage. Each volume has different storage capacities.

Let’s say V1’s (4TB) sensor outputs 17%, V2 (4TB) outputs 0% and V3 (2TB) outputs 0%. The total storage capacity of my NAS is 10 TB. This is where I stopped:

``````- name: "Suma de volúmenes NAS"
unique_id: suma_volumenes_nasrene
state: >-
{{
states('sensor.nasrene_volume_1_volume_used'),
states('sensor.nasrene_volume_2_volume_used'),
states('sensor.nasrene_volume_3_volume_used')
}}
unit_of_measurement: "%"
state_class: measurement
``````

is “sensor.volume_x_volume_used”, etc, in % or actual storage used?

if it’s actual storage then:

``````state: >-
{{
(( states('sensor.nasrene_volume_1_volume_used') | float +
states('sensor.nasrene_volume_2_volume_used') | float +
states('sensor.nasrene_volume_3_volume_used') | float)  / 18 ) * 100
}}
``````

if it’s in percent then you need to convert back to actual used then use the above because I can’t think of a way to get the actual used out of the total by using just the percentages since they are different.

Maybe this:

``````- name: "Suma de volúmenes NAS"
unique_id: suma_volumenes_nasrene
state: >-
{% set tb = 1024 * 1024 * 1024 * 1024 %}
{% set v1_size = 8 * tb %}
{% set v2_size = 8 * tb %}
{% set v3_size = 2 * tb %}
{% set total_size = v1_size + v2_size + v3_size %}
{% set v1_used_perc = states('sensor.nasrene_volume_1_volume_used') %}
{% set v2_used_perc = states('sensor.nasrene_volume_2_volume_used') %}
{% set v3_used_perc = states('sensor.nasrene_volume_3_volume_used') %}
{% set v1_used_bytes = v1_size * v1_used_perc / 100 %}
{% set v2_used_bytes = v3_size * v2_used_perc / 100 %}
{% set v3_used_bytes = v3_size * v3_used_perc / 100 %}
{% set total_used = v1_used_bytes + v2_used_bytes + v3_used_bytes %}
{{ (total_used / total_size * 100) | round(1) }}
unit_of_measurement: "%"
state_class: measurement
``````

I’m assuming that your sensors put out a value between 0 and 100 rather than between 0 and 1. I’m also assuming that 1 TB = 1024 * 1024 * 1024 * 1024 bytes

@templeton_nash
That was an amazing try, but it didn’t work. It outputs “unknown”. Thanks for the help though, this may be really close to solution.

@evoke0
What is the actual output from your sensors?

@finity I guess this would work with actual storage used, but it has to work with percentages. I do have sensors with actual storage but except for the first volume (which is the one with most storage used and the value is 0.637 TB → 637GB) the rest output odd values, which I guess it has to do with low storage used (1.8123245e-05)

@templeton_nash V1 outputs 17.7, V2 and V3 output 0 (less than 1 percent storage used), I updated my post values to real ones, shouldn’t really make a difference as I changed what I had to in order to the other example to work

If I substitute your sensor values into the code and paste this into the Developer Tools > TEMPLATES, I get 7.9

``````{% set tb = 1024 * 1024 * 1024 * 1024 %}
{% set v1_size = 8 * tb %}
{% set v2_size = 8 * tb %}
{% set v3_size = 2 * tb %}
{% set total_size = v1_size + v2_size + v3_size %}
{% set v1_used_perc = 17.7 %}
{% set v2_used_perc = 0 %}
{% set v3_used_perc = 0 %}
{% set v1_used_bytes = v1_size * v1_used_perc / 100 %}
{% set v2_used_bytes = v3_size * v2_used_perc / 100 %}
{% set v3_used_bytes = v3_size * v3_used_perc / 100 %}
{% set total_used = v1_used_bytes + v2_used_bytes + v3_used_bytes %}
{{ (total_used / total_size * 100) | round(1) }}
``````

If you want the total percent, then multiple the 4TB drives with 0.4 and the 2TB drive with 0.2 and add the values up.

If you want the actual space in TB the then multiple the percentage value with the drive size and add them up.

@templeton_nash Alright this is weird, you are right and I get the same result as you do, clearly your code is right. However, when I change the actual values to the sensors my HA crashes and reboots. This happened before when instead of trying it in TEMPLATE, I pasted the code into a secondary info from a template card, thought I made something wrong but I apparently I didn’t.

Yeah, it crashes mine too! I’ve simplified the math now.

Try this in Dev Tools:

``````{% set v1_size = 8 %}
{% set v2_size = 8 %}
{% set v3_size = 2 %}
{% set total_size = v1_size + v2_size + v3_size %}
{% set v1_used_perc = 100 %}
{% set v2_used_perc = 100 %}
{% set v3_used_perc = 100 %}
{% set v1_used_bytes = v1_size * v1_used_perc / 100 %}
{% set v2_used_bytes = v2_size * v2_used_perc / 100 %}
{% set v3_used_bytes = v3_size * v3_used_perc / 100 %}
{% set total_used = v1_used_bytes + v2_used_bytes + v3_used_bytes %}
{{ total_used / total_size * 100 | round(1) }}
``````

Then try this:

``````{% set v1_size = 8 %}
{% set v2_size = 8 %}
{% set v3_size = 2 %}
{% set total_size = v1_size + v2_size + v3_size %}
{% set v1_used_perc = states('sensor.nasrene_volume_1_volume_used') %}
{% set v2_used_perc = states('sensor.nasrene_volume_2_volume_used') %}
{% set v3_used_perc = states('sensor.nasrene_volume_3_volume_used') %}
{% set v1_used_bytes = v1_size * v1_used_perc / 100 %}
{% set v2_used_bytes = v2_size * v2_used_perc / 100 %}
{% set v3_used_bytes = v3_size * v3_used_perc / 100 %}
{% set total_used = v1_used_bytes + v2_used_bytes + v3_used_bytes %}
{{ total_used / total_size * 100 | round(1) }}
``````

Maybe the sensors need to be made into floats:

``````{% set v1_size = 8 %}
{% set v2_size = 8 %}
{% set v3_size = 2 %}
{% set total_size = v1_size + v2_size + v3_size %}
{% set v1_used_perc = states('sensor.nasrene_volume_1_volume_used') | float %}
{% set v2_used_perc = states('sensor.nasrene_volume_2_volume_used') | float %}
{% set v3_used_perc = states('sensor.nasrene_volume_3_volume_used') | float %}
{% set v1_used_bytes = v1_size * v1_used_perc / 100 %}
{% set v2_used_bytes = v2_size * v2_used_perc / 100 %}
{% set v3_used_bytes = v3_size * v3_used_perc / 100 %}
{% set total_used = v1_used_bytes + v2_used_bytes + v3_used_bytes %}
{{ total_used / total_size * 100 | round(1) }}
``````
1 Like