Analog mqtt sensor conversion

Hi, I made recently a board with an analog sensor which reads the temperature of a water tank and sends it over mqtt. This is the raw value, so the conversion must be done on hass.
I tried with value_template like this but it doesn’t work because it seems like it can read the actual value of the sensor.

    - name: SolarTemp analog
      platform: mqtt
      state_topic: home/solar/analog
      friendly_name: "Water Temp"
      unit_of_measurement: "°C"
      value_template: "{{ ((states.sensor.solartemp_analog.state | float  * -0.136709) + 113.362 )  }}"

If the value_template is left only with the state, it shows a wrong value too (always shows 0)

       value_template: "{{ states.sensor.solartemp_analog.state | float  }}"

The weird part is that if I remove the value_template the analog value is shown correctly on hass and if I put it on the template debugger it shows the correct temperature, so it looks like the template is ok.

    {{ ((states.sensor.solartemp_analog.state | float  * -0.136709) + 113.362 )  }}

The value_template is the function that stores the data from the message into the state. Using the value from the state in it doesn’t get anything from the message.

The payload from the message is stored in the value field (see the templating documentation ).

So the value_template should be something like

value_template: "{{ (value | float)  * -0.136709 + 113.362 }} "
1 Like

Thanks, it works fine now.
The template shown on the Serial analog sensor page confused me.:sweat_smile: