Weather without a lot of "if's"

As many others, I would like to show some weather info on a display. Besides some textual info (temperature, humidity, etc…) I would like to use sime weather icons for the forecast.
I have studied several project codes from other people. but I only saw pretty long code snippets with a lot of "if's...then...else if".

I have an entity which gives me the forecast in text, for example “Sunny”, “Cloudy”, etc.
I have images and gave them the same id, like “Sunny”, “Cloudy”, etc.

In the display >> page code, I have for example the lines:

....

- file: Images/Weer/Sunny.png
    id: Sunny
    resize: 100x100


  - file: Images/Weer/Cloudy.png
    id: Cloudy
    resize: 100x100
    type: RGBA

....

text_sensor:
  - platform: homeassistant
    id: weather_icon_now
    entity_id: weather.thuis

....

display:
  - platform: ili9xxx
    model: ili9488
    id: my_display
    rotation: 0
    dc_pin: 33
    cs_pin: 34
    reset_pin: 13
    invert_colors: false
    pages:
      - id: page1
        lambda: |-
          if (id(weather_icon_now)  =  "Sunny") {
                  it.image(2, 185, id(Sunny));
                  }
          else if (id(weather_icon_now)  =  "Cloudy") {
                  it.image(2, 185, id(Cloudy));
                  }
          else if .........

But is it possible to put all those “if’s…then” in one line:

it.image(2, 185, id(*SOME PLACEHOLDER*));

Or is it possible to use some function within an external header.h file? And put some line like:

it.image(2, 185, id(*FUNCTION()*));

Can someone help me or give me some push in the right direction? Maybe with some example snippets or something?

No, because the image name is hardcoded at compile time. If you work out a way to do it a lot of people will thank you.

I did suggest on another thread using an animated gif containing each of the images as a frame then using animation.set_frame to display the correct image. This still requires some lookup to select the image number…

Maybe there is a way by using the internal function calls of ESPhome. The only issue doing that is that the code may change, and you need a way to store the images. You can code the bmp in C++ I guess. Maybe… :thinking:

Okay, tnx for your reply!

And is it also not possible to put the whole “if…then” code in an external header.h file and return the needed

it.image(2, 185, id(Sunny));

In that way, if you have several weather images on your display, you only have to write the whole “if…then” code in the header file and let the header file generate the write it-line with the right image name and x- and y-coordinates…

Well - you can write your own functions. But whether they would work the way you expected in the display: block is another thing.

You can only try…

I can give it a try!
I am not a professional programmer and only made several codes in C#, VB and a few other languages, but I am not a die hard in all those, so it is a lot of copying and trying!

Btw (and offtopic): I have build the display in a housing, but the touchscreen keeps working laggy and buggy. I now have added two optical sensors and use them as a button to go to the previous or next page and that works great!

Yes the ESPHome team broke the touchscreen component a while back - it’s never been the same since.

I use a PIR on my multi-sensor to turn on the display, then cycle the pages. I left the touchscreen code in but as you said - it’s buggy…