ESPHOME: Get BLE Sensors from different ESP's / Set sensor specific topic in ESPHome

Hi there,
I have ten lywsd03mmc sensors scattered around the house and want to collect their data with esp32’s which are already doing something else. The placement of the esp devices is not perfect, so sometimes a sensor is catched by one esp and sometimes by another. To make sense of all this, my plan was the following:

  1. Configure every sensor within every esp
  2. Send sensor data to a sensor-specific mqtt topic (one topic for sensor one, another topic for sensor two, etc.)
  3. Configure a mqtt device for each topic to bundle the sensors together in home assistant

I’m already failing at step two: The states are published to HA via MQTT, but my configured topics aren’t used. What I’ve tried (smaller testing config):

[ESPHome config]
sensor:
  - platform: xiaomi_lywsd03mmc
    mac_address: "A4:C1:38:8A:XX:XX"
    bindkey: "XXXXX"
    temperature:
        name: "Temp01"
        state_topic:  homeassistant/sensor/LYWSD03MMC_02/Temp
    humidity:
        name: "Hum01"
        state_topic:  homeassistant/sensor/LYWSD03MMC_02/Hum
    battery_level:
        name: "Bat01"
        state_topic:  homeassistant/sensor/LYWSD03MMC_02/Bat
  - platform: xiaomi_lywsd03mmc
    mac_address: "A4:C1:38:73:YY:YY"
    bindkey: "YYYYY"
    temperature:
        name: "Temp04"
        state_topic:  homeassistant/sensor/LYWSD03MMC_04/Temp
    humidity:
        name: "Hum04"
        state_topic:  homeassistant/sensor/LYWSD03MMC_04/Hum
    battery_level:
        name: "Bat04"
        state_topic:  homeassistant/sensor/LYWSD03MMC_04/Bat
  - platform: uptime
    name: Uptime

The state topic seems to be ignored. Can someone push me in the right direction, or is this approach doomed to fail?

I have no hands-on experience with ESPHome, but I’m pretty sure it doesn’t work that way. You cannot “force” the topic from the sensor configuration.

What are the topics created by ESPHome? Arent’t they name-based?

You are correct. Neither xiaomi_lywsd03mmc nor the core sensor component have a configuration variable for state_topic. And making up your own is not how this works. You can only use the configuration variables specified in the documentation.

The topic that ESPHome uses is:

<TOPIC_PREFIX>/<COMPONENT_TYPE>/<COMPONENT_NAME>/state.

TOPIC_PREFIX defaults to the device name though it can be changed in the ESPHome MQTT config.

So as you have to have different device names (or you won’t be able to manage them), you should set a topic_prefix: under mqtt: and use the same xiaomi_lywsd03mmc name: in each ESP device. Then they will all publish to the same topics.

Actually I wonder how to do the same without mqtt but just by using the native api.

Guess easiest should be to have a template sensor in home assistant which is configured to be updated once a new value from any of the ep32 is received.