Lambda printf help

I am trying to print the value of a sensor - temperature on an oled display. I am sure my syntax is off but I cannot decipher what my issue is and how to fix it. Am I totally off here? Sorry, first time giving this a try.

error-------
Compiling /data/bt-monitor-display/.pioenvs/bt-monitor-display/src/main.cpp.o
/config/esphome/bt-monitor-display.yaml: In lambda function:
/config/esphome/bt-monitor-display.yaml:58:51: warning: format ‘%d’ expects argument of type ‘int’, but argument 6 has type ‘esphome::sensor::Sensor*’ [-Wformat=]
it.printf(5, 20, id(myfont), “Temp: %d”, id(btwater));

code----

lambda: |-
it.printf(5, 20, id(myfont), “Temp: %d”, id(bttemp));
it.strftime(5, 0, id(font10), “%a %m/%d %I:%M”, id(currtime).now());
- id: page2
lambda: it.fill(COLOR_OFF);
esp32_ble_tracker:
xiaomi_ble:
sensor:

  • platform: xiaomi_hhccjcy01
    mac_address: C4:7C:8D:6D:49:33
    temperature:
    id: bttemp
    name: “BT Temperature”

I think you shoud add .state

it.printf(5, 20, id(myfont), “Temp: %d”, id(bttemp).state);

Please add your code inside a </> block the way I did, so the formatting of your code is preserved.

Also, the error speaks of btwater, not bttemp, did you change anything inbetween?

oh sorry about that. i was trying different variables and forgot i copied log before i changed it. tried what you suggested, and result is below. (I actually started with this from the example I found)

  it.printf(5, 25, id(myfont), "Temp: %d", &id(bttemp).state);

/config/esphome/bt-monitor-display.yaml: In lambda function:
/config/esphome/bt-monitor-display.yaml:58:58: warning: format ‘%d’ expects argument of type ‘int’, but argument 6 has type ‘float*’ [-Wformat=]
it.printf(5, 25, id(myfont), “Temp: %d”, &id(bttemp).state);

Then %d should be %f. I suggest you use %.1f to get one decimal place.

  it.printf(5, 20, id(myfont), “Temp: %.1f”, id(bttemp).state);
1 Like

looks like that did it. no errors. thanks for your help!

one more question. this sensor sends in Celcius. in my dashboard it is showing in Fahrenheit but now i am unsure why as i did no conversion. if i wanted to format this to F would it be best to format it in the display printf command? not sure how i’d do that - just wondering what the best way to do that would be.

The Home assistant UI has temperature conversion built in, and probably picks what’s normal for your timezone by default. EspHome is not so sophisticated. I did not check the below code, but it could look something like this. If you want to display the °F as well:

  float temp_f = id(bttemp).state * 9 / 5 + 32;
  it.printf(5, 20, id(myfont), “Temp: %.1f°F”, temp_f);
1 Like

Thanks once again. I tried it and are getting some errors. I tried a few things but nothing worked - moving declaration to globals: and a few other tries.

      - id: page1
        lambda: |-
         float temp_f = id(bttemp).state * 9 / 5 + 32;
         it.printf(5, 0, id(myfont), “Temp: %.1f°F”, temp_f);
         it.printf(5, 15, id(myfont), "Moisture: %.1f", id(btwater).state);
         it.printf(5, 30, id(myfont), "Light lx: %.1f", id(btlight).state);
         it.printf(5, 45, id(myfont), "EC: %.1f", id(btec).state);
#        it.strftime(5, 0, id(font10), "%a %m/%d %I:%M", id(currtime).now());
      - id: page2
        lambda: it.fill(COLOR_OFF);
Compiling /data/bt-monitor-display/.pioenvs/bt-monitor-display/src/main.cpp.o
/config/esphome/bt-monitor-display.yaml:60:7: error: stray '\342' in program
          it.printf(5, 0, id(myfont), “Temp: %.1f°F”, temp_f);
       ^
/config/esphome/bt-monitor-display.yaml:60:7: error: stray '\200' in program
/config/esphome/bt-monitor-display.yaml:60:7: error: stray '\234' in program
/config/esphome/bt-monitor-display.yaml:60:7: error: stray '\302' in program
/config/esphome/bt-monitor-display.yaml:60:7: error: stray '\260' in program
/config/esphome/bt-monitor-display.yaml:60:7: error: stray '\342' in program
/config/esphome/bt-monitor-display.yaml:60:7: error: stray '\200' in program
/config/esphome/bt-monitor-display.yaml:60:7: error: stray '\235' in program
/config/esphome/bt-monitor-display.yaml: In lambda function:
/config/esphome/bt-monitor-display.yaml:60:34: error: 'Temp' was not declared in this scope
          it.printf(5, 0, id(myfont), “Temp: %.1f°F”, temp_f);
                                  ^
*** [/data/bt-monitor-display/.pioenvs/bt-monitor-display/src/main.cpp.o] Error 1
========================== [FAILED] Took 1.92 seconds ==========================

I found another example by chance and it worked. A different take on it but it worked.

sensor:
- platform: xiaomi_hhccjcy01
  mac_address: C4:7C:8D:6D:49:33
  temperature:
    filters:
    **- lambda: return x * (9.0/5.0) + 32.0; # report temperature in Fahrenheit**
    name: "BT Temperature"
    id: bttemp

my only issue now is while oled is correct, my dashboard says it is 164 degrees lol.

Seems like in my example some (invisible?) weird characters got copied along with the code. I guess some cut and paste problem. But glad you found something that worked.

As for it showing up wrong in HA now: you multiplied the sensor value, so you should also set the proper unit_of_measurement for your sensor, so HA knows it is °F now.

Ah, I think I see what happened: the normal double quote got converted to smart quotes (look nice, but make no sense in code). iPad keyboard does that by default. Seems like the IOS16 turned that nasty feature back on while I had it turned off.

ah now i see! i stared and compared for 10 minutes but my eyes just did not catch that.
and thanks for the last tip - found how to set it.

appreciate it!