[SOLVED] Need help displaying a variable from a sensor to an oled display

Hi,
I am trying to take a reading from a temp sensor and display it on an oled, I can read the sensor and display simple text on the display but when I try and display the variable I get errors, I’m new to this and I’m sure I’m missing somethinf simple.

Here is the code

esphome:
  name: humdisp
  friendly_name: Humdisp

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "z+iQbjCQbN2Ic+3H9okL+eTWqp6pCniUhyBa/7Ci64c="

ota:
  password: "5b0a9e23794cd0d26f7c06ba30d69369"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Humdisp Fallback Hotspot"
    password: "oW7nPzEYNUsU"

captive_portal:

sensor:
  - platform: dht
    pin: GPIO0
    temperature:
      id: "temp"
      name: "Temperature"
    humidity:
      id: "hum"
      name: "Humidity"
    # Update intervel in Seconds
    update_interval: 30s
    model: DHT11

font:
  - file: "arial.ttf"
    id: my_font
    size: 20

i2c:
  sda: GPIO4 
  scl: GPIO5 
  frequency: 800kHz

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    lambda: |-
      it.print(0, 0, id(my_font), "%.1f°", id(temp).state);

What are the errors ?

Do you need to use it.printf as it’s formatted text ?

Hi,

The errors look like this -

INFO ESPHome 2023.8.3
INFO Reading configuration /config/esphome/humdisp.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing humdisp (board: esp01_1m; framework: arduino; platform: platformio/[email protected])
--------------------------------------------------------------------------------
HARDWARE: ESP8266 80MHz, 80KB RAM, 1MB Flash
Dependency Graph
|-- ESPAsyncTCP-esphome @ 1.2.3
|-- ESPAsyncWebServer-esphome @ 2.1.0
|-- DNSServer @ 1.1.1
|-- ESP8266WiFi @ 1.0
|-- ESP8266mDNS @ 1.2
|-- noise-c @ 0.1.4
|-- Wire @ 1.0
Compiling /data/humdisp/.pioenvs/humdisp/src/main.cpp.o
/config/esphome/humdisp.yaml: In lambda function:
/config/esphome/humdisp.yaml:58:52: error: no matching function for call to 'esphome::display::Display::print(int, int, esphome::font::Font*&, const char [7], float&)'
   58 |       it.print(0, 0, id(my_font), "%.1f°", id(temp).state);
      |                                                   ^
In file included from src/esphome.h:18,
                 from src/main.cpp:3:
src/esphome/components/display/display.h:215:8: note: candidate: 'void esphome::display::Display::print(int, int, esphome::display::BaseFont*, esphome::Color, esphome::display::TextAlign, const char*)'
  215 |   void print(int x, int y, BaseFont *font, Color color, TextAlign align, const char *text);
      |        ^~~~~
src/esphome/components/display/display.h:215:8: note:   candidate expects 6 arguments, 5 provided
src/esphome/components/display/display.h:225:8: note: candidate: 'void esphome::display::Display::print(int, int, esphome::display::BaseFont*, esphome::Color, const char*)'
  225 |   void print(int x, int y, BaseFont *font, Color color, const char *text);
      |        ^~~~~
src/esphome/components/display/display.h:225:50: note:   no known conversion for argument 4 from 'const char [7]' to 'esphome::Color'
  225 |   void print(int x, int y, BaseFont *font, Color color, const char *text);
      |                                            ~~~~~~^~~~~
src/esphome/components/display/display.h:235:8: note: candidate: 'void esphome::display::Display::print(int, int, esphome::display::BaseFont*, esphome::display::TextAlign, const char*)'
  235 |   void print(int x, int y, BaseFont *font, TextAlign align, const char *text);
      |        ^~~~~
src/esphome/components/display/display.h:235:54: note:   no known conversion for argument 4 from 'const char [7]' to 'esphome::display::TextAlign'
  235 |   void print(int x, int y, BaseFont *font, TextAlign align, const char *text);
      |                                            ~~~~~~~~~~^~~~~
src/esphome/components/display/display.h:244:8: note: candidate: 'void esphome::display::Display::print(int, int, esphome::display::BaseFont*, const char*)'
  244 |   void print(int x, int y, BaseFont *font, const char *text);
      |        ^~~~~
src/esphome/components/display/display.h:244:8: note:   candidate expects 4 arguments, 5 provided
*** [/data/humdisp/.pioenvs/humdisp/src/main.cpp.o] Error 1
========================== [FAILED] Took 2.16 seconds ==========================

I have no idea if I should be using it.printf or something else, I’ve just been copying what others have done and trying to match it to what I want

yes, replace it.print with it.printf, if you use anything other than static text

That was the problem !!

Thank you so much

No worries, can you mark this as solved ?

1 Like

Holy cow, came across this because I had my config working at one time perfectly but for some reason, I couldn’t get sensor data to populate any more on my display. I had to re do my config on the same device. After reading this I totally forgot to add the device via esphome integration lol. So thank you guys for posting this.