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)

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

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))}}