Formatting a number with 2 decimal base 0.05

Hello,

I try to find a solution for my template sensor, I will roud the value_template with base 0.05, example:

  • platform: template # Consumo in CHF Giornaliero
    sensors:
    daily_cost_luce_corridoio_1p:
    friendly_name: Costo Giornaliero
    unit_of_measurement: ‘CHF’
    icon_template: mdi:cash-multiple
    value_template: “{{ (states(‘sensor.daily_energy_luce_corridoio_1p’) |round(2) }}”

if the sensor is the “states('sensor.daily_energy_luce_corridoio_1p” 12.3346 the resul is 12.33 but i will 12.35.

more example:
sensor value: desired value template:
12.2726 => 12.30
12.3746 => 12.35
12.3946 => 12.40

Any idea where I’m going wrong?

I think understand your question. I think you want to round up and not use standard rounding.
To make this easier for others to understand: use “</>” button to format your code.
Change the ‘will’ to ‘want’ if that is what you mean, it took me awhile to interpret what I think you meant.

Convoluted, there might be a better way:

{{ (states(‘sensor.daily_energy_luce_corridoio_1p’)|float * 100 / 5) |round * 5 / 100}}

Almost. The second and third values are converted the way the OP wants but the first value, 12.2726, is converted to 12.25 (as opposed to 12.30).

This uses a consistent ‘ceiling’ approach but produces very different results from the desired ones.

{{ 12.2726 | round(2, 'ceil') }}
{{ 12.3746 | round(2, 'ceil') }}
{{ 12.3946 | round(2, 'ceil') }}

The challenge here is that the desired rounding is to two decimal places but sometimes floor and sometimes ceiling and only in increments of 0.5.

Sorry, a better explanation below.
I need a formula to round 0.05 cents, below is a screen shot via developer tools:

On the documentation is also this:

  • Filter round(x) will convert the input to a number and round it to x decimals. Round has four modes and the default mode (with no mode specified) will round-to-even.
    • round(x, "floor") will always round down to x decimals
    • round(x, "ceil") will always round up to x decimals
    • round(1, "half") will always round to the nearest .5 value. x should be 1 for this mode

Unfortunatly is not apply with the decimal.

2 Likes

I would argue that the proper rounding of 12.2726 at 0.05 precision is indeed 12.25 (it’s < 12.2750) :wink:

12.2726 => 12.30
12.3746 => 12.35

are basically incompatible as far as rounding goes, imho

I agree but, for whatever reason, the first post requested 12.30. :man_shrugging: However, now I see the requirement has changed to 12.25.

sorry, it’s my mistake

Paste this into the Template Editor and confirm it reports the values you want:

{{ '%.2f' | format((12.2726 / 0.05) | round() * 0.05) }}
{{ '%.2f' | format((12.3746 / 0.05) | round() * 0.05) }}
{{ '%.2f' | format((12.3946 / 0.05) | round() * 0.05) }}

If it does then you can use this in your Template Sensor:

  value_template: "{{ '%.2f' | format((states('sensor.daily_energy_luce_corridoio_1p')|float / 0.05) | round() * 0.05) }}"

FWIW, it’s effectively koying’s suggested formula but also employs the format statement to ensure the result always has two decimal places (like 12.40 instead of 12.4).

many thanks @123 ,@koying for your help,

I have problem with the “format” statement, following information:

Template Editor and lovelace correct with round 1.25:


image

Template Editor Showing correct value but in lovelace 1.3 instead 1.30:


image

sensor template configuration:

  - platform: template 
    sensors:
      template_daily_cost_power_meter:
        unit_of_measurement: 'CHF'
        icon_template: mdi:cash-multiple
        value_template: "{{ '%.2f' | format((states('sensor.daily_energy_power_meter')|float / 0.05) | round() * 0.05) }}"

Any idea?

1 Like

It’s not currently possible to format numbers in lovelace.
Please upvote

3 Likes

Sorry I’m not a programmer… but I didn’t understand why on template editor is correct with result 1.30 and the same “value_template” applied to the sensor template show me 1.3.

Template Editor:

States:

I think this may be caused by Home Assistant’s ‘native typing’ feature. In brief, it examined the value 1.30 and determined it looks like a floating point number so it stored it as 1.3 because that’s the equivalent of 1.30 or even 1.300, etc.

Unfortunately, in this case, the conversion ruins our effort to present the value in the desired format. The conversion to 1.3 wouldn’t be a problem if there was a way for a Lovelace card, such as the Entities Card, to present the value as 1.30 but, as explained by koying, this is not currently possible.

There’s one workaround that you might consider to be acceptable: use a Markdown Card because it supports the use of a template and displays the template’s result verbatim.

Here’s an example where the sensor’s value is 22 but the Markdown Card allows me to display it as 22.00.

In your case, you can use your Template Sensor:

Daily Cost Power Meter: {{ '%.2f' | format(states('sensor.template_daily_cost_power_power_meter')| float) }} CHF

Or you can eliminate the Template Sensor and perform the entire calculation within the Markdown Card.

Daily Cost Power Meter: {{ '%.2f' | format((states('sensor.daily_energy_luce_corridoio_1p')|float / 0.05) | round() * 0.05) }} CHF

HI @123 thanks a lot for the explanation and thanks for the help.
I hope in the future will be created “device_class” with currency option, just to show 2 decimal in home hassistant.

2 Likes

You’re welcome!

If you found the workaround useful (Markdown Card), please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a link below your first post that leads to the solution post. All of this helps users find answers to similar questions.