Display multiple text_sensor HA entities

Hi everyone,

How would I go about displaying a second text_sensor homeassistant entity to display on an esp32 screen? I’ve managed to get one working!

I’m able to read in from HA with the below.

text_sensor:
  - platform: homeassistant
    entity_id: sensor.esp32_bluetooth_scanner
    name: "Bluetooth"
    id: bluetooth
    internal: true
  - platform: homeassistant
    entity_id: sensor.hotwater_mode
    name: "Hot Water"
    id: hot_water
    internal: true

and output one of these to the display with this

    lambda: |-
      it.printf(0, 5, id(roboto_20), id(ha_time).now().strftime("%Y-%m-%d %H:%M:%S").c_str());
      std::string val = to_string(id(bluetooth).state);
      it.printf(0, 20, id(roboto_20), "%s",id(bluetooth).state.c_str());

I thought this might work to display the second entity but it doesn’t compile. Where am I going wrong?

    lambda: |-
      it.printf(0, 5, id(roboto_20), id(ha_time).now().strftime("%Y-%m-%d %H:%M:%S").c_str());
      std::string val = to_string(id(bluetooth).state);
      it.printf(0, 20, id(roboto_20), "%s",id(bluetooth).state.c_str());
      std::string val = to_string(id(hot_water).state);
      it.printf(0, 40, id(roboto_20), "%s",id(hot_water).state.c_str());

Any assistance greatly appreciated.

You forgot to post the second text sensor code and the error you get when compiling…

Apologies Karosm,

here’s the compile error.

Compiling .pioenvs/heltec-display/src/main.cpp.o
/config/esphome/water-display.yaml: In lambda function:
/config/esphome/water-display.yaml:71:19: error: redeclaration of 'std::__cxx11::string val'
       std::string val = to_string(id(hot_water).state);
                   ^~~
/config/esphome/water-display.yaml:69:19: note: 'std::__cxx11::string val' previously declared here
       std::string val = to_string(id(bluetooth).state);
                   ^~~
*** [.pioenvs/heltec-display/src/main.cpp.o] Error 1
========================== [FAILED] Took 7.32 seconds ==========================

I thought I could ready two text_sensors from HA like this, do they need to be separated somehow?

text_sensor:
  - platform: homeassistant
    entity_id: sensor.esp32_bluetooth_scanner
    name: "Bluetooth"
    id: bluetooth
    internal: true
  - platform: homeassistant
    entity_id: sensor.hotwater_mode
    name: "Hot Water"
    id: hot_water
    internal: true

You are declaring the same variable twice.

I think you can do like this:

lambda: |-
      it.printf(0, 5, id(roboto_20), id(ha_time).now().strftime("%Y-%m-%d %H:%M:%S").c_str());
      std::string val = to_string(id(bluetooth).state);
      it.printf(0, 20, id(roboto_20), "%s",id(bluetooth).state.c_str());
      val = to_string(id(hot_water).state);
      it.printf(0, 40, id(roboto_20), "%s",id(hot_water).state.c_str());

or

lambda: |-
      it.printf(0, 5, id(roboto_20), id(ha_time).now().strftime("%Y-%m-%d %H:%M:%S").c_str());
      std::string val = to_string(id(bluetooth).state);
      it.printf(0, 20, id(roboto_20), "%s",id(bluetooth).state.c_str());
      std::string val2 = to_string(id(hot_water).state);
      it.printf(0, 40, id(roboto_20), "%s",id(hot_water).state.c_str());

But what is val supposed to do? You aren’t using it in the code.

My mistake, I didn’t notice you had the second sensor already there.
Like @Hellis81 pointed, your code makes not sense.
You should print val, since you declared it for your sensor.

std::string val = to_string(id(bluetooth).state);
it.printf(0, 20, id(roboto_20), "%s", val.c_str());

and same for val2.

Thank you for your help with this. There’s a lot to learn.

Always, for all of us… :wink: