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"
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.
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.
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: