Template Sensor rounding problems

I have this template sensor to get the valve position of a HomeMatic TRV:

      value_template: '{{ states.climate.frontlivingroom_trv.attributes.level * 100 | round|int }}'
      unit_of_measurement: '%'
      friendly_name: 'Front Living Room TRV Valve'

Most of the time this works, but sometimes I get this:

Screenshot 2020-12-16 194018

Anyone got any ideas as to what I’m doing wrong?

You’re rounding the number 100. The int filter is in the wrong place and you are using a method of obtaining the attribute that will generate errors if the sensor is not ready (like after a restart).


 value_template: "{{ ( state_attr('climate.frontlivingroom_trv', 'level')|int * 100 ) | round(0) }}"
1 Like

Thanks @tom_l. It’s amazing that with so much wrong with my template it actually produced valid results most of the time! That’s why templates are still a bit of a black art for me.
I’ll keep an eye on it over the day as the heating goes on and off to see how it works out.

Hi @tom_l, just a quick update on this - I noticed that the template was returning 0% a lot and I think it was caused by the integer conversion in the template. The TRV returns the valve opening as a number from 0.0 to 1.0 so I presume anything less than 1 was being returned as 0.
Here is the working version without the integer conversion:

value_template: '{{ states.climate.frontlivingroom_trv.attributes.current_temperature | round(1) }}'

Yes, int acts as a floor operation.

One thing I love about Home Automation is that you never stop learning :nerd_face: :sob: