Hi
I am using an ESP32 with a TFT display to show sensor data from HomeAssistant. I want to set up four of these modules, i.e., four ESP32s, each with the same software. What is the best way to do this? I don’t want to create four devices in ESPHome Builder and copy the code. However, this is not very maintenance-friendly.
What’s wrong with that?
Packages would help but you would still need your four base devices.
If you want to flash over the air, you need 4 yaml files, but they can be identical.
See here re mac_suffix;
N.b.:
This approach might be old and not line up with the latest ESPHome code - I haven’t touched it for months because it works; I think it was flashed to the ESP devices under Version 2025.7.3 or 2025.8.1
Here you go:
I use include and a template file, e.g. this is the code for the individual device, I have 10+ of them - doesn’t work without individualizing them:
substitutions:
devicename: "sonoff_s31_01"
devicename_no_dashes: sonoff_s31_01
friendly_devicename: "Sonoff_S31_01"
device_description: "Sonoff_S31_01"
voltage_cal: "0.5"
restore_mode_setting: ALWAYS_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 if not possible to restore.
#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"
cse7766_throttle_interval: "5s"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
reboot_timeout: 0s
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "${devicename}"
password: !secret other_wifi_password
<<: !include templates/template-s31-outlet.yaml
Together with other templates, the template file called template-s31-outlet.yaml lives in a subfolder of my main esphome folder called templates; it starts off like this:
# Basic Config
esphome:
name: ${devicename}
comment: ${device_description}
friendly_name: ${friendly_devicename}
# NEW - remove if upgrades fail
name_add_mac_suffix: false
esp8266:
board: esp01_1m
early_pin_init: false #This prevents the on-off-on of the relay upon firmware upload
logger:
baud_rate: 0 # (UART logging interferes with cse7766)
and it holds the ‘standard yaml code’ for all the devices like
sensor:
- platform: wifi_signal
name: "WiFi Signal"
update_interval: ${update_interval_wifi}
Note that everything except the comments is moved two spaces/one tab to the right compared to where it would be in an individual device’s yaml file.
I could probably move even more of the individual code into the template file, like update_interval_wifi, cse7766_throttle_interval, and restore_mode_setting, but they vary from device to device.
that’s why I do pass variables, I got bme280 sensors listening on 0x76 aswell as 0x77 for example or some which simply need to send values in different intervals.
sensor:
- << !include
file: sensor/bme280.yaml
vars:
addr: 0x76
prefix: bme280/
sendevery: 8
update: 30s # (8 *30 => 4mins)
By now I think I have next to no directly coded sensor, binary_sensor.
And I do hate code redundancy. Once a new sensor is set up it’s sort of static till hell and back, perhaps it might require a mod in case of a breaking change (such as the dallas sensor when 1-wire got modified some months ago) but then this is a “modify 1 yaml” not modify 5 yamls since 5 differnt ESPs got dallas sensors attached.
What I’m struggling with is I haven’t found a way yet to enable/disable things by conditions. Found no way yet.
what I’m after it for example to enable/disable
MQTT:
file: package/mqtt.yaml
this within the packages: by a condition so I could for example set up a substitution such as “useMQTT: true” or such like which then adds the MQTT package aswell as allowing the same condition with files like te above for the bme280 sensor with the difference that needs a line “state_topic: xx” in case of MQTT being active.
This is currently a misery since I need 2 yaml for each sensor, one with and another without MQTT.
Perhaps someone with a dead clever idea on how to define things being taken into account by a condition? And no the condition isn’t playing a '#" in front of the relevant lines.