Passing argument to esphome script

Hello, first time poster here :slightly_smiling_face:

I’m working on ESP32 with a bunch of relays and switches - final version should consist of about 20 of each. However for test purposes there are only two relays and four switches connected.

The same piece of code is being applied to each relay/button. It works perfectly by copying code over and over again, but that’s not what I want. My goal is (in pseudocode) something like that:

function do_something(component){
 turn_off(component)  
 delay
 log_event
}

do_something(relay1)
do_something(relay2)

Now for the real part I’m using scripts in esphome, mostly because I rely on restart mode. The code (partial) is:

switch:
  - platform: gpio
    pin: 16
    inverted: True
    name: "Main relay 1"
    id: mn_relay_1

  - platform: gpio
    pin: 14
    name: "Switching relay 1"
    id: sw_relay_1

binary_sensor:
  - platform: gpio
    pin:
      number: 18   
      mode:
        input: true
        pullup: true
    filters:
        delayed_on: 10 ms
    name: "Button up"
    on_press:
        then:
          if:
              condition:
                switch.is_on: mn_relay_1
              then:
                script.execute: 
                  id: stop_it
                  id_mn_rel: mn_relay_1
                  id_sw_rel: sw_relay_1
              else:
                script.execute: 
                  id: lift_it
                  id_mn_rel: mn_relay_1
                  id_sw_rel: sw_relay_1
                 
script:
  - id: stop_it
    parameters:
      id_mn_rel: string
      id_sw_rel: string
    mode: restart
    then:
      - switch.turn_off: id_mn_rel
      - delay: 10ms
      - switch.turn_off: id_sw_rel

  - id: lift_it
    mode: restart
    parameters:
      id_mn_rel: string
      id_sw_rel: string
    then:
      - switch.turn_off: id_sw_rel;
      - delay: 10ms
      - switch.turn_on: id_mn_rel
      - delay: 5s
      - switch.turn_off: id_mn_rel
      - delay: 10ms
      - switch.turn_off: id_sw_rel

And it fails with “Couldn’t find id_mn_rel”. I’ve tried to replace line with

- switch.turn_off !lambda return id_mn_rel;

but no success - it fails with “This option is not templatable!.”

I would be really grateful for any advice as I’m sure that there is some simple trick which I can’t work out.

Take a look at this post. Looks like the script will need to be in the included template file, with unique id used for each id: stop_it_${id_mn_rel}

Thank you, that worked :slightly_smiling_face:. The code is fragmented into multiple files, but much cleaner as original version. My implementation is such:

switch:
  - platform: gpio
    pin: ${gl_pin}
    inverted: True
    name: "Main relay ${num}"
    id: mn_rel_${num}

  - platform: gpio
    pin: ${iz_pin}
    #inverted: true
    name: "Switching relay ${num}"
    id: sw_rel_${num}

binary_sensor:
  - platform: gpio
    pin:
      number: ${up_pin}    
      mode:
        input: true
        pullup: true
    filters:
        delayed_on: 10 ms
    name: "Button up ${num}"
    on_press:
        then:
          if:
              condition:
                switch.is_on: mn_rel_${num}
              then:
                script.execute: stop_it_${num}
              else:
                script.execute: lift_it_${num}

script:
  - id: stop_it_${num}
    mode: restart
    then:
      - switch.turn_off: mn_rel_${num}
      - delay: 10ms
      - switch.turn_off: sw_rel_${num}
1 Like