Template Sensor state round to 2 decimal places

I am trying to round the result of a template sensor to 2 decimal places would like some help of how to best place the rounding syntax.

This is what I have so far

sensor:  
  - platform: alpha_vantage
    api_key: !secret alphavantage_key
    foreign_exchange:
      - from: AUD
        to: USD
        name: AUD_USD
      - from: USD
        to: GBP
        name: USD_GBP
      - from: BTC
        to: AUD
        name: Bitcoin
        
## AUS to GBP Conversion - Did this as for some reason alpha vantage doesn't provide AUD to GBP Rates propably due to being USD based site
  - platform: template
    sensors:
      aud_to_gbp:
        unit_of_measurement: 'GBP'
        value_template: "{{ states('sensor.aud_usd')|float * states('sensor.usd_gbp')|float }}"
        friendly_name: 'AUD to GBP'

can I just add something like '%+.2f'|format to the front of the value_template?

1 Like

You’re looking for round().

http://jinja.pocoo.org/docs/2.10/templates/

Sebastian

5 Likes

So like this?

{{ states('sensor.aud_usd')|float * states('sensor.usd_gbp')|float|round(2) }}

2 Likes

Little offtopic, you can try the Developer tools Template page to verify the output :slight_smile:

I also just created a template value and also used round(2) a t the end to give it 2 decimals, but if the value_template is alright like that i’m not sure, try with the Developer Tools and then the Template page

2 Likes

Thanks for the tip I never use the template editor it doesn’t round with my template unfortunately I just tried it there

Just build the expression step by step to see where it fails.
{{ 12.3456 | round(2) }} for me returns 12.35 as expected.

Sebastian

2 Likes

The expression doesn’t fail, it just doesn’t round(2), I have successfully used this function in other templates this is the only one where I am multiplying the result of 2 separate sensors states and need to round the entire. Maybe I need to encompass in brackets

yep that was it thanks for the sounding board

"{{ (states('sensor.aud_to_usd')|float * states('sensor.usd_to_gbp')|float)|round(2) }}"

18 Likes

I realise this is an old post, however would be nice to get a solution.

The problem I have, is if the value is, for example: ÂŁ21.10, with the round(2), this shows as ÂŁ21.1. Is there anyway of forcing the 2 decimal places?

Thanks
Tony

1 Like

You can know adjust the display precision on the entity page (assuming the template sensor has an unique_id)

1 Like

Hi, thanks. Unfortunately the template doesn’t give the entity a unique id. I think it would need to be part of the template code.

{{((float(states(‘sensor.one’)) * 4090 / 100) / float(states('sensor.two)) * -1 ) | round(2) }}

Note the extra brackets, you need to enclose the calculation and round the answer !

Courty

2 Likes

i found this which helps change the display precision Display two decimal places in the dashboard - #3 by Troon
so instead of (or as well as) using round you could wrap your template in
{{ '{:.2f}'.format( -CALCULATION-) }}

in my usage example
€{{ '{:.2f}'.format((states('sensor.apartment_current_hca') |float * states('input_number.heat_cost') | float) |round(2))}}

Courty40 gave the correct answer. To explain it a bit further:

If you did {{ 10 / 3 |round(2) }} people expect 0.33333 that is rounded to 0.33.
This is not how it works;
first 3 would get rounded to 2 decimals, next 10 is divided by 3.00. This results in 3.3333…

This is logical, since when we do {{ "10" |int / "3" |int |round(2) }}, we would also expect the function to make an int of “10” and an int of “3” before doing the division.

Any function with the piping character always executes on the variable just before the function.

How do you realize the wanted outcome?
you do
{{ ( 10 / 3 ) |round(2) }}
The calculation is placed between ( ). This forces it to be calculated and become one variable that is passed to the function behind it.

So in steps this results in

{{ 10 / 3 | round(2) }} =
{{ 10 / 3.00 }} =
{{ 3.333333333 }}

and

{{ (10 / 3) | round(2) }} =
{{ 3.3333333 | round(2) }} =
{{ 3.33 | round(2) }}

I hope this helps people with a better general understanding of how this all fits together.