Calculations with sensor values

I’m not sure how to phrase this properly, so I’ll just jump right in.

I’ve set up Home Assistant to receive SNMP data from Windows File Server in the form of sensors. The idea behind this would be to monitor free disk space. The SNMP data is in the form of OIDs and I can get total disk space and used disk space.

I would like to combine these two figures to get a percentage value and ideally have it represented visually with a green-orange-red icon, depending on how much disk space is available.

Basically, I want to take two values from sensors and do a calculation with them.

Any help would be much appreciated

1 Like

Is the plan to report the result in a third sensor?

If so, then what you want is a Template Sensor. The calculation is performed in the Template Sensor’s value_template. Here’s a simple example:

sensor:
  - platform: template
    sensors:
      my_sensor:
        value_template: "{{ states('sensor.something') | float + states('sensor.whatever') | float }}"

sensor.my_sensor now reports the sum of the other two sensors.

8 Likes

Hi, that’s exactly what I want to do, thanks a bunch.

I can now get it to do simple things, but unfortunately ‘free disk space’ isn’t one of the measured metrics for SNMP, or I’m just not seeing it, so I have to do a long calculation.

Basically {[(Total - Used) / Total] x 100}

This is the line with my custom sensor, would you mind having a look at how I need to change it to get it working:

value_template: "{{{states(‘sensor.hrstoragetotal_disk_c’) | float - states(‘sensor.hrstorageused_disk_c’) | float} / states(‘sensor.hrstoragetotal_disk_c’) | float x 100}}

I do this exact thing

OK you are trying to do a maths calculation so you need to use brackets for starters :wink:

so my snmp sensors present like so

so try like this for percentage used

{{ (100 - ((states.sensor.hrstorageused_disk_c.state|int) / (states.sensor.hrstoragetotal_disk_c.state|int) *100))|round(1) }}

or for percentage free

{{ ((states.sensor.hrstorageused_disk_c.state|int) / (states.sensor.hrstoragetotal_disk_c.state|int) *100)|round(1) }}

You can use variables within the template to help make the calculation more legible.

  value_template: >-
    {% set t = states('sensor.hrstoragetotal_disk_c') | float %}
    {% set u = states('sensor.hrstorageused_disk_c') | float %}
    {{ ((t - u) / t) x 100 }}
5 Likes

Thanks for all the help so far, I’ve got it set up almost exactly how I want it, except for one minor irritating point.

Is there a way to round to one or two decimal points? Right now I have about 11 digits after the decimal, which works, but doesn’t look very nice.

The function I shared has a round at the end of it can you share what you used and I can help you add it.

Thanks, I actually used it (assuming) but it doesn’t seem to work.

  my_sensor_ad_c_free_percent:
    value_template: "{{ states('sensor.my_sensor_ad_c_free') | int / states('sensor.hrstoragetotal_disk_c') | int * 100 |round(1) }}"
1 Like

you haven’t used the brackets so it is ignoring that function

{{ ((states.sensor.hrstorageused_disk_c.state|int) / (states.sensor.hrstoragetotal_disk_c.state|int) *100)|round(1) }}

You’re the best, thank you so much; now I can set it up exactly as I want it.

1 Like

Now you just need to make it look pretty

image

1 Like

Damn, that looks nice, I’m almost embarrassed to show mine so far.

I’m still digging around for the OIDs to show processor load and free memory, the values at the moment are for something else as placeholders. The fan speed and temperature is a nice touch.

1 Like

if you know your total cpu and memory we can clean them up to percentages also if you like?

Just to clarify, it’s not really ignoring the function. It’s an “order of operations” thing.

since you didn’t use the parentheses around the entire thing it assumed that you just wanted to round the last term in the equation - which in this case is 100. since 100 is already rounded to a full decimal place then nothing is different.

the “round” is specific to the last grouped terms just before the command. just like the “int” is specific to the last grouped term - states(‘sensor.hrstoragetotal_disk_c’)

2 Likes

Thanks, but I managed it by myself.

Might I ask how you got those full-circle indicators for your storage?

1 Like

I got the circle sensor card here

Example of how I use it here

1 Like

I put the lines in the sensor directory?

Dear @thekevinkalis,
I am trying to do the same as you did, SNMP sensors to monitor a WIN machine, in my case the NUC on which my Hass runs in a VM under VirtualBox.
I have tried to use SNMPWalk, but I can’t figure out how to retrieve the OIDs I need: disk total space, disk free space, memory, CPU, etc.
Could you please share the OIDs you used? Or your sensors config.? I am a bit desperate.
Thanks!

I ended up using OID Browser, I found the interface much more user friendly. With a bit of clicking around, you get a feel for the structure and where the OIDs are. I’m assuming you’ve enabled the SNMP service within Windows and know the community string.

When you have the OIDs, you have to create sensors for each value, ie. “Total Disk Space”, “Used Disk Space” etc. You then have to create a separate sensor to calculate the available disk space.

Here’s an example of one of the sensors:

- platform: snmp
  host: 192.168.2.200
  name: adUsed_Disk_C
  community: public
  baseoid: 1.3.6.1.2.1.25.2.3.1.6.1

Personally, I could never get a consistent reading from the CPUs, so I just dropped them altogether.

If you need any more help along the way, let me know, I struggled for ages to get this working on my own system, so I feel your pain.

Thanks, I eventually found an OID browser showing enough information.
FYI, for the CPU, I use the Windows Device Portal API, which provides the info (curl in a command_line sensor). It is not always matching 100% what I see in the Task Manager running on the machine, but it is a good indication.