Round(2) is rounding to 3 decimal places (NEWBIE)

Hi, i am trying to collect all my upstairs thermostats, and average them, and round them to 2 decimal places.

I have finally got the formatting correct (i think) and without the | round(2) i get the right result, but as soon as i add that in, i get the final temperature to 3 decimal places.

Hoping you can advise, and point me in the right direction :slight_smile:

Thank you

{% 
set upstairs_temp = (
states('sensor.ensuite_sensor_temperature') | float +
states('sensor.harper_s_thermostat_temperature') | float +
states('sensor.mia_s_thermostat_temperature') | float +
states('sensor.landing_thermostat_temperature') | float +
states('sensor.main_bedroom_thermostat_temperature') | float +
states('sensor.study_thermostat_temperature') | float 
)
/ 6 | round (2)
%}

{{upstairs_temp}}

Your template is rounding the number 6 only and not the entire calculation. Wrap the calculation in parentheses to make the round filter round the calculation’s result.

{% 
set upstairs_temp = ((
states('sensor.ensuite_sensor_temperature') | float +
states('sensor.harper_s_thermostat_temperature') | float +
states('sensor.mia_s_thermostat_temperature') | float +
states('sensor.landing_thermostat_temperature') | float +
states('sensor.main_bedroom_thermostat_temperature') | float +
states('sensor.study_thermostat_temperature') | float 
)
/ 6) | round(2)
%}

{{upstairs_temp}}

You should also supply the float filter with a default value. Otherwise, if it receives a non-numeric value it will cause an error message.

If you’re simply trying to calculate the average, you should consider using the average filter.

Reference: Templating - Numeric function and filters

thank you very much. I have just done the addition ( and ) and that has worked. knew it would be something simple.

Yes I’m just trying to do the average of the room (next step is to create a custom sensor and graph that).

I will take a look at the link you referenced and see if i can work out the average, because that would be seemingly much more simple :slight_smile:

Hi @123, thank you for the help yesterday. I am now getting this in my error log. Are you able to assist in how and why?

2022-11-22 13:35:01.545 ERROR (MainThread) [homeassistant.helpers.template_entity] TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set upstairs_temp = (( states('sensor.ensuite_sensor_temperature') | float + states('sensor.harper_s_thermostat_temperature') | float + states('sensor.mia_s_thermostat_temperature') | float + states('sensor.landing_thermostat_temperature') | float + states('sensor.main_bedroom_thermostat_temperature') | float + states('sensor.study_thermostat_temperature') | float ) / 6) | round (2) %} {{upstairs_temp}}' but no default was specified') while processing template 'Template("{% set upstairs_temp = (( states('sensor.ensuite_sensor_temperature') | float + states('sensor.harper_s_thermostat_temperature') | float + states('sensor.mia_s_thermostat_temperature') | float + states('sensor.landing_thermostat_temperature') | float + states('sensor.main_bedroom_thermostat_temperature') | float + states('sensor.study_thermostat_temperature') | float ) / 6) | round (2) %} {{upstairs_temp}}")' for attribute '_attr_native_value' in entity 'sensor.upstairs_temperature'

@123 i have just found your explaination here for someone else (here) i will try add the float(0) to all my floats in the template.

If thats the answer no need to reply :slight_smile: yes i still need to move it to the average function :slight_smile:

The error is due to what I explained in my first post:

If the sensor’s value is unknown then float(0) will report it as 0. However that will, of course, affect the calculation of the average value.

1 Like