Help with packages: override

Help with packages: override

First, I can’t find this in the docs. I know they are there, but I can’t find it again.
I use a couple of packages that contain code common to most of my ESPHome devices. It reduces 60 lines to two. It makes the YAML file very clean.
Below is an example of how I use it:

#kim-bedroom-light.yaml
#Sonoff Basic Module

substitutions:
  device_name: kim-bedroom-light
  friendly_name: kim_bedroom_light
  
packages:
  wifi: !include common/wifi.yaml
  device_base: !include common/esp8266.yaml
#Override the default board type.
esp8266:
  board: esp01_1m    #Sonoff Basic and Mini

As shown, I can override the board type by setting it in the device YAML file. (Yes, I know that esp01_m is redundant, but this is an example).

Here for reference is my common/esp8266.yaml file:

esphome:
  name: ${device_name}
  project:
    name: steve.${project_name}
    version: ${project_version} 
    
esp8266:
  board: esp01_1m
  framework:
    version: recommended
    
# Logger level "debug" is needed to get 1-Wire addresses, like the ds18b20.
logger:
  level: DEBUG
  # Set the log level for the light component to info
  logs:
    light: info

ota:
  safe_mode: True

# Enable Home Assistant API
api:

Now, here’s my question…
Here is my wifi-Static.yaml file:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password  
  manual_ip:
    static_ip: 192.168.1.211
    gateway: 192.168.1.1
    subnet: 255.255.255.0

sensor:
  - platform: wifi_signal
    name: ${friendly_name} WiFi Level
    id: ${friendly_name}_WiFi_level
    update_interval: 300s

# Get the WiFi details
text_sensor:
  - platform: wifi_info
    ip_address:
      name: ${friendly_name} IP
    ssid:
      name: ${friendly_name} SSID
    mac_address:
      name: ${friendly_name} Mac Address    

# to remove the Web Server, Add this line: "web_server: !remove" to the device YAML file.
web_server:
  port: 80

I want to override static_ip: 192.168.1.211

Can anyone help me with the syntax?

I just did something similar like this:

This is the entire yaml for the device.

substitutions:
  static_ip_last_3chars: "134"
  plug_number: "1"
  plug_colour:  "Black"

packages:
  device_base: !include common/.arlec-smartplug-common.yaml

common/.arlec-smartplug-common.yaml

substitutions:
  relay_restore_mode: RESTORE_DEFAULT_OFF         #Adjust as required.
  hlw8012_update_interval: 30s                    #Not sure how this affects lifetime and maybe overheating? 
  static_ip_last_3chars: "xxx"
  plug_number: "xxx"
  plug_colour:  "xxx"


esphome:
  name: arlec-smartplug-${plug_number}
  friendly_name: Arlec Smartplug ${plug_number}
  comment: Arlec ${plug_colour} Grid Connect Smart Plug in Socket with Energy Meter 
  
wifi:
  ap:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.1.${static_ip_last_3chars}
    gateway: 192.168.1.1
    subnet: 255.255.255.0


bk72xx:
  board: generic-bk7231n-qfn32-tuya #https://docs.libretiny.eu/boards/cb2s/  

logger:
mdns:
api:
  password: ""
ota:
  password: ""

...code continues....

Note how the substitutions get overidden. I’m still learning about packages so not sure if my way is the best…

Substitutions in your main config will override substitutions with the same name in a package.

1 Like

Thanks- this looks to me like the perfect solution.

1 Like