Display two decimal places in the dashboard

A really noddy question here but I’ve tried a few things and cannot figure it out.

I want to display how much electricity I have exported today in pounds/pence with 2 decimal places, but I cannot figure it out, the entity card typically only displays a single decimal place unless the value is something like 2.35, eg:

image

The above is from a template sensor:

  - name: "Exported Electricity Today"
    unit_of_measurement: GBP/kWh
    state_class: total
    device_class: monetary
    state: "{{ '{:.2f}'.format(
states('sensor.octopus_electricity_export_day_compensation') | float + 
states('sensor.octopus_electricity_export_flux_compensation') | float +
states('sensor.octopus_electricity_export_peak_compensation') | float) }}"

The above template works fine in the Developers Tool template tester but not on the dashboard. I thought defining device_class: monetary would help but it didn’t

Any ideas please?

Not sure it’s possible as the frontend removes trailing zeros, looking at this thread:

You could create a template sensor for display only, forcing the state to a string by adding the units on the end. Bit ugly, but it’s the best you can do:

template:
  - sensor:
      - name: Exported electricity as string
        state: "{{ '{:.2f}'.format(states('sensor.exported_electricity_today')|float(0)) ~ ' GBP/kWh' }}"

or:

        state: "{{ '£' ~ '{:.2f}'.format(states('sensor.exported_electricity_today')|float(0)) ~ ' per kWh' }}"
1 Like

Thanks all, interesting, I will try those options :grinning:

Hmm, can’t get your example to work.

This is fine:

  - name: "Exported Electricity Today"
    unit_of_measurement: GBP/kWh
    state_class: total_increasing
    state: "{{ '{:.2f}'.format(
      states('sensor.octopus_electricity_export_day_compensation') | float + 
      states('sensor.octopus_electricity_export_flux_compensation') | float +
      states('sensor.octopus_electricity_export_peak_compensation') | float) }}"

But when I then try this:

  - name: "Testing String"
    state: "{{ 'Testing ' ~ '{:.2f}'.format(states('sensor.exported_electricity_today')) ~ 'test' }}"

in the logs I get

Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:408
First occurred: 15:40:30 (1 occurrences)
Last logged: 15:40:30

TemplateError('ValueError: Unknown format code 'f' for object of type 'str'') while processing template 'Template<template=({{ 'Testing ' ~ '{:.2f}'.format(states('sensor.exported_electricity_today')) ~ 'test' }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.testing_string'

I think that means it believes my sensor.exported_electricity_today is a string not an integer hence can’t run the format on it?

Correct-ish, should have a |float on the end as per your original. Fixed in my post above.

Brill, thanks for the assistance, much appreciated.

Just found another, maybe better way of doing this.

Define the template sensor with a unique_id, eg:

 - name: "Imported Electricity Today"
    unit_of_measurement: GBP
    state_class: total_increasing
    state: "{{ states('sensor.octopus_electricity_day_cost') | float + 
      states('sensor.octopus_electricity_flux_cost') | float +
      states('sensor.octopus_electricity_peak_cost') | float }}"    
    unique_id: imported_today

Then in the dashboard select the entity, click the cog icon and you can choose the display precision, eg:

image

1 Like

That will not show trailing zeros — it’ll show 4.2 not 4.20.

I stand corrected :crazy_face:

1 Like

image

1 Like

Merci !!!