Passing parameters to scripts

I’m battling to pass parameters to a script in esphome and would appreciate any assistance.

I have an LED configured with different pulse patterns:

    effects:
      - pulse:
          name: "1"
      - pulse:
          name: "2"
          transition_length:
            on_length: 450ms
            off_length: 450ms
          update_interval: 900ms
      - pulse:
          name: "3"
          transition_length:
            on_length: 300ms
            off_length: 300ms
          update_interval: 600ms
      - pulse:
          name: "4"
          transition_length:
            on_length: 120ms
            off_length: 120ms
          update_interval: 240ms
      - pulse:
          name: "5"
          transition_length:
            on_length: 75ms
            off_length: 75ms
          update_interval: 150ms

I want to pass the names (1, 2, 3, 4 or 5) to the script so that the LED pulses differently for different conditions.

The script:

script:
  - id: blink_led
    parameters:
      pulse_type: int
    then:
      - light.turn_on: 
          id: led_yellow
          effect: !lambda return pulse_type;

The call I am trying is:

            - script.execute:
                id: blink_led
                pulse_type: !lambda 'return (id(counter));'

counter is an integer global.

The error is:

/config/esphome/psu-hass.yaml: In lambda function:
/config/esphome/psu-hass.yaml:334:16: error: could not convert ‘pulse_type’ from ‘int’ to ‘std::string’ {aka ‘std::__cxx11::basic_string’}
334 |
| ^
| |
| int
*** [.pioenvs/psu-hass/src/main.cpp.o] Error 1

I suspect that the problem is the data type being passed but I can’t figure out the correct way to do it so any help would be greatly appreciated!

Try with

effect: !lambda |-
            return std::to_string(pulse_type);

Thanks, @Karosm

I realised that I was over complicating the issue and didn’t need a script at all because I was using a global.

I ended up using this:

            - light.turn_on:
                id: led_yellow
                brightness: 100%
                effect: !lambda |- 
                  if (id(counter) == 1) return "1";
                  else if (id(counter) == 2) return "2";
                  else if (id(counter) == 3) return "3";
                  else if (id(counter) == 4) return "4";
                  else if (id(counter) == 5) return "5";
                  return "None";

which works perfectly for each of the 6 states (0 = normal)

I’ll leave this here in the hope it helps someone else in the future - and will try your approach to improve my knowledge, too.

Much cleaner :+1: