How do I print (lambda) out a sensor state

I am setting up a small OLED screen, and I want to show current time, and some weather conditions like current weather, wind force and temperature. I get all to show correctly, except for the string from sensor.yr_symbol_current

I was hoping printf with the parameter %s would work, but unfortunately not.
How can I print the state (as text))

My first sensor below, id: current is based on sensor.yr_symbol_current where {{ states.sensor.yr_symbol_current.state }} returns Regn (rain) in the template builder.

What am I missing here?

sensor:
  - platform: homeassistant
    entity_id: sensor.yr_symbol_current
    name: "Akkurat nå"
    id: current
    internal: true
  - platform: homeassistant
    entity_id: sensor.yrcurrent_wind_speed
    name: "Vind"
    id: wind
    internal: true
  - platform: homeassistant
    entity_id: sensor.pir_ute_paviljong_temperature
    name: "utendørs"
    id: temp
    internal: true
  
display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    reset_pin: D0
    address: 0x3C
    lambda: |-
      it.strftime(20, 0, id(font1), "%H:%M", id(homeassistant_time).now());
      it.printf(0, 58, id(font3), "%s", id(current).state);
      it.printf(50, 58, id(font3), "%.1f ms", id(wind).state);
      it.printf(114, 58, id(font3), TextAlign::CENTER, "%.1f°C", id(temp).state);

If the template says “regn” then you probably need to use a text sensor.

-J

Good idea, @JayElDubya, but I can’t figure out how the lambda string would be, then…

lambda: |-
  it.printf(0, 58, id(font3),,, id(current).state);

According to some documentation I found it looks like the correct synth string would be

printf(int x, int y, Font *font, Color color, TextAlign align, const char *format, ...)

Then I get these errors while uploading:

Compiling /data/info_bad_vask/.pioenvs/info_bad_vask/src/main.cpp.o
src/main.cpp: In lambda function:
src/main.cpp:552:30: error: expected primary-expression before ',' token
       it.printf(0, 58, font3,,, current->state);
                              ^
src/main.cpp:552:31: error: expected primary-expression before ',' token
       it.printf(0, 58, font3,,, current->state);
                               ^
*** [/data/info_bad_vask/.pioenvs/info_bad_vask/src/main.cpp.o] Error 1

See if adding .c_str() helps:

it.printf( 0, 58, id(font3), "%s", id(current).state.c_str());

-J

5 Likes

That truly helped. :grinning:
Text is displaying fine in the OLED
Still got this, though:

Compiling /data/info_bad_vask/.pioenvs/info_bad_vask/src/main.cpp.o
src/main.cpp:299:3: warning: multi-line comment [-Wcomment]
   //   lambda: !lambda "it.strftime(20, 0, id(font1), \"%H:%M\", id(homeassistant_time).now());\n\
   ^

And I got this: some seconds later

[20:34:50][W][homeassistant.sensor:014]: Can't convert 'Regn' to number!

Here is my entire lambda section

    lambda: |-
      it.strftime(20, 0, id(font1), "%H:%M", id(homeassistant_time).now());
      it.printf( 0, 58, id(font3), "%s", id(current).state.c_str());
      it.printf(50, 58, id(font3), "%.1f ms", id(wind).state);
      it.printf(114, 58, id(font3), TextAlign::CENTER, "%.1f°C", id(temp).state); 

are you using it as a text sensor?

1 Like

Yes.

text_sensor:
  - platform: homeassistant
    entity_id: sensor.yr_symbol_current
    name: "Akkurat nå"
    id: current
    internal: true

Is it still correct to use printf as opposed to print ?

THANK YOU. I was very glad to find this.
Adding adding .c_str() helped me output text to an oled display from data stored in a global with type: std::string using %s as above.

I’m still looking for a way to assign initial_value: much easier than like this:
initial_value: “{‘w’,‘i’,‘f’,‘i’,’ ',‘c’,‘o’,‘n’,‘n’}”
and also for how to use globals.set: so assign a new value: on types std::string and char[]