Template sensor lamdba return error

The BME680 sensor uses Celsius values, and HA converts these for the frontend, but I also want to display Fahrenheit values on a SSD1306 wired to the nodeMCU. I wrote this template to return display_temperature from bedroom_temperature:

  - platform: template
    id: display_temperature
    internal: true
    filters:
      - lambda: return ('bedroom_temperature' | float) * (9.0/5.0) + 32.0;

but I get the following errors on compiling:

/config/esphome/bedroomthermostat.yaml:52:15: warning: character constant too long for its type
   52 |       - lambda: return ('bedroom_temperature' | float) * (9.0/5.0) + 32.0;
      |               ^~~~~~~~~~~~~~~~~~~~~
/config/esphome/bedroomthermostat.yaml: In lambda function:
/config/esphome/bedroomthermostat.yaml:52:39: error: expected primary-expression before 'float'
   52 |       - lambda: return ('bedroom_temperature' | float) * (9.0/5.0) + 32.0;
      |                                       ^~~~~
/config/esphome/bedroomthermostat.yaml:52:38: error: expected ')' before 'float'
   52 |       - lambda: return ('bedroom_temperature' | float) * (9.0/5.0) + 32.0;
      |              ~                       ^~~~~~
      |                                      )
*** [/data/bedroomthermostat/.pioenvs/bedroomthermostat/src/main.cpp.o] Error 1

What have I got wrong here? Thanks for any help.

I think

lambda: id(bedroom_sensor).state * (9.0/5.0) + 32.0;

Thank you. This code will compile:

  - platform: template
    id: display_temperature
    device_class: temperature
    state_class: measurement
    filters:
      - lambda: return id(bedroom_temperature).state * (9.0/5.0) + 32.0;

but doesn’t display on the attached SSD1306. That code is:

display:
  - platform: ssd1306_i2c
    id: oled1
    update_interval: .5s
    contrast: 0.4
    model: "SSD1306 128x64"
    rotation: 0
    address: 0x3C
    lambda: |-
      it.printf(64, 4, id(font2), TextAlign::TOP_CENTER, " %.1f°", id(display_temperature).state);
      it.printf(0, 64, id(font3), TextAlign::BASELINE_LEFT, "Set to: %.1f°", id(bedroom_thermostat).target_temperature_low);

What shows on the display is nan; that is, the sensor is apparently not available. So I decided to make the conversion in the display. After some fumbling around, I got this to work

      it.printf(64, 4, id(font2), TextAlign::TOP_CENTER, " %.1f°", id(bedroom_temperature).state * (9.0/5.0) + 32.0);
      it.printf(0, 64, id(font3), TextAlign::BASELINE_LEFT, "   Set to: %.1f°", id(bedroom_thermostat).target_temperature_low * (9.0/5.0) + 32);

Thanks again.

1 Like