ESPHome reuse code

I have several sonoff mini (same version) with esphome running behind light wall switch. they all have same code, I used substitute to change device, sensor etc name. So basically apart from substitute part, all others are same. Can I write the code in one yaml file and call that file in main device file?

For example:

Full code:

substitutions:
  location: abstellraum
  device_name: eh-${location}-sw

esphome:
  name: ${device_name}
  platform: ESP8266
  board: esp8285

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

wifi:
  ssid: !secret wifi_ssid_NOT
  password: !secret wifi_password_NOT
  fast_connect: true

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name}_Hotspot"
    password: !secret wifi_password_NOT

captive_portal:

# Enable Web server
web_server:
  port: 80
  
sensor:
# Extra sensor to keep track of plug uptime
  - platform: uptime
    name: ${device_name}_uptime
    filters:
      - lambda: return x / 3600;
    unit_of_measurement: "hours"
    accuracy_decimals: 2

# Extra sensor for WiFi signal
  - platform: wifi_signal
    name: ${device_name}_WiFi
    update_interval: 60s

#######################################
# Device specific Config Begins Below #
#######################################

switch:
  - platform: gpio
    name: ${device_name}_switch
    pin: GPIO12
    id: relay_1
    restore_mode: restore_default_on

button:
  - platform: restart
    name: "${device_name}_switch Restart"
    
binary_sensor:
  - platform: gpio
    internal: true
    pin: GPIO00
    id: reset
    filters:
      - invert:
      - delayed_off: 10ms
    on_press:
      - switch.toggle:
          id: relay_1

  - platform: gpio
    name: ${device_name}_status
    pin: GPIO04
    id: switch_1
    on_press:
      then:
        - switch.toggle:
            id: relay_1
    on_release:
      then:
        - switch.toggle:
            id: relay_1

I want maybe like this:

device.yaml

substitutions:
  location: abstellraum
  device_name: eh-${location}-sw

!include esphome_common.yaml
!include sonoff_mini_common.yaml

esphome_common.yaml

esphome:
  name: ${device_name}
  platform: ESP8266
  board: esp8285

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

wifi:
  ssid: !secret wifi_ssid_NOT
  password: !secret wifi_password_NOT
  fast_connect: true

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name}_Hotspot"
    password: !secret wifi_password_NOT

captive_portal:

# Enable Web server
web_server:
  port: 80
  

sonoff_mini_common.yaml

sensor:
# Extra sensor to keep track of plug uptime
  - platform: uptime
    name: ${device_name}_uptime
    filters:
      - lambda: return x / 3600;
    unit_of_measurement: "hours"
    accuracy_decimals: 2

# Extra sensor for WiFi signal
  - platform: wifi_signal
    name: ${device_name}_WiFi
    update_interval: 60s

#######################################
# Device specific Config Begins Below #
#######################################

switch:
  - platform: gpio
    name: ${device_name}_switch
    pin: GPIO12
    id: relay_1
    restore_mode: restore_default_on

button:
  - platform: restart
    name: "${device_name}_switch Restart"
    
binary_sensor:
  - platform: gpio
    internal: true
    pin: GPIO00
    id: reset
    filters:
      - invert:
      - delayed_off: 10ms
    on_press:
      - switch.toggle:
          id: relay_1

  - platform: gpio
    name: ${device_name}_status
    pin: GPIO04
    id: switch_1
    on_press:
      then:
        - switch.toggle:
            id: relay_1
    on_release:
      then:
        - switch.toggle:
            id: relay_1

Yes you can, works just like any program with multiple files you would make. I do this with my ESP32’s with display to maintain fonts/colors/sensor files etc. Make your default common file and then in each device file add something like this towards the top:

<<: !include base.yaml

I keep this right under the esp32: board module section.

4 Likes