How to printf device name?

I can’t for the life of me figure out the syntax, how do you printf the device name? I feel like there’s a simple answer that I’ve missed somehow. Here’s my code - what do you put in there to output the esphome device name?

      // Print device name in top center.
      it.printf(64, 0, id(font1), TextAlign::TOP_CENTER, "%s", WHATGOESHERE);

Maybe this will help

it.printf(64, 0, id(font1), TextAlign::TOP_CENTER, "Device name: %s", name.c_str());

1 Like

Or maybe not… :frowning:

Compiling /data/seeed-xiao-esp32c3/.pioenvs/seeed-xiao-esp32c3/src/main.cpp.o
/config/esphome/seeed-xiao-esp32c3.yaml: In lambda function:
/config/esphome/seeed-xiao-esp32c3.yaml:138:73: error: 'name' was not declared in this scope
       it.printf(64, 0, id(font1), TextAlign::TOP_CENTER, "Device name: %s", name.c_str());

Anyone know the proper syntax to print the esphome device name? I can’t seem to find any references in the docs…

Do not listen to uncapped_shady. He’s posting information from what seems to be a bot, or he is a bot. :man_shrugging: Either way, the information he’s posting is 100% made up.

Thanks @petro - it definitely seemed a bit…shady

So your question, you just want the device name via a template in home assistant or do you want the device name inside ESPHome?

EDIT: Just read your initial post, sorry, I don’t think I can help.

EDIT2: maybe I can, hold on.

Use App.get_name().c_str() for WHATGOESHERE.

Isn’t the device name not set as substitutions by luck?

  devicename: myesponsteroids
  upper_devicename: My ESP on Steroids

  it.printf(64, 0, id(font1), TextAlign::TOP_CENTER, "Device name: ${upper_devicename}", name.c_str());

maybe :thinking:

@orange-assistant makes a very good point here. App.get_name().c_str() will give you the hostname, limited to lowercase, numbers, hyphen & underscore. This might not be quite what you are looking for. For the substitution route, just use print():

it.print(64, 0, id(font1), TextAlign::TOP_CENTER, "Device name: ${upper_devicename}" );

This worked perfectly, thanks!

1 Like

Yes, I should have been more clear, what I wanted was the hostname. I have some devices with little OLED screens attached and thought it would be nice to display a small status page with the network info.