Input_select as a way to choose display page

HI all!
I have input_select in my Home Assistant

oled_page:
  name: OLED page
  options:
    - 'page1'
    - 'page2'
    - 'page3'
    - 'page4'

I want to change display pages with it. Thats why i write some code in ESPHome:

text_sensor:
  - platform: homeassistant
    id: pages
    entity_id: input_select.oled_page
    on_value:
      then:
        - display.page.show: !lambda |-
            if (id(pages).state == "page1") {
              return page1;
            }
            if (id(pages).state == "page2") {
              return page2;
            }
            if (id(pages).state == "page3") {
              return page3;
            }
            if (id(pages).state == "page4") {
              return page4;
            }

It works, but it looks really bad. Is there a way to transfer the input_select state using return id(pages) without using if - else?
P.S. sorry for my English, it is not my native language

2 Likes

What looks bad? The code, or the UI generated.

If you are talking about the code, don’t sweat it. If it works, do not change it.

And if there are 20, 30, 40 pages? Then write 3 lines of code for each? It doesn’t seem logical to me. Is there really no mechanism to transfer the state of the sensor directly?

Hey, I am still new to esphome so this might not work but can’t you just do this instead?

text_sensor:
  - platform: homeassistant
    id: pages
    entity_id: input_select.oled_page
    on_value:
      then:
        - display.page.show: !lambda 'return id(pages).state;'

Unfortunately it’s not working

/config/esphome/wemos_d1_debug.yaml:253:21: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'esphome::display::DisplayPage*' in return
         - display.page.show: !lambda 'return id(pages).state;'

I want to do something very similar (define the to-be-shown display page through MQTT) and have the same problem. The error message says that it received a string (“page1”), but expected the type DisplayPage. Now if there were a way to convert (“cast”) a string into the DisplayPage type, then the problem would be easily solvable. I didn’t find anything yet, though.

Hey,
I am trying to do exactly what you did here (using Input_select to change the pages on my OLED screen).
I copy pasted your code to my ESPhome but it keeps failing. not sure why.
can you past your exact code you’ve used for your ESPhome here?
Thanks

Never mind, I used a slightly different code and got it working

Hi
would be interessted in the working code here?
thanks