Understanding and Managing Decimal Precision in Input Numbers

Hi everyone, I’m currently working on fine-tuning my smart home setup and came across an interesting issue with input numbers and their decimal precision. I thought it might be helpful to share my findings and ask for any additional insights or tips from the community.

So, to start off, I’ve set up several input numbers in my Home Assistant configuration for controlling temperatures in different rooms. Here’s a snippet of how they’re defined in my configuration.yaml:

yaml
input_number:
max_living_temp:
name: Maximum Living Room Temperature
min: 20.0
max: 26.0
step: 0.1
icon: mdi:thermometer-chevron-up
mode: box
unit_of_measurement: °C
set_living_temp:
name: Desired Living Room Temperature
min: 20.0
max: 26.0
step: 0.1
icon: mdi:thermometer-lines
mode: box
unit_of_measurement: °C
min_living_temp:
name: Minimum Living Room Temperature
min: 11.0
max: 21.0
step: 0.1
icon: mdi:thermometer-chevron-down
mode: box
unit_of_measurement: °C

I’ve noticed that even though I’ve set the step value to 0.1, the values displayed in Lovelace sometimes show multiple decimal places. For example, instead of showing 20.1°C, it might display 20.123456789°C. This isn’t causing any functional issues, but it’s a bit distracting and not as clean as I’d like it to be.

I started by checking how these values are used in my automations. I have a few templates that calculate the difference between the current temperature and the desired temperature to trigger certain actions, like adjusting the heating. Here’s a simplified version of one of those templates:

yaml
condition: template
value_template: >-
{{ (states(‘sensor.ht_living_temp’)|float) - (states(‘input_number.set_living_temp’)|float) >
(((states(‘input_number.max_living_temp’)|float) - (states(‘input_number.set_living_temp’)|float)) / 2|float) }}

At first, I thought the extra decimal places might be a result of these calculations, but upon closer inspection, it seems the issue arises directly from the input number values themselves, not the templates.

I decided to dig into the Lovelace configuration to see if there was a way to enforce a specific number of decimal places. Here’s how I have the relevant cards set up in my .dash file:

yaml
MinimumTemp:
widget_type: input_slider
title: Minimum Temperature
widget_style: “background-image: url(/custom_css/blur_light/img/Cold.jpg);”
title_style: “font-weight: bold;”
level_style: “padding-top: 20px; font-weight: bold; font-size: 250%”
level_up_style: “font-size: 150%”
level_down_style: “font-size: 150%”
unit_style: “padding-top: 15px; font-weight: bold”
units: “°C”
entity: input_number.min_living_temp
step: 0.1
precision: 1

I noticed that even though I’ve set precision: 1, the display still shows multiple decimal places. This led me to wonder if there’s a deeper configuration or workaround needed to enforce this formatting consistently across all input numbers.

After some research and experimentation, I found that one effective way to manage this is by using a combination of Lovelace card settings and custom JavaScript within the dashboard. For instance, you can create a custom card that formats the value before displaying it. Here’s a basic example of how that might look:

javascript
function formatTemperature(value) {
return parseFloat(value.toFixed(1));
}

This function ensures that the temperature value is always rounded to one decimal place before being displayed. While this requires a bit more setup, it provides a clean and consistent solution.

I’m curious to hear if others have encountered similar issues and what approaches they’ve taken to resolve them. Whether it’s through Lovelace configuration, custom scripts, or other methods, I’d love to learn more about the different ways people manage decimal precision in their smart home setups.

Additionally, if anyone has insights into why the extra decimal places appear in the first place, despite the step and precision settings, I’d appreciate hearing your thoughts. Understanding the root cause could help in finding a more elegant solution.

Thanks in advance for your input and experiences!

Best regards,
[Your Name]