How add device with MQTT discovery?

I’m developing an ESP8266 board with 2 temperature sensors. They use mqtt over wifi to connect to HA.
At first I defined the devices in the yaml files like this:

- platform: mqtt
  name: "mqtt_dev0_temp1"
  state_topic: "cv_temp/ECFABC5ECDBB/temp"
  value_template: "{{ value_json.temp1 }}"
  qos: 1

This worked fine.

But then I wanted to improve things and add MQTT device discovery.
Like here

And I just can’t get this right.
If I send a straightforward json like this one, it is seen by HA:

{
  "name": "mqtt_ECFABC5ECD98_temp1",
  "stat_t": "cv_temp/ECFABC5ECD98/temp",
  "uniq_id": "cvtemp-ECFABC5ECD98-temp1",
  "dev_cla": "temperature",
  "unit_of_meas": "°C",
  "val_tpl": "{{ value_json.temp1 }}"
}

But I want to add the proper device too. When I try something like this, HA won’t accept it.

{
  "name": "mqtt_ECFABC5ECD98_temp1",
  "stat_t": "cv_temp/ECFABC5ECD98/temp",
  "uniq_id": "cvtemp-ECFABC5ECD98-temp1",
  "dev_cla": "temperature",
  "unit_of_meas": "°C",
  "val_tpl": "{{ value_json.temp1 }}",
  "dev": {
    "identifiers": "cvtemp-ECFABC5ECD98",
    "manufacturer": "Me",
    "model": "model",
    "name": "CV_Temp_module",
    "sw_version": "1.0"
  }
}

What am I doing wrong here?

try

{
  "name": "mqtt_ECFABC5ECD98_temp1",
  "stat_t": "cv_temp/ECFABC5ECD98/temp",
  "uniq_id": "cvtemp-ECFABC5ECD98-temp1",
  "dev_cla": "temperature",
  "unit_of_meas": "°C",
  "val_tpl": "{{ value_json.temp1 }}",
  "dev": {
    "identifiers": ["cvtemp-ECFABC5ECD98"],
    "manufacturer": "Me",
    "model": "model",
    "name": "CV_Temp_module",
    "sw_version": "1.0"
  }
}

Thanks for your suggestion. I’ve changed it a bit and the serializeJsonPretty commands now prints it as:

{
  "name": "mqtt_cvtemp_ECFABC5ECD98X_temp1",
  "stat_t": "cv_temp/ECFABC5ECD98X/stat",
  "uniq_id": "cvtemp-ECFABC5ECD98X-temp1",
  "dev_cla": "temperature",
  "unit_of_meas": "°C",
  "val_tpl": "{{ value_json.temp1 }}",
  "dev": {
    "identifiers": [
      "cvtemp-ECFABC5ECD98X"
    ],
    "manufacturer": "Me",
    "model": "model",
    "name": "CV_Temp_module",
    "sw_version": "1.0"
  }
}

That still doesn’t work however.

Never mind. The problem was somewhere else. pubsubclient publish was limited to 256 bytes and my message was longer.

2 Likes

Had the exact same problem! Off to see where to increase message sizes

Basically it’s making use of beginPublish and endPublish instead of just publish functions of mqtt client.

See my sketch:

Thanks for this. Spent why more time than I wanted to try to get this working and your code was the answer.

[edit] This was the simple solution
mqttPubSubObject.setBufferSize(1024);