Round to whole number

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
"{{ (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

3 Likes

That’s amazing, thanks for your help

1 Like

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 :wink: