Display 2 decimal places in overview UI for a sensor

I have a template to convert radon value from Bq/m³ to pCi/L. No matter what I try, the UI overview always shows the value as 0.0 when it should be 0.27. The calculation to convert is the Bq/m³ value divided by 37. The value of Bq/m³ is 10.0. The following is what I have for my template (after testing it):

template:
  - sensor:
      - name: "Basement Radon US"
        unique_id: Basement_Radon_US
        state_class: measurement
        unit_of_measurement: "pCi/L"
        state: "{{ (states('sensor.airthings_radon') | float / 37) | round(2) }}"

What am I doing wrong? The template tester shows correct value.

Home Assistant Core 2022.5.5
Home Assistant Supervisor 2022.05.3
Home Assistant OS 8.1
Kernel version 5.15.32-v7l
Agent version 1.2.1
Frontend version: 20220504.1 - latest

I’m thinking you might need to add

step: 0.01

to the config, as mentioned here in the docs.

I received the following error: Invalid config for [template]: [step] is an invalid option for [template]. Check: template->sensor->0->step. (See /config/configuration.yaml, line 23).

Yeah, I misread the docs. Your exact config entry works for me though, with the entity changed to one that I have, so I’m not sure what’s happening.

That should work. Did you reload templates? Home assistant only truncates trailing zeros in the UI. If the template tester is showing the correct value, then the issue lies somewhere in your configuration or if you didn’t restart. Your configuration looks fine, so did you restart/reload templates?

Yes, I reload after updating it every time.

Are you sure you are looking at the right entity and not some derelict old attempt? the unique id in the template above is not correct as far as I can tell, captital letters in it, missing domain.

Yes. I tried hardcoding it to “10.25” and that displays correctly. What’s wrong with this?

template:
  - sensor:
      - name: "Basement Radon US"
        unique_id: Basement_Radon_US
        state_class: measurement
        unit_of_measurement: "pCi/L"
        state: "{{ (states('sensor.airthings_radon') | float / 37) | round(2) | string }}"

Maybe the final filter converting it to string? That is not what is expected for measurements, is it? But I guess you tried that seeing your example before.

Is it also displaying 0 in the states tab of the developer view? In case it is an UI thing.

check your log for errors. The only thing that stands out is that float does not have a default and will cause issues when sensor.airthings_radon is unknown at startup.

Yep, that’s the issue.
Template warning: ‘float’ got invalid input ‘unknown’ when rendering template ‘{{ (states(‘sensor.airthings_radon’) | float / 37) | round(2) }}’ but no default was specified. Currently ‘float’ will return ‘0’, however this template will fail to render in Home Assistant core 2022.6

But is the problem gone when you specify a default? Because I fixed a few of those quite recently but I’ve never seen these issues pop up.

I’m working on it now. Just switched over to HA, from misterhouse. A bit of a learning curve from PERL :slight_smile:

So it’s a timing thing that I’m not sure how to handle. The value comes from the airthings wave integration. This code sets the value to 0.00.

template:
  - sensor:
      - name: "Basement Radon US"
        unique_id: Basement_Radon_US
        state_class: measurement
        unit_of_measurement: "pCi/L"
        state: >-
          {% if is_state('sensor.airthings_radon', 'unknown') %}
            {{ (states('sensor.airthings_radon') | float / 37) | round(2) }}
          {% else %}
            0.00
          {% endif %}

If I were you I’d use float(0) to get rid if the error. That will give the float filter the default it is about to be required and it will produce the 0 result. But the else should in my opinion be “unknown” for the result to be the way it should be. 0 is not the accurate representation.

But I guess it still is not working? And please do not tell me radon levels have dropped below 3 in the mean time :crazy_face:

Just add availability to it. It wont calculate when it’s not available. The source sensor has to be a number for this template to calculate.

template:
  - sensor:
      - name: "Basement Radon US"
        unique_id: Basement_Radon_US
        state_class: measurement
        unit_of_measurement: "pCi/L"
        state: "{{ (states('sensor.airthings_radon') | float / 37) | round(2) }}"
        availability: "{{ states('sensor.airthings_radon') | is_number }}"

That works, but the value is now unavailable. Will it eventually get set? I’m still trying to figure out the order of things. BTW, thanks for the help!

Went back and read the documentation on the integration and it polls every 5 minutes. Shouldn’t it persist the value from the last poll through a restart?

No, HA does not persist data on restarts

What’s weird though is that there are 2 other values it has as well (Humidity and Temperature) and those are set.