MQTT: Understanding Entities vs Devices

I am building a device that reports, via MQTT, various sensor readings (temp, humidity, pressure, co2, etc.)

I am confused about how I set these up in hass.

It’s easy enough to define an MQTT sensor:

  - platform: mqtt
    name: "My CO2 Sensor"
    state_topic: "topic/subtopic/co2"
    unit_of_measurement: "ppm"
    unique_id: "my-co2-sensor"
    expire_after: 300

That creates a new “entity” and I can do that for each sensor I have.

How do I associate them all together into one device, however?

The MQTT discovery page seems to indicate that I can create a new device for each sensor (section on “Sensors with Multiple Values”) https://www.home-assistant.io/docs/mqtt/discovery/#sensors-with-multiple-values.

I want one device, multiple entities. Is this possible?

I am not 100% sure about MQTT but when I am making integrations, setting the unique_id to the same makes HA understand that it is the same device. :blush:

Curious. The MQTT documentation says:

An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception.

Take a look at the mqtt discovery docs for an entity type… (switch: https://www.home-assistant.io/integrations/switch.mqtt/#device). When you use HA’s discovery feature, you can send a device payload with each entity’s config as opposed to putting static config in yml. I’m not sure if you can replicate this in yml or not but it may be doable.

The way I think of this is imagine you’re building a physical device that has multiple sensors on it/switches… you’d send a discovery payload for each entity but the “device” block would be consistent for all of them. Discovery blocks device id should be something distinct from every other device, but stable (something like a MAC address, but probably not IP address). This allows HA to retain stable configuration and recognize/associate it with the unique id.

1 Like

Right, it says in the docs for MQTT that you have to use MQTT discovery if you want to group entities by device. :blush:

1 Like

Thank you, all! This makes more sense. When I first read the documentation, I assumed the “device” section was about correlating an entity with an already defined device.

I’d still prefer it if this whole thing was done on the hass side - I don’t want my device to have hass specific details embedded within its public interface.

I am going to dig into what 123 wrote and see if I can come up with something clever (but not too delicate) from that.

Thanks again!