Use lambda template in switch actions to define the switch to control

Hi everyone,

I have some code that starts to get a bit long and I was wondering if there is a way to use lambda templates to define the switch to manipulate in an automation.

Let me explain, currently I have the following code:

  - platform: gpio
    name: valve_toggle_button
    pin:
      number: 17                                                                                                                                                                                                          
      mode: INPUT_PULLUP
    on_press:
      then:
        lambda: |-
          auto valve = id(valve_select).state;
          if(valve == "valve1"){
            id(valve_1_switch).toggle();
          }
          else if(valve == "valve2"){
            id(valve_2_switch).toggle();
          }

where “valve_select” is a select component with options: “valve1” and “valve2”

As I am planning to add more valves I was trying to do:

  - platform: gpio
    name: valve_toggle_button
    pin:
      number: 17                                                                                                                                                                                                          
      mode: INPUT_PULLUP
    on_press:
      then:
        - switch.toggle: !lambda return id(valve_select).state;

But I get the error:

This option is not templatable!

My question is: Is there a way to accomplish what I am trying to do? Perhaps not with lambdas, but in another way? Or with lambdas but in a different fashion?

Thanks for your help!