How to use a parameter inside a id() function iside a lambda inside a script

Hi all,

I have a script like this:

  • id: update_battery_icons
    parameters:
    param_name: string
    value: float
    then:
    - lambda: |-
    if (isnan(value)){
    id(param_name) = “󰁾”;
    }
    else if (value < 10){
    id(param_name) = “󰀘”;
    }

but the compiler does not like this as “param_name” is not an id.

Couldn’t find ID ‘param_name’. Please check you have defined an ID with that name in your configuration.

Any idea how to fix this?

Please use code tags to post your yaml, otherwise it’s not readable.
Also, describe what are you trying to accomplish.

The fine manual shows the correct usage:

if (isnan(value)){
  param_name = "...";
}
else if (value < 10){
  param_name = "...";
}

You are right. I did it from my phone, but now I am on my laptop I see the code button.

Let me try again:

I have a long lambda that I am using on multiple places.
Right now I have the same code copied, but I would like to define it once as a script and reuse it.

The lambda basically updates a global variable.

- lambda: |-
          if (isnan(x)){
            id(solarman_battery_iconstring) = "󰤮"; 
          }
          else if (x < 10){
              id(solarman_battery_iconstring) = "󰁺";
          }
          else if (x < 20){
              id(solarman_battery_iconstring) = "󰁻";
          }
          else if (x < 30){
              id(solarman_battery_iconstring) = "󰁼";
          }
          else if (x < 40){
              id(solarman_battery_iconstring) = "󰁽";
          }
          else if (x < 50){
              id(solarman_battery_iconstring) = "󰁾";
          }
          else if (x < 60){
              id(solarman_battery_iconstring) = "󰁿";
          }
          else if (x< 70){
              id(solarman_battery_iconstring) = "󰂀";
          }
          else if (x < 80){
              id(solarman_battery_iconstring) = "󰂁";
          }
          else if (x < 90){
              id(solarman_battery_iconstring) = "󰂂";
          }
          else if (x < 100){
              id(solarman_battery_iconstring) = "󰁹";
          }
          else{
              id(solarman_battery_iconstring) = "󰁹";
          } 

I want to supply the name of the global parameter to be updated as a variable to the script. But that gives me the error as displayed

I’m still not following.
Global variable is clear to me, but what is global parameter and variable here?

I have globals like this:

globals:
  - id: wifi_iconstring
    type: std::string
    restore_value: no
    initial_value: '"󰤮"'
  - id: battery_iconstring
    type: std::string
    restore_value: no
    initial_value: '"󰂁"'
  - id: solarman_battery_iconstring
    type: std::string
    restore_value: no
    initial_value: '"󰂁"'
  - id: recent_touch
    type: bool
    restore_value: no
    initial_value: "true"

And the script takes the name of a global as a vriable like this:

script:
  - id: update_battery_icons
    parameters:
      param_name: string
      value: float
    then:
      - lambda: |-
          if (isnan(value)){
            id(param_name) = "󰁾"; 
          }
          ....
          else{
              id(param_name) = "󰁹";
          }

Thanks for your help

I don’t think there is “out of the box” way to do that.
Have a look at the approach from last post of this topic:

thanks!

Sounds a little too complex for me. For now i will just copy past the code

I totally agree…