Hi,
I am trying to round this to the closest whole number.
"{{ states('sensor.id_4_range') | float / 1.609 | round(1) }}"
I want to remove all decimal point and round down if the value is less than 0.5 e.g., 95.4 would equate to 95, or round up if the value is equal to or greater than 0.5 e.g., 95.5 or 95.7 would equate to 96.
Any ideas?
Thanks.
1 Like
123
(Taras)
November 18, 2022, 6:43pm
2
"{{ (states('sensor.id_4_range') | float(0) / 1.609) | round(0) }}"
You have to group the calculation with parentheses so that the result of the calculation is rounded. If you omit the parentheses, the rounding is performed on the value 1.609
only.
round(0)
rounds the supplied value to the nearest integer value (in the manner you requested).
Reference: Templating - Number Functions and Filters
4 Likes
That’s amazing, thanks for your help
1 Like
mpschr
(michphip)
March 15, 2024, 9:39pm
5
Found the answer on a thread with the exact same device. A VW ID.
For other interested people, I just added two sensors with theoretical ranges based on the range sensor and the state of charge sensor.
template:
- sensor:
- name: "ID4 theoretical 100% range"
unique_id: "sensor.grissi_theoretical_100p_range"
unit_of_measurement: "km"
device_class: distance
state_class: measurement
state: "{{ (states('sensor.grissi_range') | float() / states('sensor.grissi_state_of_charge') | float() * 100) | round(0) }}"
- sensor:
- name: "ID4 theoretical 80% range"
unique_id: "sensor.grissi_theoretical_080p_range"
unit_of_measurement: "km"
device_class: distance
state_class: measurement
state: "{{ (states('sensor.grissi_range') | float() / states('sensor.grissi_state_of_charge') | float() * 80) | round(0) }}"
I was actually looking how to do simple math on the statistics card, but here ware
1 Like
ceekay1
(Ceekay1)
November 12, 2024, 4:05pm
6
Hi there. I use round, and most of the time it does what I want. Now I found a situation where it doesn’t work as expected.
The sensor is a template calculating the absolute water level:
{{ (1000*e**(19.016-(4064.95/(float(states('sensor.luft_aussen_temperature'))+236.25)))*100/(461.66*(float(states('sensor.luft_aussen_temperature'))+273.15)) * float(states('sensor.luft_aussen_humidity'))/100 | float) | round(1) }}
Currently it calculates 6.5
In developer tools - template this code
{{ float(states('sensor.luft_aussen_abs_feuchtigkeit')) }}
{{ float(states('sensor.luft_aussen_abs_feuchtigkeit'))|round(0) }}
or shorthand this
{{ float(6.5) }}
{{ float(6.5)|round(0) }}
outputs
Ergebnistyp: string
6.5
6
I expected to get a 7 in the second line. What am I missing?
{{ float(6.6)|round(0) }}
Renders 7, so I think it is a bug in round()?
Thanks for any input.