esphome / esphome/feature-requests

Add actions per option in `select` component

Open
#3,151 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
450
Forks
29
PR merge metrics
No merged PRs in 30d

Description

**Describe the problem you have/What new integration you would like**

I'd love to see support for an `on_selected` action in the `select` component. The idea is to let users define what should happen when each specific option is chosen, directly in YAML, without needing to use a lambda or write a switch-case. I initially tried to figure out a lambda under `set_action` and spent way too much time trying to solve data type mismatches.

**Please describe your use case for this integration and alternatives you've tried:**

I'm using a `select.template` in my project to control a water heater with several modes (Off, Half - L1, Half - L2, Full). Right now, the only way to trigger different actions depending on the selected option is to use `on_value` or `set_action` and write a lambda that checks the selected index or value and performs the corresponding actions. It works under `on_value`, but it took a lot more time than I like to do the research and testing for figuring out the lambda. Here's my current working example
```yaml
select:
- platform: template
name: Mode
id: mode
options:
- "Off"
- "Half - L1"
- "Half - L2"
- "Full"
initial_option: "Off"
optimistic: True
on_value:
then:
- lambda: |-

switch(i){
case 0:
id(relay1).turn_off();
id(relay2).turn_off();
break;
case 1:
id(relay1).turn_on();
id(relay2).turn_off();
break;
case 2:
id(relay1).turn_off();
id(relay2).turn_on();
break;
case 3:
id(relay1).turn_on();
id(relay2).turn_on();
break;
default:
id(relay1).turn_off();
id(relay2).turn_off();
}
```

It would be so much nicer if I could just define the actions for each option directly in YAML, something like:
```yaml
select:
- platform: template
name: Mode
id: mode
options:
- "Off"
- "Half"
- "Full"
on_selected:
"Off":
- switch.turn_off: relay1
- switch.turn_off: relay2
"Half":
- switch.turn_on: relay1
- switch.turn_off: relay2
"Full":
- switch.turn_on: relay1
- switch.turn_on: relay2
```
Alternately
```yaml
select:
- platform: template
...
options:
- "Off":
on_selected:
- switch.turn_off: relay1
- switch.turn_off: relay2
...
```
This structure is easier to understand, requires no lambda code, and aligns with other `on_*` automation patterns in ESPHome.

**Additional context**

This feature would make configuration files more declarative and accessible, especially to users who are less comfortable with C++ lambdas.

Similar patterns already exist in other ESPHome components like `binary_sensor` and `button`, where `on_press`, `on_click`, etc., can be used declaratively.

It would improve readability and maintainability for configurations that use multiple selectable modes or behaviors.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.