ESPHome configuring multiple identical sensors with different IDs

I plan to use several proximity sensors. Obviously they will need unique names, but otherwise they are all the same type of device. Do I need to have a separate ESPHome .yaml for each one, even though the bulk of the config data is the same? I want to use “Ultrasonic Sensor 1”, “Ultrasonic Sensor 2”, etc. and that is the only config data that will change.

Here is the ESPHome yaml:

esphome:
  name: proximity_sensor
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "..."
  password: "..."

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Proximity Sensor"
    password: "GGQdNE8ypHXa"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

# Example configuration entry
sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"
    update_interval: 2s
1 Like

Yes, you should have separate yaml files with different name, as Esphome will assign these sensors hostnames in local network, based on that (proximity_sensor.local) and use that for (e.g.) OTA updates.

You might also want to move to MQTT in the future which means every sensor will need its own client_id and topic_prefix in mqtt section of the yaml.

Thanks! I’m still trying to grok the MQTT labeling. The dox say the topic prefix defaults to my node name plus the MAC address of my device. I left those blank but the mqtt messages don’t seem to follow that pattern. Here’s my ESPHome yaml for the device and the resulting messages; maybe you could help me understand what I’m seeing.

proximity_sensor.yaml

esphome:
  name: proximity_sensor
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "my_ssid"
  password: "my_pwd"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Proximity Sensor"
    password: "a_bunch_of_giberish"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
#api: disable to native api and use mqtt
mqtt:
  broker: [broker ip]
  username: [broker username]
  password: [broker_pwd]

ota:

# Example configuration entry
sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Ultrasonic Sensor"
    update_interval: 2s

mqtt messages:

▼proximity_sensor

   debug = e[0;36m[D][sensor:092]: 'Ultrasonic Sensor': Sending state 1.34730 m with 2 decimals of accuracye[0m
   ▼sensor
      ▼ultrasonic_sensor
          state = 1.35
    status = online
▼homeassistant
   ▼sensor
      ▼proximity_sensor
         ▼ultrasonic_sensor
            config = {"unit_of_measurement":"m","icon":"mdi:arrow-expand-vertical","name":"Ultrasonic Sensor","state_topic":"proximity_sensor/sensor/ultrasonic_sensor/state","availability_topic":"proximity_sensor/status","unique_id":"ESPsensorultrasonic_sensor","device":{"identifiers":"84cca8b16622","name":"proximity_sensor","sw_version":"esphome v1.15.3 Jan 20 2021, 10:12:52","model":"PLATFORMIO_D1_MINI","manufacture…

Thanks!

Is there a reason to use MQTT? Your example in the first post doesn’t, why the change in plan?

I’m using MQTT because it allows me to view the comm traffic independent of HA. That way I can use an MQTT sniffer to see if the device is working properly without relying on HA integration.

You should look into ESPHome substitutions and packages to avoid redundancies in your config files.

https://esphome.io/guides/configuration-types.html#substitutions

1 Like

Hi, came across the same issue. It seems (it worked) you can define all sensors under one ‘sensors’ definer. providing you give them all seperate id’s

Here’s my example script below - I have a battery monitor and a dht instance, all working

sensor:

  • platform: dht

    pin: GPIO3

    id: “DHT”

    temperature:

    name: “Living Room Temperature”

    humidity:

    name: “Living Room Humidity”

    update_interval: 60s

  • platform: adc

    pin: VCC

    id: “LIION”

    name: “Battery Voltage”

    update_interval: 15s

    accuracy_decimals: 2

  • platform: template

    name: “D1003_Battery_Percentage”

    unit_of_measurement: ‘%’

    update_interval: 15s

    accuracy_decimals: 0

    lambda: |-

    return (id(LIION).state /3.3 * 100.00);