Issues with rounding to one decimal place in template sensor

Greetings All,

I have a template sensors setup and I want it to round the value to a single decimal place. It doesn’t seem to do this each time. Can someone tell me what I am going wrong?

value_template: '{{(states.sensor.smartups_load.state) | float / 100  * 1500 | round(1)}}'

You’re only rounding the 1500

Try this

value_template: '{{((states.sensor.smartups_load.state) | float / 100  * 1500) | round(1)}}'
2 Likes

Thanks for the reply.

I tried what you suggested but it doesn’t seem to work. The sensor currently has a value of: 253.49999999999997

That’s epsilon error, that will happen. The rounding on the other hand is working.

If you wan’t to remove epsilon error, you’ll have to convert it to a string.

value_template: >
  {% set value = ((states.sensor.smartups_load.state) | float / 100  * 1500) | round(1) %}
  {{ '{:.1f}'.format(value) }}
3 Likes

Also, to prevent errors on startup when the sensor isn’t available, you could use this:

"{{ ((states('sensor.smartups_load')) | float / 100 * 1500) | round(1) }}"

Jacobs 4 life

1 Like

Jakobs are powerful, but I’m all about that Tediore chucking lol. And the guns with legs in BL3 is some of the funniest shit I’ve seen in a while

1 Like

Thanks everyone. This is working great. Now that I think of it. Would it be possible to eliminate the decimal place altogether? Just have a whole number?

replace float with int and remove | round(1)

Edit: forgot that just truncates the decimal, effectively rounding the number down regardless of its value. Simply changing round(1) to round() would be a better choice.

a, but op wanted a single decimal…

so why not use it this way:

{{states('sensor.smartups_load')|float|round(1)}}

I use that in all my energy sensors (rounding to 2 but that shouldn’t make a difference?), and never have seen the epsilon error, (which I am studying now…)

1 Like

@Mariusthvdb Look above my last comment, OP just asked if it would be possible to display it as a whole number.

sorry, missed that… :blush:
using an |int solves it all then :wink:

still, wonder if my suggestion works for @Shadex12’s original question.

Thanks folks. I am very close. Right now I have this:

value_template: "{{ ((states('sensor.smartups_load')) | int / 100 * 1500) }}"

This is giving me the old epsilon error again. May decimals. I’d like to not display any decimals at all if possible. I just want a value thats says 250 for example. Not 250.0

Thanks again.

you should note the differences when rounding:

24

using |int might have unexpected results, as you can see :wink:

1 Like

Ah good point.

I keep forgetting about the template tool. Thanks for the reminder!

Here’s what I have and it is now working as expected:

value_template: '{{ (states.sensor.smartups_load.state | int / 100 * 1500)|round()}}'
1 Like

round() won’t do anything since you specified int before it. But it also won’t hurt anything to have it.

That’s interesting. I couldn’t seem to get rid of the decimal without it.

Epsilon error is in all languages. In all honesty, it should be applied on the state level by the core. And us as users should only have to specify a number of sig figs at the sensor level, with a system wide level to fall back on.

2 Likes

That would be a nice one for Hacktober: system wide decimal setting!

+1 to that

You should issue a feature request :wink: