Streamlining ESPHome configs, is this possible?

Dear Forum,

I have 21 ESPHome devices in my house. I created the first ones some two years ago and the most recent yesterday. During this time I learned a lot of improvements that I would like to have in all of them. So I frequently update all cofigurations manually to incorporate the lessons learned. This is quite tedious and prown to errors. I would wish there is another way.

Actual example code (reduced to the minimal lines to show myquestion):

esphome:
  name: mobil_hal9000
  platform: ESP32
  board: esp32dev
wifi:
  networks:
  - ssid: "SSID1"
    password: "PW1"
  - ssid: "SSID2"
    password: "PW2"
  ap:
    ssid: "mobil_hal9000 Fallback Hotspot"
    password: "PW5"
captive_portal:

text_sensor:
  - platform: wifi_info
    ip_address:
      name: "mobil_hal9000_BMS_IP Adress"

So the first thing I would like to have is a way of reusing the esphome name in the config. Something like this:

esphome:
  name: mobil_hal9000
  platform: ESP32
  board: esp32dev
[...]
  ap:
    ssid: "$espname Fallback Hotspot"
    password: "PW5"
captive_portal:

If this works, I can hopefully use the include mechanisms to have the common parts of the config in just one file, using ā€œ$espnameā€ as a variable. Then I would just edit one file, do an ā€œupdate allā€ and be ready to goā€¦ Also this would highly increase the readability of the configurations.

Is there a way to do this?

You can use ā€˜substitutionsā€™

Argh, how did I miss this?

So my example could look like this?

substitutions:
  devicename: mobil_hal9000
esphome:
  name: $devicename
  platform: ESP32
  board: esp32dev
wifi:
  networks:
  - ssid: "SSID1"
    password: "PW1"
  ap:
    ssid: "${devicename} Fallback Hotspot"
    password: "PW5"
captive_portal

Yep. That should work

1 Like

Wohoo! My config looks now like this:

substitutions:
  devicename: og_ma_kristalllampe
  upper_devicename: OG_MA_Kristalllampe

esphome:
  name: $devicename
  platform: ESP8266
  board: d1_mini

wifi: !include conf_wifi.yaml
text_sensor: !include conf_text_sensor.yaml
captive_portal:
logger:
api:
ota:
web_server:
  port: 80

Iā€™d guess it does not become cleaner than this :slight_smile:

Thanks for pointing me in the right direction!

No worries. Unfortunately there are often awesome things out there we simply havenā€™t read about yet!

If you havenā€™t already, you might want to look into the ā€œ!secretā€ substitution mechanism as well. Itā€™s an HA thing that is also supported in ESPhome configs.

1 Like

You can also use includes.

1 Like