Rounding to specific values

I’ve played around in the template editor and can’t seem to get near what I need.

Basically I’ve got power values coming into HA for example 236.56 and I want to round them down in various ways.

For example I might want to round to every 10 so -

236.56 = 230
529 = 520

But also not jumps of 10, maybe 30 -

236.56 = 210
529 = 510

Out of interest it’s to play with the base load of grid tie inverters to balance the output to net zero as close as I can.

Any pointers would be greatfully received!

Phil

Jinja has the round filter to round values in templates. This is mostly used to determine the number of decimals. if you want to round to 10, 100, … I used to devide the number by 10, round, and multiply by 10 again. But it seems round also accepts a negative number of decimals to support this. Experiment with this in developer tools if you like. Change the number in the round filter to your liking. You could even put a calculation in there to change rounding dynamically.

{{ (pi*1000) | round(-2) }}

For jumps of 30, devide by 30, round, and multiply by 30.

By the way: the examples you gave aren’t rounding, because rounding takes the nearest value, so that can also round up. Removing the decimals (always down) is called trucating. You could do that by subtracting half of what you are rounding to, just before rounding. But intgeger division (//) also truncates. This does truncation to a factor of 30:

{{ 389.765 // 30 * 30 }}
1 Like

Ah I didn’t think of negative values in round(), thanks!

I edited above because you wanted truncating though, so I edited the reponse to include that too.