Rounding percentages

Hi,

I am trying to figure out 2 things:

No decimal places, e.g., 99%, 100%.
Round to the nearest whole number, e.g., if 99.2% the value would be 99% whereas if the value is 99.5%, the result would be 100%.

"{{ (states('sensor.b3_solar_production') | float / states('sensor.b3_house_consumption') | float * 100 | round(2))  }}"

Any ideas?

Thanks All :slight_smile:

round(0) ?

I tried this, however it seems to produce decimal places whenever the value goes above 100%, which can happen for a few seconds as the CTs catch up.

Have you tried β€œround(0)”?

Also, the way you are doing it now is only rounding the 100.

Move your last ) to right behind the 100 and it will round everything.

1 Like

This worked, many thanks

1 Like

Just a question. How would I prevent this value ever going above 0 or 100?

As the inverter changes etc, the value currently goes above 100 and less than 0, before stabilising.

How can this be achieved?

I need to add min and max values essentially.

Thanks.

try this:

{% set value =  (states('sensor.b3_solar_production') | float / states('sensor.b3_house_consumption') | float * 100) | round(2)  %}
{% set limits = [0, value, 100] %}
{% set constrained_value =  (limits | sort)[1] %}
{{ constrained_value }}

you could probably combine the whole template into one line but I spelled it out so you could see what it’s doing.

Worked a treat. Many thanks.

I can see how this works now. Appreciate your help.

1 Like