Template sensor - different resultsfrom sensor and in template preview

I have an MQTT sensor that is returning temperature from ESP8266 device, defined like this:

  - platform: mqtt
    state_topic: "Temp1/Salon/Temperature"
    name: "salon_temperature"
    unit_of_measurement: "°C"
    expire_after: 180

This sensor is then used in template sensor defined as below:

      temperatura_salon:
        friendly_name: "Salon"
        unit_of_measurement: '°C'
        value_template: >-
          {{ states.sensor.salon_temperature.state | float - 2.5 }}

I use a template to apply offset. In the above case, I need to subtract 2.5 Celsius degrees.
I’m getting weird results because the value looks like this:
image

The weirdest part is that when I use template editor I get two results:

In the preview, you can see ORG - the original value (MQTT sensor), MOD is the value of my template sensor and NEW is the value template taken from my template sensor.

First of all, I get weird rounding. All I need is one decimal place, second is the wrong value.
In my template, I’m subtracting 2.5, but the MOD has 2.4 subtracted.

That’s a long way to go about it when the mqtt sensor supports a value template.

  - platform: mqtt
    state_topic: "Temp1/Salon/Temperature"
    value_template: "{{ value - 2.5 }}"
    name: "salon_temperature"
    unit_of_measurement: "°C"
    expire_after: 180

No template sensor required.

1 Like

Doh :man_facepalming:
I’ll try that when I get back home.

But the thing that troubles me so much is why there is a difference in the first place and why I get that weird rounding. Any ideas on that?
Maybe it’s a bug or it’s just the lack of knowledge.

Epsilon error. Wrong link. Try https://en.wikipedia.org/wiki/Machine_epsilon

So why there are differences in evaluating the same template.
Especially those two give bad results:

MOD: {{ states.sensor.temperatura_salon.state }}
NEW: {{ states.sensor.salon_temperature.state | float - 2.5 }}

image

MOD is the state of the template sensor and NEW is just the value_template of that template sensor.
In theory, they should have the same value.
The value_template is returning the correct value, but the state of the template sensor is wrong.
Not sure how they are evaluated, I don’t know the insides of HA.

What does this give:

MOD: {{ states.sensor.temperatura_salon.state }}
NEW: {{ states.sensor.salon_temperature.state }}

MOD: 24.0
NEW: 26.4

I didn’t apply the value_template for the MQTT sensor.

So why do you think they should be the same?

The problem is with sensors naming.
I’ll describe this with English names, it will be easier for all of us.

I have an MQTT sensor:

  - platform: mqtt
    state_topic: "Temp1/Salon/Temperature"
    name: "original_sensor"
    unit_of_measurement: "°C"
    expire_after: 180

and a template sensor:

fixed_sensor:
  friendly_name: "Salon"
  unit_of_measurement: '°C'
  value_template: >-
    {{ states.sensor.original_sensor.state | float - 2.5 }}

Now inside the template editor, I have 3 lines:

ORG: {{ states.sensor.original_sensor.state }}
MOD: {{ states.sensor.fixed_sensor.state }}
NEW: {{ states.sensor.original_sensor.state | float - 2.5 }}

Assuming ORG is returning 26.4, then MOD is returning 24 and NEW 23.9 (this is correct).

My point is that I have the same template, for value_template of fixed_sensor and as the third line in the template editor. So both values (second and third) should be the same.

I hope the description is more clear now.