WTH Code templates for ESPHome (code you reuse on many/all your ESP projects)

Over time I developed some standard on how to program my ESP boards. I like to reuse the same code and dislike having to edit each device individually when I find a better way to do something.

One way to address this is to have a file with the common code and to include it in the YAML specific to the device.

In the example below, the code I use for the S21 outlet is common to all of them, except for calibrations, names, and other settings. I have all those as substitutions in the device specific file so I don’t have to create custom variants of the main template when I need to change something.

substitutions:
  devicename: s31-outlet-6
  devicename_no_dashes: s31_outlet_6
  friendly_devicename: "Spare Outlet"
  device_description: "Spare Outlet"
  voltage_cal: "0.0"
  restore_mode_setting: RESTORE_DEFAULT_ON
  #restore_mode: Control how the relay attempts to restore state on bootup.
  #RESTORE_DEFAULT_OFF          - Attempt to restore state and default to OFF if not possible to restore.
  #RESTORE_DEFAULT_ON           - Attempt to restore state and default to ON.
  #RESTORE_INVERTED_DEFAULT_OFF - Attempt to restore state inverted from the previous state and default to OFF.
  #RESTORE_INVERTED_DEFAULT_ON  - Attempt to restore state inverted from the previous state and default to ON.
  #ALWAYS_OFF                   - Always initialize the pin as OFF on bootup.
  #ALWAYS_ON                    - Always initialize the pin as ON on bootup.
  # Interval of how often the wifi info is updated
  update_interval_wifi: "60s"


# Basic Config
esphome:
  name: ${devicename}
  comment: ${device_description}
  platform: ESP8266
  board: esp01_1m

<<: !include s31-outlet-template.yaml

What I’d like to see is something like a code snippet library where I save the WIFI section of the code and just need to pick it from a list to add it to the device YAML. While this can be done manually, it would be easier and helpful as the library grows to have a list, and ESPHome do the inclusion (ie.: <<: !include wifi-settings.yaml)

I think you are looking for esphome packages :slight_smile:

2 Likes

@Mynasru I guess that gets close to it, but you need to remember what is in there. If that already exists what would be nice to have is a way to browse the templates and see what they contain. One way is to place them in the main folder, as I did with mine, but it is not ideal.

Thank you for providing a link to packages! I guess that is the functionality I was using but I had not thought about using it for smaller snippets (mine has the entire firmware except the configuration variables).

totally agree, now the device is shown as offline, while actually this is just my base.
if you use a subfolder, you can browse it with vscode. but for instance not use secrets anymore.

So actually this can use some improvements.

1 Like

Found one way to help with hiding templates from view:

Tip
To hide these base files from the dashboard, you can

Place them in a subdirectory (dashboard only shows files in top-level directory)

Prepend a dot to the filename, like .base.yaml
1 Like