Template sensor to state the last zero

I have created a template sensor to state an average of 2 sensors.

  - platform: template
    sensors:
      outside_illuminance:
        unit_of_measurement: lux'
        device_class: voltage
        value_template: "{{ ((states('sensor.front_illuminance')|float + states('sensor.rear_illuminance')|float) /2)|round(2) }}"

The above works; however, if the total is 0.305, it will omit the last zero and only displays

0.3

How can I have it to states the last zero?

0.30
value_template: "{{ '$%.2f'|format( ( states('sensor.front_illuminance')|float + states('sensor.rear_illuminance')|float ) / 2 ) }}"
1 Like

Thank you for the reply. When I input this in the developer tool

{{ '$%.2f'|format( ( states('sensor.front_illuminance')|float + states('sensor.rear_illuminance')|float ) / 2 ) }}

the result comes back as

$0.31

When I remove the $, the result is what I want.

0.31

Ok, to remove the $?

Yeah sure. I copied and pasted it from one of my templates. Forgot about the $.

What does the $ do? Without it, it does not display the zero

0.2

but with the $ in place it is displaying like this

$0.20

It treats the value as currency.

So you want trailing zeros but not currency. Try formatting like this:

{{ '0:.2f'|format(...etc

So you want trailing zeros but not currency. Try formatting like this:

Not exactly. I want it to show the zero if for example the state is 0.205. I want it to show like the below.

0.20

My original code above omits the zero and display like this, which is not what I want.

0.2

It is giving me an error with this now.

{{ '0:.2f'|format( ( states('sensor.front_illuminance')|float + states('sensor.rear_illuminance')|float ) / 2 ) }}

error:

TypeError: not all arguments converted during string formatting

Additional note:
Just to clarified what HA is outputting in developer tools.

code without the $ is omitting the zero. (Not what I want)

code with the $. This is what I want but without the $.

Wrap the formatting instructions in parentheses:

{{ '{0:.2f}'|format( ( states('sensor.front_illuminance')|float + states('sensor.rear_illuminance')|float ) / 2 ) }}

EDIT

See correction below.

Hi. Thank you for your reply. I have tried adding the below as suggested, but I still get the same error.

{{ '{0:.2f}'|format( ( states('sensor.front_illuminance')|float + states('sensor.rear_illuminance')|float ) / 2 ) }}

error:

TypeError: not all arguments converted during string formatting

Replace the first | with a period.

That suppressed the error but it still not showing the zero.

Didn’t you mark this topic as solved? :slight_smile:

It looks like Home Assistant’s native typing feature infers that the string value 0.20 appears to be a number so it converts it to a number (and to express 0.20 as a number simply requires 0.2 without the trailing zero).

Here’s an example to show that the formatting does in fact work when processed by python. Notice how the number is padded with zeros to make three decimal places.

Screenshot from 2021-08-16 17-42-33

The same technique doesn’t work in Home Assistant because the result is converted from a string to a number (by the native typing feature).

The reason why it worked with the leading $ symbol is because $0.20 is interpreted to be a string and there’s no attempt to convert it to a number. To prove the point, the following template prepends a ~ character (anything other than a space) and that makes the result clearly a string that cannot be interpreted as a number (notice how it indicates the result is a string, not a number like in the previous example).

1 Like

Yea, I jumped the gun thinking it was ok and not really checked physically in developer tool.

In this case, I don’t see a way to work around native typing (other than disabling it … which will eliminate its usefulness everywhere else). I doubt you want to add a useless character (like ~) to the value to force it to be understood as a string.

The sensor’s value will have to remain the way native typing generates it. Just speculating but maybe there’s a custom card that allows formatting values so you can present it in the Lovelace UI in the way you want.