I have two utility helpers for miles my car is doing (daily and monthly), working perfectly but i want to knock the amount of decimal places down - any suggestions ?
Ideally you could specify the value’s appearance in the Helper’s configuration and/or in the card displaying the Helper’s value. However, this functionality isn’t available so you must create a Template Sensor. It lets you control its value’s appearance. So you will use a Template Sensor to duplicate the Helper’s value and display it in the format you prefer.
For example, if the Helpers are:
input_number.daily_miles_utility
input_number.monthly_miles_utility
then you can create two Template Sensors like this:
template:
- sensor:
- name: Daily Miles Utility
state: "{{ states('input_number.daily_miles_utility') | float(0) | round(5) }}"
- name: Monthly Miles Utility
state: "{{ states('input_number.monthly_miles_utility') | float(0) | round(5) }}"
The round()
function is used to round the value to 5 decimal places.
The resulting Template Sensors will be:
sensor.daily_miles_utility
sensor.monthly_miles_utility
Any idea why this isnt rounding to 2?
Daily: £{{ (states('sensor.utility_meter_gas_daily_kwh') | float(0) * 3.537) / 60 | round(2)}}
Because it’s only rounding the number 60
and not the result of the calculation. You have to use parentheses to demarcate what should be rounded.
Daily: £{{ ((states('sensor.utility_meter_gas_daily_kwh') | float(0) * 3.537) / 60) | round(2) }}
Hi, can anyone help me?
I have created a custom sensor and I am trying to format the code below to only return the output before the decimal place? I’m new to HA and coding and I have been looking at this for a couple of days on and off and I’m not sure if this is possible, if so how to do it.
- platform: template
sensors:
powerwall_charge_actual:
friendly_name: powerwall charge actual
#state_class: "measurement"
unit_of_measurement: "%"
device_class: "battery"
value_template: >-
{% set percent = ((states('sensor.powerwall_charge') | float(0) / 100.0) - (0.05)) / (0.95) %}
{{ percent * 100 }}
The current output when viewing via a template editor is - 64.210
I would like it to output the result just as 64
Try
{{ (percent * 100)|round(0) }}
Thank you much appreciated
Sorry one more question, it looks like the sensor card is rounding the figure up still, is there another way to return the actual digits without rounding, i.e. return the number 70 not 71?
Use int(0)
instead of round(0)
You can set the decimal places in the entity settings (Display Precision)
Perfect thanks…