How to use Lambda to update Dropdown

I’m fairly new at YAML and LVGL but getting on pretty well… but stuck on this issue.
I have a text sensor that should drive a change to the selected value of a dropdown. I’ve tried a number of variations, but I’m getting similar messages with all of them. I’m clearly missing something…

   on_value:
      - lvgl.label.update:
          id: lbl_hvacshop_mode
          text: !lambda return id(hvacshop_mode).state;
      - lvgl.dropdown.update:
          id: dropdown_hvacshop_mode
          selected_index: 
            - lambda: 'return(id(hvacshop_action).state == "heat" ? "2" : (id(hvacshop_action).state == "cool" ? "3" : "0"));'

The label update works fine, but I can’t find a variation of how to get the dropdown update to compile.

   - lvgl.dropdown.update: 
        id: dropdown_hvacshop_mode
        
        Must be string, got <class 'esphome.helpers.EList'>. did you forget putting quotes around the value?.
        selected_index: 
          - lambda: |-
              return(id(hvacshop_action).state == "heat" ? "2" : (id(hvacshop_action).state == "cool" ? "3" : "0"));

Curious on this too, as I am running into the same issues.

Afaik this is not (yet) possible

Docs tell just that it’s int8, no mention if it’s templateable.
Maybe you could try with lvgl select?

You’re returning a string when it expects an integer.

Templates are fine, I use them in my current project.

  - then:
    - lvgl.dropdown.update:
        id:
        - id: wake_end_time
        selected_index: !lambda |-
          return id(presence_end_index);
    - lvgl.dropdown.update:
        id:
        - id: wake_start_time
        selected_index: !lambda |-
          return id(presence_start_index);

I added the quotes to try to see if that would get rid of the error. It didn’t make any difference. I’ll remove them before I continue troubleshooting.

Try !lambda: on the selected index line, not - lambda:

that was actually my starting point. Here’s what it gives, maybe there’s another syntax issue I’m missing:

      - lvgl.dropdown.update:
          id: dropdown_hvacshop_mode
          selected_index: !lambda return(id(hvacshop_mode).state == "heat" ? 2 : (id(hvacshop_mode).state == "cool" ? 3 : 0));
INFO ESPHome 2025.6.1
INFO Reading configuration shoptouchpad.yaml...
ERROR Error while reading config: Invalid YAML syntax:

mapping values are not allowed here
  in "shoptouchpad.yaml", line 505, column 80

Line 505 is the line with the lambda, column 80 is just after the firsst “?”.
I’ve also simplified the expression by dropping the second condition with the same result (i.e., return either 2 or 0).

I suggest rewriting your lambda -

      - lvgl.dropdown.update:
          id: dropdown_hvacshop_mode
          selected_index: !lambda |-
            if (id(hvacshop_mode).state == "heat" ) return 2;
            if (id(hvacshop_mode).state == "cool") return 3;
            return 0;

Yes, if I can get lambda to work at all, I’ll go there. There are actually 5 cases, so the ternaray operator wasn’t going to hold up. Currently exploring doing the cases in YAML.

You are trying to update the selected item in the dropdown right?

I was hoping to find a way to populate the lvgl dropdown from an array (input_select or a textsensor with comma separates values for example)

Correct. My dropdown list is static, but the selected item change change external to the ESP, so when the text sensor changes, I need to update the dropdown selected item to keep it in sync.

If the text sensor values map one-to-one with the dropdown choices, just update selected_text with the text sensor value. If not, use the mapping component to create a map from the text sensor values either to indices or strings to update the dropdown.

Lambdas in YAML are strings, and often need enclosing in quotes, or embedded as a multi-line string using |- - strange errors can result otherwise. See YAML Configuration in ESPHome - ESPHome - Smart Home Made Simple

This was my final solution (showing 2 of 5 cases).

     - if:
          condition:
            lambda: 'return(id(hvacshop_mode).state == "off");'
          then:
            - lvgl.dropdown.update:
                id: dropdown_hvac_mode
                selected_index: 0
      - if:
          condition:
            lambda: 'return(id(hvacshop_mode).state == "fan_only");'
          then:
            - lvgl.dropdown.update:
                id: dropdown_hvac_mode
                selected_index: 1