Create multiple similar components in yaml

Is there an easy way to create a set of parametrized devices? Notably, I have a bunchy of fans that each have unique RF codes. Pretty much everything is the same except I’d like to change their name, ID & Code.

Looking at these forums, I’ve tried substitution by declaring my parameters, and that works to create one instance, but on the second instance, it doesn’t like that I’m declaring substitution again. Fair enough that’s not how it works.

Is there a cleaner way than copy/pasting and substituting things around? Here’s what I have that I’d like to create functions for:

output:
  - platform: template
    type: float
    id: ${device_id}_output
    write_action:
      - lambda: get_cc1101(transciver).beginTransmission();
      - remote_transmitter.transmit_raw:
          repeat:
            times: 5
            wait_time: 12.12ms
          code: !lambda return getMinkaAirCodes(${device_code}, round(state * 3));
      - lambda: get_cc1101(transciver).endTransmission();

fan:
  - platform: speed
    output: ${device_id}_output
    name: ${device_name}
    friendly_name: ${device_name}
    icon: mdi:fan
    speed_count: 3
    id: ${device_id}

Write a Jinja template to generate the code in HA’s Template Editor, then copy/paste. See here for an example:

I use GO templates to generate YAML code at HA docker container startup

Use packages and substitutions, both part on ESPHome. See the docs for more info.

I have about the same situation. I use 7 heating boosters running on ESPHome. They all share the same code, except for their specific device names that are visible for HA.

3 Likes

I did this recently for the first time this way and it worked quite well. Just followed the docs.

Here’s another example for OP.

Thank you so much. That’s the solution. Packages. The yaml I posted above works as-is. I can’t use “substitution:” because only one version of that is allowed, but I can use packages and vars in the following way:

packages:
  family_room_fan: !include
    file: minkaair.yaml
    vars:
      device_id: family_room_fan
      device_name: Family Room Fan
      device_code: "0b1000"

Copy/paste the above block and changes the variables and presto. Cleaner than other alternatives for sure.

1 Like