Device attributes from MQTT subtopic

I have sensors that report via MQTT as follows:

/sensors/SENSOR_NAME/temperature reporting e.g. 20.0
/sensors/SENSOR_NAME/humidity reporting e.g. 100.0

How do I configure HA so that it records each SENSOR_NAME as a device and all data coming as e.g. SENSOR_NAME/temperature as an attribute of that device with the value as reported? Ideally I would like a rule that automatically creates the device and the attributes, but definitely I do not want to define each single topic and attribute by hand.

  • An entity can have attributes, not a device.

  • A device is a means of logically grouping multiple entities.

If you want to maintain a history of temperature and humidity values, you should create separate entities, one for temperature and another for humidity. You can logically group them as a single device.

Example

mqtt:
  sensor:
    - name: Outdoor Temperature
      unique_id: outdoor_temperature
      state_topic: /sensors/outdoor/temperature
      unit_of_measurement: "°C"
      device:
        name: Foo Environmental Monitor
        manufacturer: Foo
        model: foo9000
        identifiers:
          - abc123
    - name: Outdoor Humidity
      unique_id: outdoor_humidity
      state_topic: /sensors/outdoor/humidity
      unit_of_measurement: "%"
      device:
        name: Foo Environmental Monitor
        manufacturer: Foo
        model: foo9000
        identifiers:
          - abc123

Right, thanks. But is there a way where I don’t have to define all these entities one-by-one, but rather HASS automatically assumes SENSOR_NAME from the topic name as the device and e.g. SENSOR_NAME_temperature as the entity?

You have to define each entity. There’s no process where an entity is automatically created merely by supplying a topic.

Whether you define the entity in YAML or via MQTT Discovery, it amounts to the same thing; each entity must be defined.

Clear, thanks.