How to display both °C and °F?

I have a simple thermometer using a DS18b20 sensor and an SSD1306 128x64 OLED display, and it works just fine. But I would like to display both Centigrade and Fahrenheit on the display. Is there a way that I can create a new entity with the Fahrenheit value?

# Simple thermometer. A DS18b20 sensor and an SSD1306 128x64 OLED display
#
substitutions:
  device_name: thermometer
  
esphome:
  name: ${device_name}
  
esp8266:
  board: d1_mini

logger:
  level: VERBOSE

api:

ota:
  safe_mode: True

packages:
  wifi: !include common/wifi.yaml
  
sensor:
  - platform: dallas
    address: 0xd00217924557a228
    name: ${device_name} Temperature
    id: temperature_c
    resolution: 9
    


dallas:
  - pin: D4
    update_interval: 5.0s
    
i2c:
  sda: D2
  scl: D1
  frequency: 800kHz


# Define a blinking LED
output:
  - platform: gpio
    pin:
      number: D5
      mode: output
    id: alarmLED
    inverted: false


# Check the temperature then turn on or off the blinking LED
interval:
  - interval: 2s
    then:
      if:
        condition:
          sensor.in_range:
            id: temperature_c
            above: 25.0
        then:
        - output.turn_on: alarmLED
        - delay: 500ms 
        - output.turn_off: alarmLED
        - delay: 500ms
        else:
          output.turn_off: alarmLED
          
          
# Display stuff
font:
  - file: 'arial.ttf'
    id: font1
    size: 12  
  - file: 'arialbd.ttf'
    id: font2
    size: 24  


display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    lambda: |-
      it.print(0, 0, id(font1), "Temperature_c");  
      if (id(temperature_c).has_state()) {
        it.printf(0, 15, id(font2), TextAlign::TOP_LEFT, "%.1f°C", id(temperature_c).state);
      }

Create a template sensor, the formula to convert C to F is easy to find.

Thanks, that pointed me to the solution.

sensor:
  - platform: dallas
    address: 0xd00217924557a228
    name: ${device_name} Temperature_C
    id: temperature_c
    resolution: 9
    
  - platform: template
    name: ${device_name} Temperature_F
    id: temperature_f
    lambda: return id(temperature_c).state * 9/5+32;
    update_interval: 5s

It took me a while to realize that the template updated separately from the dallas update interval. I would see the °C updating every 5s, but the °F every minute. That’s when it clicked- “there must be a default of 60s”.


Another topic?
I plan to use °F in my HomeAssistant front-end, but if I wanted to use °C I would have to calculate is again since Home Assistant “automagically” converts °C to °F. Is there a way to turn this automatic conversion off?

1 Like

Since 2022.4 you can set a temp sensor to C,F or K.

I have 2022.4.8. What do I need to do?
unit_of_measurement: ‘°C’ doesn’t do anything.

I read in the git:
" Deprecate temperature conversion in base entity class #68978"
that this removes the automatic conversion as of 2022.4.0b4

But, the automatic conversion is still taking place:

Home assistant I mean

Figured it out.
The syntax:
unit_of_measurement: C
is correct.

Hello,

Where do you define/apply the above?

I have following entry for temp sensor in a dashboard to display temperature in both C and F, one per line:

type: entities
entities:
  - entity: sensor.master_bedroom_temp_sensor_temperature
    unit_of_measurement: F
  - entity: sensor.master_bedroom_temp_sensor_temperature
    unit_of_measurement: C
  - entity: sensor.master_bedroom_temp_sensor_humidity
  - entity: sensor.master_bedroom_temp_sensor_pressure
  - entity: sensor.master_bedroom_temp_sensor_battery
title: ZB Outdoor temp sensor - porch, on a tree
state_color: true
show_header_toggle: false

But the output still shows the F only…

image

This thread is about esphome.

Who are you asking what? Speak in complete sentences.
If you are referring to post #3, it is in my ESPHome device yaml file.

In Home Assistant you can make a new entity (template entity, actually) in your configuration file. Here’s one that I did to use the sensor value (it provides windspeed in meters/second) to create a new template entity in knots. You can use this as an example to do the same with temperatures.

template:
  - sensor:
    - name: 'windspeed in knots'
      unique_id: 'windspeed_kts'
      state: "{{ ((states('sensor.openweathermap_wind_speed') | float(0)) * 1.94384) | round(0) }}"
      unit_of_measurement: 'kts'

This new sensor is now:

sensor.windspeed_in_knots

Thanks.

Instead of defining a new sensor and its attributes, is it possible only to define a formula of conversion as a function/macro and call that function when displaying properties on the dashboard?

Probably through a template.
That’s the extent of my expertise, (In other words, I found it a lot easier to simply make a new sensor entity.)

FWIW, here’s a way to do it directly on the sensor WITHOUT a template, if you just want Fahrenheit instead of Celsius or both:

- platform: dallas
    address: 0xa80722b1b1ebc328
    filters:
      - lambda: return x * (9.0/5.0) + 32.0;
    unit_of_measurement: "°F"
    name: "Garage Fridge"

That is a template.