Load average convert to precise percentage

Hi,

I need help converting load average statistics (which are available as attributes on a sensor) to percentages.

those 3 load averages are:

  • 1 minute load
  • 5 minute load
  • 15 minute load
    image

which return different CPU values ​​every second.

a 1 load means a load of 25 percent of the system.
a 2 load 50%, 3 load 75% and 4 load 100%.

What I want to achieve is that it gives the precise percentage and not just 25, 50, 75 or 100%.
so when the load number is 0,84 it must give me a percentage of 14% for instance.

is this possible?

Yes, with a template sensor. Guessing your sensor and attribute names:

template:
  - sensor:
      - name: "Load 1 min percentage"
        unit_of_measurement: "%"
        state: "{{ state_attr('sensor.cpu_load','load1min') * 25 }}"

      - name: "Load 5 min percentage"
        unit_of_measurement: "%"
        state: "{{ state_attr('sensor.cpu_load','load5min') * 25 }}"

      - name: "Load 15 min percentage"
        unit_of_measurement: "%"
        state: "{{ state_attr('sensor.cpu_load','load15min') * 25 }}"

You can also set this up via the UI: just put the {{ ... }} bit in the template field.

Notes:

  • Attributes can be numbers, whereas states are always strings. If you were using a state value in a template like this, you’d need to convert it with |float.
  • I’ve not included any rounding in this. You can do that in the front end; or you could do it in the template using |int for integers or |round(2) for two decimal places. So {{ (...)|int }} with the brackets so you’re not just doing it to the 25.
1 Like