Can't use "friendly_name" in a printf command?

I’m using a D1 mini with an OLED display as part of an air quality sensor, I’ll deploy several of these sensors and I want the first line on the display to be the device name (friendly_name), however I’m completely stumped on how to pass the friendly_name value to a printf command.
This is my sample code:

esphome:
  name: air-monitor-2
  friendly_name: Air Monitor 2

  
esp8266:
  board: d1_mini

i2c:
  sda: D2
  scl: D1
  frequency: 50kHz

font:
  - file: 
      type: gfonts
      family: Roboto Mono
      weight: 400
    id: my_font1
    size: 12

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    id: oled
    address: 0x3c
    update_interval: 10s
    pages:
      - id: page1
        lambda: |-
          it.printf(0, 0, id(my_font1),"%s", friendly_name.state.c_str());

This gives the error

error: 'friendly_name' was not declared in this scope

Is there any way this can be done?

I think you need to have a text sensor that has the contents of friendly_name, add an id and then print the state of the id. Something like

text_sensor:
  - platform: template
    name: "Template Text Sensor"
    lambda: |-
      return SOMETHING THAT RETRNS FRIENDLY NAME;
   id: fn

Then your print will be something like

it.printf(0, 0, id(my_font1),"%s", id(fn).state.c_str());
1 Like

There might be something in this thread you can adjust or use a similar approach.

Maybe using this.

https://esphome.io/api/classesphome_1_1_application#a3f6581ed5daade98290f589410be1cc4

1 Like