Text sensor question

Hi, all.

I have a text sensor:

text_sensor:
  - platform: homeassistant
    id: current_conditions
    entity_id: sensor.home_condition_today
    internal: true

This is just an accuweather sensor, giving the current conditions.

If I try to use this text sensor like this, it crashes my device:

it.printf(0, 150, id(font_roboto_medium22), id(my_red), TextAlign::TOP_LEFT, "Conditions: %s", id(current_conditions).state);

But if I use it like this, it works fine:

std::string conditions = id(current_conditions).state;
it.printf(0, 150, id(font_roboto_medium22), id(my_red), TextAlign::TOP_LEFT, "Conditions: %s", conditions.c_str());

Why does the one work and the other fail?

Thanks.

The printf statement requires a null terminated string. You can apply the c_str() function directly to the state:

it.printf(0, 150, id(font_roboto_medium22), id(my_red), TextAlign::TOP_LEFT, "Conditions: %s", id(current_conditions).state.c_str());
1 Like

Thanks, that makes sense.