# Convert celsius to fahrenheit in sensor

Receiving values just fine with MQTT of course they are in Celsius. spent hours trying different things and using the googler

I am sure I am close but…

My sensor template is:

``````- platform: mqtt
state_topic: "rtl_433/Acurite-609TXC/64/temperature_C"
value_template:{{ states('sensor.back_room_temp') | float * 5 / 9  + 32 | round(2) }}
device_class: 'temperature'
unit_of_measurement: "°F"
name: "back Room Temp"
force_update: "true"
``````

I am getting a value of 32F in lovelace so it is just adding the 32 to 0. MQT is reporting 21.2 C so it should be showing 70.16. Somehow I don’t think it is getting the value of MQTT statement. Can you let me know where I am going wrong.

I have a better example somewhere but this is what I could readily find, it is an example where I had to convert F to C then back to F for calculating dew point. I found that sometimes the number of characters is limited so you have to substitute variables:

``````- platform: template
sensors:
bathroom_master_dewpoint:
friendly_name: "Bathroom Master"
value_template: >-
{% set h = states('sensor.bathroom_master_humidity') | float %}
{% set t = states('sensor.bathroom_master_temperature') | float %}
{{((t-32)*5/9-(100-h)/5)*9/5+32}}
unit_of_measurement: "F"

``````
1 Like

OK so I think I understand

I have a value coming from this sensor in C:

``````    state_topic: "rtl_433/Acurite-609TXC/64/temperature_C"
device_class: 'temperature'
name: "back Room Temp"
force_update: "true"
``````

then I take that value sensor.back.room and using a template create a new sensor with a F value

``````  - platform: template
sensors:
back_room_f:
friendly_name : "Back Room Temp F"
value_template: >-
{% set t = states('sensor.back_room_temp') | float %}
{{((t)*9/5)+32}}
unit_of_measurement: "F"
``````

in Developer Tools I get the correct value of F

In file editor i get the green check mark but when I go to check configuration validation it will just spin and not let me restart. Any ideas?

Solved

Just a follow up with what worked.

First created a sensor that received the data:

``````  - platform: mqtt
state_topic: "rtl_433/Acurite-609TXC/64/temperature_C"
device_class: 'temperature'
name: "back Room Temp"
force_update: "true"

``````

Then created a template that converted form C to F and put in the sensor section (I didn’t get this in my head at first)

``````  - platform: template
sensors:
back_room_temp_f:
friendly_name: "Back room F"
unit_of_measurement: '°F'
value_template: >-
{% set t = states('sensor.back_room_temp') | float %}
{{((t)*9/5)+32}}
``````

and vola, added sensor.back_room_temp_f to the card. This kicked my butt but I learned a lot

Thanks for pointing me in the right direction

4 Likes

You might as well add the dew point since you have temperature and humidity.

https://community.home-assistant.io/t/dewpoint-comfort-level-display/253344

1 Like

Hey All!
So the code @watertime posted works great in developer tools / template and converts perfectly from C → F

But in lovelace, no love!
The ezm temp comes in as F, it’s a multifunction zwave device, homeseer multi sensor

In config yaml

``````sensor:
- platform: template
sensors:
hot_room_temp_f:
friendly_name: "Hot Room F"
unit_of_measurement: '°F'
value_template: >-
{% set t = states('sensor.ezm_air_temperature_9') | float %}
{{((t)*9/5)+32}}
``````

And love lace, using apex charts

``````type: custom:apexcharts-card
graph_span: 48h
color_list:
- yellow
experimental:
color_threshold: true
show: true
title: Hot Room Temp
show_states: true
colorize_states: true
series:
- entity: sensor.hot_room_temp_f
name: Mining Room F
stroke_width: 5
``````

If it works in dev tools and not lovelace, I must be missing something small here.
Thanks!!

Dev tools

Lovelace

Hello everyone,

I found an alternate way to do this conversion json within my configuration.yaml file in one of my MQTT sensors.

I hope this helps someone.

``````mqtt:
sensor:
- name: "Current Battery TempC"
unique_id: "current_battery_tempc"
state_topic: "NodeRenogy/state"
value_template: "{{ float(value_json['battT']) | multiply(9/5) + int(32) }}"
unit_of_measurement: "°F"
- name: "Current Controller Temp"
unique_id: "current_controller_temp"
state_topic: "NodeRenogy/state"
value_template: "{{ float(value_json['controlT']) | multiply(9/5) + int(32) }}"
``````
2 Likes

It is actually even easier now, just add a device_class of temperature and you can select C/F or K and HA will do the conversions; I just learned of this in the past week.

3 Likes

Although the mqtt platform method has changed since this was posted, the basic concept works in the most recent 2023 versions of HA.

Arrggg, I’m pulling my hair out trying to get a rounded result.

Here’s my config snippet:

``````mqtt:
sensor:
- name: "Outside Temperature"
state_topic: "agt/wx/bme280/temperature"
unit_of_measurement: "..F"
value_template: "{{ value | multiply(9/5) + 32 | round(1) }}"
device_class: "temperature"
``````

But when the value is say, 6.6, the result shown is 43.879999999999995. It’s like it not seeing the final round(1) in the filter. I’ve tried every sort of different expression, but can’t figure it out.

What version of HA are you on? If you are on a recent version, this can be done in the GUI to fix the decimal places.

I’m not sure if the “…F” will be recognized as “F” or not though. You should be able to allow it to come in as “C” and not have to do the conversion yourself now.

Thanks. Much of this is me not really understanding the filter syntax, which I believe is jinja. I thought it reads from left to right, piping the result to the next command, like in a shell.

What looks like a “…F” is actually “ºF”, but I was using vi in a docker container, so it copied and pasted as that. Regardless though, I must be doing something wrong, or I don’t know what GUI you are referring to. Here’s the setting GUI I know of:

Setting the `unit_of_measurement` to any of ‘F’, ‘ºF’, ‘C’, or ‘ºC’ simply changes the label but had no effect on the value. I am on the latest HA currently at 2023.4.1.

I found a similar posted question, and modified a template suggested there to end up with:
`value_template: '{{ "%.1f"|format((( value | float ) *9/5) +32) }}'`

That worked, but confused me even more with a combination of parenthetical grouping and pipes.

2 Likes

The simplist way to access the gui for it is to just click on the temperature on your lovelace then click the settings gear in the top right then you ‘should’ be able to select unit of measurement. This will automagically make all the conversions. There is also a “Display precision” that allows you to select the number of digits you want to display. For me all I had to do was change the device class to temperature.

This is not limited to temperature thou, there has been a lot of device classes added lately that effect this. Rain rate is one of the newest ones.

Well, given the title and purpose of this thread, using the system conversion via the GUI is probably the right answer. I discovered the reason I did not see the feature on the GUI, which is because the format of the degree symbol is not correct in the config file. Running via docker using macOS, the alt/option-0 key combo does not work. I get the error: `using native unit of measurement 'ºF' which is not a valid unit for the device class ('temperature') it is using; expected one of ['°C', '°F', 'K'];`

Crazy… the difference between the characters º and ° and how to determine how to achieve the accepted degree symbol.

As far as the jinja syntax is concerned, I’ll search for help in another thread.

Parenthesis are important. I ran across this in a past template before and I discovered that all the final round(1) is rounding is the number right before it. In this case, 32. So in order to get proper rounding with your template, use parenthesis as such:

``````value_template: "{{ (value | multiply(9/5) + 32) | round(1) }}"
``````

I forgot that it helps to have a float in there too:

``````value_template: "{{ (value | multiply(9/5) + 32) | float | round(1) }}"
``````
1 Like

@MontanaMac Thanks for posting. This worked for me! HA 2023.1.7

This worked for me:

``````{{ (states('sensor.weatherflow_air_temperature') | float )*9/5+32 | round(2) }}
``````

Thanks for that, I’d have spent forever trying to figure this out.

2024.3.3