Esphome and xiaomi LYWSD03MMC sensor and lambda values

Hi team…

I have a display connected to my ESP32. I want to display current temperature/humidity values on that display… while the ESP pushes data to MQTT/HomeAssistant etc.

My sensor is defined as :slight_smile: sensor:

  - platform: atc_mithermometer
    id: sensor_kitchen
    mac_address: "A4:C1:38:13:30:17"
    temperature:
      name: "xTemperature Kitchen"
      id: kitchen_temp
    humidity:
      name: "xHumidity Kitchen"
    battery_level:
      name: "xBattery Kitchen"
    battery_voltage:
      name: "xBattery Voltage Kitchen"

The display has coded as below

display:
  - platform: ssd1306_i2c
    model: "SSD1306 64x48"
    reset_pin: 26  #GPIO26 #D0
    address: 0x3C
    lambda: |-
      it.print(0, 0, id(my_font), id(my_light_green), "DGW ESP32 1");
      it.strftime(0, 10, id(my_font), "%d--%H:%M:%S", id(sntp_time).now());
      it.print(0, 20, id(my_font), "Kitchen:  %.2f", id(sensor_kitchen).state);

And the compile fails with:
Compiling .pioenvs/sensoresp32/src/main.cpp.o
src/main.cpp: In lambda function:
src/main.cpp:544:66: error: ‘class esphome::atc_mithermometer::ATCMiThermometer’ has no member named ‘state’
it.print(0, 20, my_font, “Kitchen: %.2f”, sensor_kitchen->state);
^
*** [.pioenvs/sensoresp32/src/main.cpp.o] Error 1

Can I do what I want to do?
If not, is there a work around?

  • David

I suspect you want kitchen_temp not sensor_kitchen in your print lambda.

Sorry… my bad… too many code changes… that doesn’t work. More spectacular compiler error though!

      it.print(0, 20, id(my_font), "Kitchen:  %.2f", id(kitchen_temp).state);
Compiling .pioenvs/sensoresp32/src/main.cpp.o
src/main.cpp: In lambda function:
src/main.cpp:545:69: error: no matching function for call to 'esphome::display::DisplayBuffer::print(int, int, esphome::display::Font*&, const char [15], float&)'
       it.print(0, 20, my_font, "Kitchen:  %.2f", kitchen_temp->state);
                                                                     ^
In file included from src/esphome.h:14:0,
                 from src/main.cpp:3:
src/esphome/components/display/display_buffer.h:139:8: note: candidate: void esphome::display::DisplayBuffer::print(int, int, esphome::display::Font*, esphome::Color, esphome::display::TextAlign, const char*)
   void print(int x, int y, Font *font, Color color, TextAlign align, const char *text);
        ^

Hi

I think you need to use printf and not print in the lambda. If you are printing values.

/Mattias

Interesting. Do you know of any code examples or documentation that shows how to do this?

It in the esphome dokumentation.

In the section
Formatted Text

There you have an example and documentation.

From the documentation


display:
  - platform: ...
    # ...
    lambda: |-
      it.printf(0, 0, id(my_font), "The sensor value is: %.1f", id(my_sensor).state);
      // If the sensor has the value 30.02, the result will be: "The sensor value is: 30.0"

Basically it seems that you only missing a the f in print statement.

/Mattias

Right. Understood. Will try when I get home from work.

That’s awesome and I can confirm it’s working. Thank you so kindly for your help.

Great that it works.
/Mattias