Template sensor from MQTT not working

Hi all!

I’ve got two MQTT sensors for my BBQ ThermPro sensor which works great. The temperature is sent in degrees C, and I’d like to create two new sensors to show the value in degrees F. I’ve got the following config in configuration.yaml:

sensor:
  - platform: mqtt
    name: 'bbq_food_probe'
    device_class: "temperature"
    state_topic: 'sensors/rtl_433/Thermopro-TP12/81/temperature_1_C'
    unit_of_measurement: '°C'
    icon: mdi:thermometer
  - platform: mqtt
    name: 'bbq_chamber_probe'
    device_class: "temperature"
    state_topic: 'sensors/rtl_433/Thermopro-TP12/81/temperature_2_C'
    unit_of_measurement: '°C'
    icon: mdi:thermometer
  - platform: template
    sensors:
      bbq_food_probe_f:
        friendly_name: 'BBQ Food Temperature °F'
        entity_id: sensor.bbq_food_probe
        unit_of_measurement: '°F'
        value_template:  "{{ ((float(states('sensor.bbq_food_probe')) * 9 / 5 )  +  32) | round(1) }}"
        device_class: "temperature"
      bbq_chamber_probe_f:
        friendly_name: 'BBQ Chamber Temperature °F'
        entity_id: sensor.bbq_chamber_probe
        unit_of_measurement: '°F'
        value_template: "{{ ((float(states('sensor.bbq_chamber_probe')) * 9 / 5 )  +  32) | round(1) }}"
        device_class: "temperature"

Problems:

  1. In the states screen the values are just a replica of the source probe
  2. The “unit_of_measurement” for the new probe is degrees C, despite explicitly being defined as oF in configuration.yaml
  3. When I add the templated sensor to a dashboard, both sensors appear

Thanks for any assistance you can offer - Been playing with HA for about a week now and I’m addicted!

Thanks,

Neil

I haven’t seen a float declared like that before and is probably your issue. It should look more like:

"{{ ( states('sensor.bbq_food_probe') | float ) * 9 / 5  + 32  | round(1) }}"

Not sure if you need to declare as a float. I’m sure others can chime in as to why.

Test your template in the template editor :

1 Like

Hi - Thanks for the reply.

I had originally set it as you’d described but had the same result - Have set it back now but still getting the same result.

- platform: mqtt
    name: 'bbq_food_probe'
    device_class: "temperature"
    state_topic: 'sensors/rtl_433/Thermopro-TP12/81/temperature_1_C'
    unit_of_measurement: '°C'
    icon: mdi:thermometer
  - platform: mqtt
    name: 'bbq_chamber_probe'
    device_class: "temperature"
    state_topic: 'sensors/rtl_433/Thermopro-TP12/81/temperature_2_C'
    unit_of_measurement: '°C'
    icon: mdi:thermometer
  - platform: template
    sensors:
      bbq_food_probe_f:
        friendly_name: 'BBQ Food Temperature °F'
        entity_id: sensor.bbq_food_probe
        unit_of_measurement: '°F'
        value_template:  "{{ ( states('sensor.bbq_food_probe') | float ) * 9 / 5  + 32  | round(1) }}"
        device_class: "temperature"
      bbq_chamber_probe_f:
        friendly_name: 'BBQ Chamber Temperature °F'
        entity_id: sensor.bbq_chamber_probe
        unit_of_measurement: '°F'
        value_template: "{{ ( states('sensor.bbq_chamber_probe') | float ) * 9 / 5  + 32  | round(1) }}"
        device_class: "temperature"

I originally didn’t have the cast to a float but got an error in the logs about trying to divide an str and an int.

You don’t need to perform the calculation. Home assistant will do it for you if you put the unit_of_measurement as ºC. It will see that your system has a unit of ºC and convert it to what is set inside your configuration.

You cannot display ºF and ºC on the same system in Home Assistant because of this.

Top tip - Thanks!

Somewhat frustratingly that works fine:

Hi Petro - Thanks for the reply, unfortunately not quite sure what you mean - What should the configuration be then?

I don’t want the unit of measure to be ºC - I want to display ºF on my dashboards but don’t seem to be able to set the unit_of_measure in the UI editor:

I’m also confused as to why there are two graphs shown here for a single entity.

I’m wondering if in trying to get this set up there’s some cruft in the history/HA DB which is confusing issues. I’ve cleared state from home-assistant_v2.db using sqlite3, but still no dice.

You should only need this:

  - platform: mqtt
    name: 'bbq_food_probe'
    device_class: "temperature"
    state_topic: 'sensors/rtl_433/Thermopro-TP12/81/temperature_1_C'
    unit_of_measurement: '°C'
    icon: mdi:thermometer
  - platform: mqtt
    name: 'bbq_chamber_probe'
    device_class: "temperature"
    state_topic: 'sensors/rtl_433/Thermopro-TP12/81/temperature_2_C'
    unit_of_measurement: '°C'
    icon: mdi:thermometer

You may need to delete your current database. It should convert your temperatures to whatever is set on your system.

Your system units are set inside Configuration -> General Configuration:

image

Once you clear your database (or wait for it to clear). All temperatures should naturally convert to ºF if you properly set the units on the sensors themselves. I.E. if the unit_of_measurement on the integration is set to ºC, it will convert it to ºF. Any ºF will not be converted, just passed through.

Thanks Petro.

Being in the UK everything is metric - Literally the only time I use imperial is cooking on the BBQ because all the recipes are in °F!

Removing unit_of_measure from the template fixed it - Clearly HA was converting my measurement back to °C!

Thanks all for your help - Great project and a great community.

Cheers,

Neil

The answer has already been posted but I’ll post my results anyway.

After some experimentation, I can confirm that excluding unit_of_measurement is (currently) the only way of preventing automatic temperature conversion.

My Unit System is set to Metric and the following Template Sensor allows me to display sensor.outside_temperature not in the default Celsius units but in Fahrenheit.

      outside1:
        friendly_name: "Outside Temperature V1"
        value_template: "{{ float(states('sensor.outdoor_temperature')) * 9 / 5 + 32 }}"
        device_class: temperature

The resulting badge displays the converted value but lacks a unit symbol.

Screenshot from 2020-04-13 13-23-15

Compared to the entity it is based on:

Screenshot from 2020-04-13 13-27-01

Thanks Taras -

So while I can now get the right metric, I can no longer display a graph of it, as the history card in Lovelace only displays a graph if unit_of_measurement is specified :confounded:

Managed to hack around this by adding a descriptive but not automatically parsed unit of measure:

  - platform: template
    sensors:
      bbq_food_probe_f:
        friendly_name: 'BBQ Food Temperature °F'
        entity_id: sensor.bbq_food_probe
        value_template:  "{{ ( states('sensor.bbq_food_probe') | float ) * 9 / 5  + 32  | round(1) }}"
        device_class: "temperature"
        unit_of_measurement: "deg. F"
      bbq_chamber_probe_f:
        friendly_name: 'BBQ Chamber Temperature °F'
        entity_id: sensor.bbq_chamber_probe
        value_template: "{{ ( states('sensor.bbq_chamber_probe') | float ) * 9 / 5  + 32  | round(1) }}"
        device_class: "temperature"
        unit_of_measurement: "deg. F"

And now graphing again - Thanks all!

Neil

Yes, I can confirm your results: using a unit other than '°F' will avoid automatic temperature conversion and allow for graphing.

In the following example, I simply omitted the degree symbol and used 'F' for unit_of_measurement.

      outside1:
        friendly_name: "Outside Temperature V1"
        value_template: "{{ float(states('sensor.outdoor_temperature')) * 9 / 5 + 32 }}"
        device_class: temperature
        unit_of_measurement: 'F' 

Screenshot from 2020-04-13 13-39-28

The resulting graph is a line chart (not worth showing here because the local outdoor temperature hasn’t changed while I’ve performed these tests).