Configuration Template. How To convert F to C on temperature?

one more question…
I have a couple more of these sensors coming, and I’m curious how HA associates the template with the sensor entity.
does the template only apply to an entity that has the state lookup in it?

so the current code below; when i have another sensor named something else, does this value show up just because the value template references an attribute of the sensor ?

  - platform: template
        unit_of_measurement: 'C'
        friendly_name: Upstairs Hall Temperature
        value_template: '{{ "%.2f"|format((( states("sensor.sensor_up_hall_temperature_air") | float ) -32) *5/9) }}'

First, let’s establish the correct terminology. All entities have a state and some also have attributes. That template (above) is using the states() function to get the state of the entity called sensor.sensor_up_hall_temperature_air. To summarize, that template doesn’t reference any attributes.

If you get a new sensor whose entity_id is sensor.sensor_downstairs_hall_temperature_air then you will create another Template Sensor with almost the same template except the states() function will now refer to the new sensor.

  - platform: template
        unit_of_measurement: 'C'
        friendly_name: Upstairs Hall Temperature
        value_template: '{{ "%.2f"|format((( states("sensor.sensor_up_hall_temperature_air") | float ) -32) *5/9) }}'
        unit_of_measurement: 'C'
        friendly_name: Downstairs Hall Temperature
        value_template: '{{ "%.2f"|format((( states("sensor.sensor_downstairs_hall_temperature_air") | float ) -32) *5/9) }}'

Yes i’m still learning the language/ terms, thanks.
I guess what i was curious about, is how this particular virtual sensor entity gets associated with the device. the only “link” i can see is this entity is referring to that state, therefore it links to that device. It was an assumption on my part, that i wanted to clarify so i dont mess up other sutff further down the road.

Thank you for the example, that’s exactly what i was planning on putting in the config for additional sensors.

I’m sorry to say but the guidance here over complicated the situation. If your device is in ºF and you want it in ºC, then put unit_of_measurement: 'ºF' and home assistant will naturally convert it to ºC. So all you needed in customize was:

      unit_of_measurement: '°F'

You have to use the correct degree symbol as well.

The resulting sensor would be °C if you set system units to metric.

To clarify: If you have system units set as °C and you tell the system that a sensor is °C. The system will say “Hey, this doesn’t need to be converted”. Where as if you give the sensor °F, the system will say “Hey, I need to convert this”.

1 Like

actually, that was the first thing tried.
when i just put a unit of measure of ‘C’ or ‘°C’, it just changes the unit, but does not convert the value.
perhaps HA doesn’t like the fact the original value is just ‘F’ with no degree symbol.

No, you aren’t understanding.

  1. The degree symbol is required.
  2. If you want it to convert to °C, you need to have unit_of_measurement as °F because your system is already set to metric (°C).

So to further clarify.

If your system units are metric:

°F will convert to °C.
°C will not convert, because it’s already °C.

if your system units are imperial:

°F will not convert, because it’s already °F.
°C will convert to °F.

So when you put your unit_of_measurement: °C, you’re telling your system to NOT convert it.

the sensor returns its temperature value as xx F, not as xx °F

this was tried: it resulted in the sensor reporting a F value with a C unit.

#  unit_of_measurement: '°C'

oh… wait… i understand what you are saying… let me try that.

well son of a bitch. that worked.

1 Like

It’s such a simple solution but it’s hard to grasp the first time you use it. Once it clicks, it makes perfect sense.

well thanks. that makes the config easier, although i do appreciate the exercise of the manual process in the thread anyway, as i learnt more that way.

1 Like

Your situation, having to convert Fahrenheit to Celsius (with Metric being the base unit system), is handled nicely by Home Assistant (as per petro’s advice posted above).

In contrast, what’s not (currently) possible is to display two different unit systems (i.e. some entities report in Celsius and others in Fahrenheit). The existing workaround is to omit the leading degree symbol because, without it, Home Assistant does not perform automatic temperature conversion. For more information, see this post.

1 Like

which all makes sense now.

Part of the problem, earlier in the thread, i wasn’t able to put the degree symbol in, my config was tossing an error about a bad symbol. it took my brain a day to realize i needed to change the encoding on the file to utf-8 and re-save my configs, then suddenly i could actually put a degree symbol in. but by then the manual / hard way solution was in place and working, so i didn’t think too much more about it.

Regardless, thanks everyone for the help. learnt a lot, and i’m still tinkering daily with HA configs. this system is such a yin-yang of complexity wrapped around flexibility. I dont like the fact i’ve sank a week + into learning and still feel like i’m only scratching the surface, but on the other hand, i cant think of anything which HA probably cant do given the correct inputs and figuring out how to write the syntax.

Yeah, that’s super annoying. I don’t know why the files default to ascii encoding. I had to do the same way back when. What text editor are you using to edit the files? That could be the root cause of the encoding errors.

EditPad Pro. a relatively unknown editor that i’ve been using for years, it’s awesome, full regex support and so much more, and no issues with encoding, it’s what i used to change the encoding.

Edit. my HA is in a docker container, using winSCP to actually get at the files on the pi.
I’ll use vi when i’m in linux directly, but nobody likes using vi. lol.

This does not seem to work for me, using the System Monitor sensor platform; it doesn’t seem to recognize the unit_of_measurement argument. I have my overall units set to Imperial, but I’d like to see CPU temperatures in Celsius, since that’s what my brain is calibrated to for computers :smiley:

Here’s what I have in my configuration.yaml file:

  - platform: systemmonitor
      - type: disk_use_percent
        arg: /home
      - type: memory_free
      - type: memory_use_percent
      - type: processor_temperature
        unit_of_measurement: '°C'

The units bit was just ignored, and my CPU temp still shows in F. Am I doing something wrong here, or is there a different/better way to accomplish this?

That’s working as intended. The units are always converted to your system wide units. If you want it to not convert, you have to use something other than ºC or ºF as a unit_of_measurement. You’ll also have to manually perform the conversion yourself.

I see…many thanks for the info. How would I go about doing the conversion myself? I definitely want my overall system units to be ºF, but I want the CPU temps to show in ºC.

I also have this question. I finally understand the posts from @petro, but I imagine for folks in the USA this is a pretty common need. We use imperial, so I want temps generally in Farenheit (humiture sensors, outdoor temp, etc) so my HA is set to that. But, when monitoring Raspberry Pi’s, Synology NAS, and other computers, most of us would want to see those in Celsius. What is the proper, and easiest way to do this in HA in 2021? I’ve been using HA significantly for more than a year now and this is one thing I still have yet to fully grasp…and one day I spent 4+ hours on this problem. Sure, I am probably being dense and over-complicating it some, but from everything I’ve read on the internet I don’t feel that it is incredibly intuitive.

1 Like

I second this complaint/request. Need to be able to display processor_temperatures in Celsius and not the “system configuration”. When we talk about computer systems temperatures few people use Fahrenheit.

One workaround to use command_line platform to cat /sys/class/thermal… However, this does not work for me on my system as the sensor moves around every boot. Attempting to do a longer code in the command_line platform like below throws command fail error

command: paste <(cat /sys/class/thermal/thermal_zone*/type) <(cat /sys/class/thermal/thermal_zone*/temp) | grep TCPU | awk '{print $2}'