Multiple Common Components

I’m trying to do exactly (i think) what the OP in the link below was trying to do. Otto mentioned in a reply that it’s a limitation of yaml and to use the !include_dir_list directive instead. However, I can’t figure out how to actually do this. I’ve created a directory with an example file in it but don’t know the syntax to call it from the main yaml file. Ideally, I’d have multiple files in that directory that each define a sensor or set of sensors that get merged at compile time.

Link:

Reference File:

- platform: wifi_signal
  id: wifi
  name: ${friendly_name} - RSSI
  icon: "mdi:wifi"
  update_interval: 60s
  unit_of_measurement: 'dB'
  accuracy_decimals: 0

- platform: uptime
  id: uptime
  name: ${friendly_name} - Uptime
  icon: "mdi:timer"
  update_interval: 60s

Things I’ve tried in the main yaml file without success:

sensor:  !include_dir_list   path/to/files/
#--------------------------------------------------------------------------
sensor:
  !include_dir_list   path/to/files/
#---------------------------------------------------------------------------
sensor:  <<: !include_dir_list   path/to/files/
#--------------------------------------------------------------------------
sensor:
  <<: !include_dir_list   path/to/files/
#---------------------------------------------------------------------------

Anyone have any ideas? (I’m on version 1.12.1)

-Josh

here is an example using a sonoff basic. i have two files that i created in my esphome config directory:

sonoff_boot_dryer.yaml:

substitutions:
  name: boot_dryer
  ip: 192.168.1.31
  friendly_name: Boot Dryer
<<: !include common_sonoff_basic.yaml

common_sonoff_basic.yaml:

esphome:
  name: ${name}
  platform: ESP8266
  board: esp01_1m
  board_flash_mode: dout

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pwd
  reboot_timeout: 0s
  fast_connect: true
  manual_ip:
    static_ip: ${ip}
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    
# Enable logging
logger:

# Enable Home Assistant API
api:
  reboot_timeout: 0s

ota:
  
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    id: button
    # name: "Sonoff Basic Button" # uncomment to expose the button as a binary senor
    on_press:
      - switch.toggle: relay_template
    on_multi_click:
    - timing:
        - ON for at most 0.5s
        - OFF for at most 0.5s
        - ON for at most 0.5s
        - OFF for at most 0.5s
        - ON for at most 0.5s
        - OFF for at most 0.5s
        - ON for at most 0.5s
        - OFF for at least 0.2s
      then:
        - switch.turn_on: restart_sonoff
           
switch:
  - platform: gpio
    pin: GPIO12
    id: relay
  - platform: template
    name: ${friendly_name}
    id: relay_template
    lambda: |-
      if (id(relay).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - light.turn_on: status_led
      - switch.turn_on: relay
    turn_off_action:
      - light.turn_off: status_led
      - switch.turn_off: relay
    # optimistic: true # this will change the toggle switch to individual on/off buttons
  - platform: restart
    id: restart_sonoff
    

output:
  ## https://esphomelib.com/esphomeyaml/components/output/esp8266_pwm.html
  ## create a PWM pin to control
  - platform: esp8266_pwm
    id: basic_green_led
    pin:
      number: GPIO13
      inverted: True

light:
  ## https://esphomelib.com/esphomeyaml/components/light/monochromatic.html
  ## attach a monochrome light to the PWM pin
  - platform: monochromatic
    # name: "Sonoff Basic Green LED" # uncomment to expose the led as a light with brightness control
    output: basic_green_led
    id: status_led
    default_transition_length: 1ms # controls the light fade/transition time
    
text_sensor:
  - platform: version
    name: ${friendly_name} ESPHome Version
    
sensor:
  - platform: wifi_signal
    name: ${friendly_name} WiFi Signal Strength
    update_interval: 60s

The substitutions pass those variables to the !included yaml file.

I have all of that working great. The problem is if I include two files that, for example, both have a sensor entry, the latest one takes precedence and overtired any other services sensors. (See link in original post)
I’m looking to be able to add multiple files that couple define entities and Otto hinted at a method to do so but I can’t figure out the syntax.

-Josh

Just tripped over your post looking to do the same thing. I have 1 of 10 (ish) sonoff basics I would like a extra template switch on and would have prefered shared YAML for the rest

As Otto said it’s a YAML limitation. I think what we would like here is the equivalent of HA packages for ESPHome

@finity’s example above is designed for multiple devices all with the same config except names. This appears to be about as good as it gets at the moment.

If it helps any I have had success with multiple includes as long as you avoid using the same key in them. This allows me to at least split out core config such as wifi, logger, ota etc.

“I think what we would like here is the equivalent of HA packages for ESPHome”

Exactly! :slight_smile:

-J

How can I include more than one yaml file?
What is the syntax?

Oh I am also trying to do this. Maybe we need a build step or similar…

Why only try and not just doing it? :thinking:

Packages

Another way to modularize and reuse your configuration is to use packages. This feature allows you to put common pieces of configuration in separate files and keep only unique pieces of your config in the main yaml file. All definitions from packages will be merged with your main config in non-destructive way so you could always override some bits and pieces of package configuration.

We need people to RTFM which even comes with a :mag: search function included :bulb:

And what made you think we are not in the process of searching and learning how to do it?

Or you just want to feel superior because you know things we do not?

Is thru searching that we land in this topics… Jeez,.

Anyway, thanks for your help.

By resuscitate a 3 year old thread and adding 2 sentences with :zero: value to the topic? :thinking:

Packages in esphome aren’t a secret at all. Like the secrets.yaml most of the people with a couple of nodes probably use packages as it just simplifies things and avoids repeating configs across devices. :bulb:

And you didn’t gave a thought that a topic from 2019 might be heavily outdated in 2023 (that’s 4 years later) specially for projects involving/growing fast like esphome or HA? :eyes:

You are very welcome :wink:

I hope you are loved internet stranger. Good vibes.

1 Like