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:
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.
- 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
@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
@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.