Substitutions in lambda?

Hello!

I’m using substitutions in esphome such as follows:

substitutions:
  project: "My reactor"
  temp_cutoff: "38"

Is there a way to access the value temp_cutoff in lambda code? id(temp_cutoff).state does not work. Any ideas?

Might be easier to use a global variable which can be accessed…

 # Example configuration entry
 globals:
   - id: my_global_int
     type: int
     restore_value: no
     initial_value: '0'

# In an automation
on_press:
  then:
    - lambda: |-
        if (id(my_global_int) > 5) {
          // global value is greater than 5
          id(my_global_int) += 1;
        } else {
          id(my_global_int) += 10;
        }

        ESP_LOGD(TAG, "Global value is: %d", id(my_global_int));

1 Like

not pretty, not even sure if you should do it this way but this works:

text_sensor:
  # status
  - platform: template
    name: sensor id
    lambda: !lambda |-
      return to_string("${name}");

as far as i can tell you need to typecast from:
‘const char [14]’ to `‘esphome::optional<std::__cxx11::basic_string >’

which i think it means you either assign the value to a variable of the right type or you typecast it but the substitution needs quotes otherwise i get an error because the to_string method breaks if you have special characters like spaces

2 Likes