Template question- what's wrong here?

I have a wireless thermometer that publishes the temperature in °C over MQTT. It works.
I tried to make a template sensor to create an entity in °F. It doesn’t work.
office

I suspect that I am not using the template sensor correctly, can someone help, please?

#In mqtt.yaml:
  sensor:
    - name: "Office Temperature"
      unique_id: "office_temperature"
      state_topic: "rtl_433/Zotac/devices/Oregon-v1/1/15/temperature_C"
      unit_of_measurement: "°C"


#In sensors.yaml
- platform: template
  sensors:
    office_temperature_fahrenheit:
      friendly_name: "Office Temperature (Fahrenheit)"
      value_template: "{{ (states('sensor.office_temperature_celsius') | float * 9/5) + 32 }}"
      unit_of_measurement: "°F"

Hi,
For the sensor template you need to use “state” instead of “value_template”.
Nick

Not if they are using legacy template sensors, which they are.

Steve, you should be using the new format for new sensors.

You can change the temperature unit from the frontend UI, but if you want another sensor with the different unit:

  1. add a default value for your float filter. If you don’t this can prevent your sensor loading.
  2. use the template editor to work out what is going on, e.g. what does this return:
{{ states('sensor.office_temperature_celsius') }}
{{ (states('sensor.office_temperature_celsius') | float(0) * 9/5) + 32 }}

How it would look in the new format:

# In configuration.yaml
template:
  - sensor:
      - name: "Office Temperature (Fahrenheit)"
        state: "{{ (states('sensor.office_temperature_celsius') | float(0) * 9/5) + 32 }}"
        availability: "{{ has_value('sensor.office_temperature_celsius') }}"
        unit_of_measurement: "°F"
        device_class: temperature 
        state_class: measurement 

Many thanks. I am trying to learn templates… There seems to be too many different types of templates for me to grok.

{{ states('sensor.office_temperature') }}
Retures: “18.9”

{{ (states('sensor.office_temperature') | float(0) * 9/5) + 32 }}
Returns: 66.02

With a few typo changes, your suggested template works:

template:
  - sensor:
      - name: "Office Temperature (Fahrenheit)"
        state: "{{ (states('sensor.office_temperature') | float(0) * 9/5) + 32 }}"
        availability: "{{ has_value('sensor.office_temperature') }}"
        unit_of_measurement: "°F"
        device_class: temperature 
        state_class: measurement

I can’t find availability: in the docs. In another post you said: “Anything that can not be converted to an integer, like unavailable or unknown will be given the default value of -1.” How does has_value change this? (Around here a value of -1 is a valid temperature).
.

That might be easier. How do I do this and where is it in the docs?

Click on your sensor in the dashboard. In the pop-up window click on the settings cog icon. Units should be an option and will do the conversion for you if you change them.

That’s what my notes said, but this is what the settings cog presents:

Which integration is providing that temperature sensor?

What attributes does the sensor have?

It probably needs a device class of temperature.

Thanks for the link.

No integration. The values are coming over MQTT from an external broker. In my first post I define the sensor in mqtt.yaml

Just what I gave it when creating the sensor in mqtt.yaml.

Give it a device class, restart, then see if you can change the unit via the UI:

#In mqtt.yaml:
  sensor:
    - name: "Office Temperature"
      unique_id: "office_temperature"
      state_topic: "rtl_433/Zotac/devices/Oregon-v1/1/15/temperature_C"
      unit_of_measurement: "°C"
      device_class: temperature
1 Like

Thanks. I learned something today.