esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
406 stars 26 forks source link

substitution support for arrays and other data types #1215

Open lanrat opened 3 years ago

lanrat commented 3 years ago

Describe the problem you have/What new integration you would like Currently substitutions appear to be limited to strings. It would be very helpful if they could be any data type, particularly arrays.

Please describe your use case for this integration and alternatives you've tried: I have multiple ESPHome nodes that send raw IR codes. It would be nice if I could define the IR codes in a package as a yaml variable or substitution.

Additional context I don't want to have the entire service call in the package as each node will implement the service slightly differently. But they will all need the same raw codes.

With the help of some friendly users on Discord, we found a hack around this. It's possible to define the array as a string in a package as a substitution, and then in the service use a lambda to return the substitution as a C++ vector. This works, but native substitution support for arrays would be preferred.

Current hacky solution

substitutions:
  vizio_power_raw: '9103, -4475, 621, -515, <SNIP...>'

 switch:
   - platform: template
     name: Power
     turn_on_action:
       - remote_transmitter.transmit_raw:
           carrier_frequency: 38kHz
           code: !lambda 'return {${vizio_power_raw}};'

Ideal solution: (currently does not compile)

substitutions:
  vizio_power_raw: [9103, -4475, 621, -515, <SNIP...>]

 switch:
   - platform: template
     name: Power
     turn_on_action:
       - remote_transmitter.transmit_raw:
           carrier_frequency: 38kHz
           code: ${vizio_power_raw}
heinekmadsen commented 2 years ago

I would love to see this too :)