Round() incorrect

I want to convert the brightness 0-255 value to percentage but something weird is going on with the round function.

{% set brightness = (255 | int / 2.55 | round(0)) %}
{{ brightness }}

Result: 85
Cause everyone knows 255 / 2.55 = 85 :confused:

{% set brightness = (255 | int / 2.55 | round(1)) %}
{{ brightness }}

Result: 102

What is going on, how is something so basic not working like one would expect?

In jinja the pipes are applied first.

Your first one is rounding 2.55 to zero decimal points, which is 3

255 / 3 = 85

Your second one is rounding 2.55 to 1 decimal point, which is 2.5

255 / 2.5 = 102

The calculation is correct, your syntax is wrong if that is not the calculations you want to do.

1 Like

Iā€™m an idiot :dizzy_face:

{% set brightness = (255 | int / 2.55) | round(1) %}
{{ brightness }}

Somehow I can break my head over something and never find a solution. But the moment I post something I find the error.

1 Like