Arduino MQTT autodiscovery Problem

I have an Arduino connected to my portafilter.

This receives data and makes it available to my mqtt. (MQTT is running and I have integrated many devices that work without problems).

For testing I then added the sensors in my configuration.yaml.

mqtt:
  sensor:
    - name: "MaraX2 Steam Temperature"
      state_topic: "MaraX2/MaraX2Data"
      unique_id: "marax2_steam_temperature"
      unit_of_measurement: "°C"
      value_template: "{{ value_json.steam_temperature }}"
    - name: "MaraX2 Target Steam Temperature"
      state_topic: "MaraX2/MaraX2Data"
      unique_id: "marax2_target_steam_temperature"
      unit_of_measurement: "°C"
      value_template: "{{ value_json.target_steam_temperature }}"

These sensors work and
now I would like to use the autodiscover but that doesn’t work.

According to the serial log of the Arduino, the autodiscovery message is sent.

11:05:29.802 → WiFi verbunden
11:05:29.833 → IP-Adresse:
11:05:29.833 → 192.168.1.129
11:05:29.865 → Verbinde zu MQTT…verbunden
11:05:29.896 → Publishing discovery message:
11:05:29.929 → {“name”:“LelitMaraX2”,“state_topic”:“MaraX2/MaraX2Data”,“availability_topic”:“MaraX2/availability”,“payload_available”:“online”,“payload_not_available”:“offline”,“device”:{“identifiers”:“LelitMaraX2”,“name”:“Mara”,“model”:“MaraX2”,“manufacturer”:“Lelit”},“unique_id”:“MaraX2mqtt”}

but when I look in the MQTT Explorer I can’t find anything under “homeassistant/sensor/MaraX2/config”
This Topic does not exist: homeassistant/sensor/MaraX2

I think there is an error somewhere in the sketch.
Because I honestly use the help ChatGPT to add the MQTT. Which also worked but not the autodiscover.

Here my Arduino Code

Maybe someone has an idea what I am doing wrong.

Please format code properly for the forum.

:roll_eyes: idiot robot strikes again.

I think the problem is in the publish call with the retain flag set. That retain flag is actually the fourth argument. Try this:

  // Publish sensor discovery message
  int len = strlen(jsonBuffer) + 1;
  client.publish(mqtt_discovery_topic, jsonBuffer, len, true);

when i change

  // Publish sensor discovery message
  client.publish(mqtt_discovery_topic, jsonBuffer, true);

to

// Publish sensor discovery message
  int len = strlen(jsonBuffer) + 1;
  client.publish(mqtt_discovery_topic, jsonBuffer, len, true);

i get an compile error message:

Users/itsmeMario/Downloads/mara_x_mqtt/mara_x_mqtt.ino: In function 'void publishDiscovery()':
/Users/itsmeMario/Downloads/mara_x_mqtt/mara_x_mqtt.ino:137:40: error: invalid conversion from 'char*' to 'const uint8_t*' {aka 'const unsigned char*'} [-fpermissive]
  137 |   client.publish(mqtt_discovery_topic, jsonBuffer, len, true);
      |                                        ^~~~~~~~~~
      |                                        |
      |                                        char*
In file included from /Users/itsmeMario/Downloads/mara_x_mqtt/mara_x_mqtt.ino:13:
Users/itsmeMario/Documents/Arduino/sketchbooks/libraries/PubSubClient/src/PubSubClient.h:154:55: note:   initializing argument 2 of 'boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)'
  154 |    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
      |                                       ~~~~~~~~~~~~~~~~^~~~~~~

exit status 1

Compilation error: invalid conversion from 'char*' to 'const uint8_t*' {aka 'const unsigned char*'} [-fpermissive]```

Ah, ignore me then. I see there is a version of publish in the client that should work with the original code:

   boolean publish(const char* topic, const char* payload, boolean retained);

Sorry I can’t help further. My next step would be to move the debug printout after the publish line, and then try to see what that publish function returns.

I have now done that.
It seems the code does not arrive at all in MQTT.

13:49:39.555 -> �D9KƜ�����
13:49:39.636 -> Verbinde mit WLAN...
13:49:40.268 -> ..........
13:49:45.497 -> WiFi verbunden
13:49:45.497 -> IP-Adresse: 
13:49:45.528 -> 192.168.1.129
13:49:45.528 -> Verbinde zu MQTT...verbunden
13:49:45.560 -> Publishing discovery message:
13:49:45.592 -> {"name":"LelitMaraX2","state_topic":"MaraX2/MaraX2Data","availability_topic":"MaraX2/availability","payload_available":"online","payload_not_available":"offline","device":{"identifiers":"LelitMaraX2","name":"Mara","model":"MaraX2","manufacturer":"Lelit"},"unique_id":"MaraX2mqtt"}
13:49:45.881 -> Publishing discovery message result: 0
13:49:45.913 -> C123b,045,000,039,0000,0
{"name":"LelitMaraX2","state_topic":"MaraX2/MaraX2Data","availability_topic":"MaraX2/availability","payload_available":"online","payload_not_available":"offline","device":{"identifiers":"LelitMaraX2","name":"Mara","model":"MaraX2","manufacturer":"Lelit"},"unique_id":"MaraX2mqtt"}

is 281 characters, the Arduino standard MQTT buffer is 255

here

you can find the supported abbreviations

I have shortened it to 224 characters but it still does not work.

14:58:56.990 -> Publishing discovery message:
14:58:57.022 -> {"name":"LelitMaraX2","stat_t":"MaraX2/MaraX2Data","avty_t":"MaraX2/availability","pl_avail":"online","pl_not_avail":"offline","device":{"ids":"LelitMaraX2","name":"Mara","mdl":"MaraX2","mf":"Lelit"},"unique_id":"MaraX2"}
14:58:57.248 -> Publishing discovery message result: 0

Does anyone else have any ideas?