How to make more than one decision in lambda?

Hello,

I have a text sensor that can have the values Off, Dim., Full or Night. Those are representations of the active scene for a set of lights. At the moment I make the decision that if the value is Off I have a green box drawn and anything else draws a yellow box on the display. I would like to further refine that to have something like this:

Off = green box
Night = grey box
Dim. = yellow box
Full = red box

Here the current parts of the config that are relevant:

text_sensor:
  - platform: homeassistant
    id: sceneid
    entity_id: input_text.text2
    internal: true
    filters:
      - map:
        - "off -> Off"
        - "Dimmed -> Dim."
        - "Full -> Full"
        - "Night -> Night"
		
display:
  - platform: ili9341
    model: TFT 2.4
    cs_pin: GPIO2
    dc_pin: GPIO15
    reset_pin: GPIO4
    rotation: 180
    update_interval: 500ms
    auto_clear_enabled: true

    lambda: |-
      if (id(sceneid).state == "Off") {
        it.filled_rectangle(181, 297, 59, 23, id(green));
        it.print(210, 295, id(roboto), id(black), TextAlign::TOP_CENTER, id(sceneid).state.c_str());
      } else {
        it.filled_rectangle(181, 297, 59, 23, id(yellow));
        it.print(210, 295, id(roboto), id(black), TextAlign::TOP_CENTER, id(sceneid).state.c_str());
      }

How would my lambda have to look like to make that decision between 4 values rather than anything but Off.

Kind regards
Jan P.

You can use a combination of else if, something like this:

if (condition1) {
  // code block 1
} else if (condition2) {
  // code block 2
} else if (condition3) {
  // code block 3
} else {
  // code block 4
}
1 Like

Super, that works !! Thank you vey much @jsuanet

Kind Regards
Jan P.