Mqtt multiple sensor discovery

Is it possible to discover multiple sensors with a single discovery topic?

I have sensors that publish on sensor/<id> with the payload:

{"Sequence": 32570,"Watt":  685.20,"kWh": 2475.654,"battery": 100,"FreqErr": 0.28,"Sensor":<id>}

The following discovery message works for a single sensor but would it be possible to discover all sensors with one discovery message?

"device": { "name": "Sensor 123", "identifiers": "123", "model": "Sense", "manufacturer": "Acme Inc."}, 
"unit_of_measurement": "kWh", 
"json_attributes": ["Sequence", "FreqErr", "battery"], 
"name": "Sensor123 kWh", 
"unique_id": "sensor_123_kwh", 
"state_topic": "sensor/123", 
"value_template": "{{ value_json.kWh | round(2) }}"

What i want is to auto generate 123 based on what is published on sensor/.


I’ve come so far that I’ve created the following automation, but I don’t know a reasonable condition to stop spamming the mqtt.

- id: 'MQTT_auto'
  alias: Sensor auto-conf
  - platform: mqtt
    topic: sensor/+
  condition: []
  - data_template:
      payload: >
        {"device": {"sw_version": "215a4aa", "name": "Sensor", 
                    "identifiers": "{trigger.payload_json.Sensor}}", 
                    "model": "Sense", "manufacturer": "Acme Inc."}, 
        "unit_of_measurement": "kWh",
        "json_attributes": ["Sequence", "FreqErr", "battery"], 
        "name": "Sensor {{ trigger.payload_json.Sensor }} kWh", 
        "unique_id": "sensor_{{trigger.payload_json.Sensor}}_kwh", 
        "state_topic": "sensor/{{trigger.payload_json.Sensor}}",
        "value_template": "{{'{{ value_json.kWh }}'}}"}
      topic: home/sensor/sense/{{ trigger.payload_json.Sensor }}/config
    service: mqtt.publish

Hello, I use unique topics for each device.
My main reference for my realitations was MQTT Discovery

May your question is answered there.

The idea behind auto discovery was that each sensor would send its own discovery string when it starts up.

You are better off just writing a standard sensor defintion than trying to send mqtt messages from HA to configure sensors.