Home Assitant - Device Registration MQTT Addon Broken broken?

Hi guys,
I currently have a problem with the device and entity registration in Home Assitant via MQTT(Mosquito Add-ON).
In my example I register 4 devices in Home Assitant:

    summary_device = {
        "name": f"DKB Zusammenfassung {model}",
        "identifiers": f"dkb_summary_{model}",
        "manufacturer": "Justin Hahn",
        "model": model
    }

    activities_device = {
        "name": f"DKB Aktivitäten {model}",
        "identifiers": f"dkb_activities_{model}",
        "manufacturer": "Justin Hahn",
        "model": model
    }

The whole function runs in a loop and {Model} differs in the runs, so there is no problem that the ID is not unique
The curious thing is that only 2 devices are registered in Home Assistant (in my case the loop runs through twice, but only the devices from the first run are registered).
Now comes the strange thing.
If I run the whole programme once, I only have 2 devices and their entities in Home Assitant, which does not change even if I run the programme 10 times. That is logical so far. But if I now change the identifiers of both devices (e.g. I write _1 in the end) then I suddenly have 6 devices in HomeAssitant, which means that suddenly all 4 devices are registered. If I then delete all devices from HomeAssitant again and start the programme with the changed identifiers, there are only 2 devices again, so I have to do the following to register my 4 devices:

  1. run the programme once (2 devices are registered)
  2. change the identifiers (4 devices are registered)
  3. delete the 2 now superfluous devices.
    I am not very familiar with MQTT but I think that this behaviour is not normal, or is it intentional?
    I have also made a video about this because the whole situation is very confusing and I may not have explained it here so that everyone understands it:
    https://youtu.be/7004sN_BKGc
    I have also made all my code public here.
    In the logs of the MQTT PLugin there is nothing that really helps me (thought there might be an error why only 2 devices are registered).
    I’ve also thought about opening an isseu on Github but maybe this behaviour is intentional (even if I couldn’t imagine why it should be).
    Thank you in advance for your answers.
2024-06-15 00:51:50: New connection from 172.30.32.1:52403 on port 1883.
2024-06-15 00:51:50: New client connected from 172.30.32.1:52403 as 3OEpiKXoq9nlbFNXHxn6lM (p2, c1, k60, u'homeassistant').
2024-06-15 00:52:11: New connection from 217.87.170.124:64390 on port 8883.
2024-06-15 00:52:11: New client connected from 217.87.170.124:64390 as auto-EBCDBFAD-756A-9ADF-BF4B-9F873354EBE9 (p2, c1, k60, u'mqtt_test').
2024-06-15 00:52:11: Client auto-EBCDBFAD-756A-9ADF-BF4B-9F873354EBE9 disconnected.
2024-06-15 00:52:11: New connection from 217.87.170.124:64393 on port 8883.
2024-06-15 00:52:11: New client connected from 217.87.170.124:64393 as auto-CA234FCE-C08F-3408-7301-5F46163537CB (p2, c1, k60, u'mqtt_test').
2024-06-15 00:52:11: Client auto-CA234FCE-C08F-3408-7301-5F46163537CB disconnected.
2024-06-15 00:52:14: New connection from 172.30.32.1:51637 on port 1883.
2024-06-15 00:52:14: New client connected from 172.30.32.1:51637 as 4m0LtlDdcyrFuTPX40FLeg (p2, c1, k60, u'homeassistant').
2024-06-15 00:52:40: New connection from 217.87.170.124:64421 on port 8883.
2024-06-15 00:52:40: New client connected from 217.87.170.124:64421 as auto-DEE7F711-4BDA-9ADF-2DCB-E6BF18EB7DEB (p2, c1, k60, u'mqtt_test').
2024-06-15 00:52:40: Client auto-DEE7F711-4BDA-9ADF-2DCB-E6BF18EB7DEB disconnected.
2024-06-15 00:52:41: New connection from 217.87.170.124:64425 on port 8883.
2024-06-15 00:52:41: New client connected from 217.87.170.124:64425 as auto-A5FC5FE4-D229-F506-5148-587C78ABA46D (p2, c1, k60, u'mqtt_test').
2024-06-15 00:52:41: Client auto-A5FC5FE4-D229-F506-5148-587C78ABA46D disconnected.
2024-06-15 00:52:46: New connection from 172.30.32.1:54117 on port 1883.
2024-06-15 00:52:46: New client connected from 172.30.32.1:54117 as 0POXj0icSnPkY2RQrZL167 (p2, c1, k60, u'homeassistant').
s6-rc: info: service legacy-services: stopping
[22:53:04] INFO: Service restart after closing
2024-06-15 00:53:04: mosquitto version 2.0.18 terminating
2024-06-15 00:53:04: Saving in-memory database to /data//mosquitto.db.
[22:53:04] INFO: Service restart after closing
s6-rc: info: service legacy-services successfully stopped
s6-rc: info: service legacy-cont-init: stopping
s6-rc: info: service legacy-cont-init successfully stopped
s6-rc: info: service fix-attrs: stopping
s6-rc: info: service fix-attrs successfully stopped
s6-rc: info: service s6rc-oneshot-runner: stopping
s6-rc: info: service s6rc-oneshot-runner successfully stopped
s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
cont-init: info: running /etc/cont-init.d/mosquitto.sh
[00:53:21] INFO: Certificates found: SSL is available
cont-init: info: /etc/cont-init.d/mosquitto.sh exited 0
cont-init: info: running /etc/cont-init.d/nginx.sh
cont-init: info: /etc/cont-init.d/nginx.sh exited 0
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
services-up: info: copying legacy longrun mosquitto (no readiness notification)
services-up: info: copying legacy longrun nginx (no readiness notification)
[00:53:21] INFO: Starting NGINX for authentication handling...
s6-rc: info: service legacy-services successfully started
[00:53:22] INFO: Starting mosquitto MQTT broker...
2024-06-15 00:53:22: Warning: Mosquitto should not be run as root/administrator.
2024-06-15 00:53:22: mosquitto version 2.0.18 starting
2024-06-15 00:53:22: Config loaded from /etc/mosquitto/mosquitto.conf.
2024-06-15 00:53:22: Loading plugin: /usr/share/mosquitto/go-auth.so
2024-06-15 00:53:22:  ├── Username/password checking enabled.
2024-06-15 00:53:22:  ├── TLS-PSK checking enabled.
2024-06-15 00:53:22:  └── Extended authentication not enabled.
2024-06-15 00:53:22: Opening ipv4 listen socket on port 1883.
2024-06-15 00:53:22: Opening ipv6 listen socket on port 1883.
2024-06-15 00:53:22: Opening websockets listen socket on port 1884.
2024-06-15 00:53:22: Opening ipv4 listen socket on port 8883.
2024-06-15 00:53:22: Opening ipv6 listen socket on port 8883.
2024-06-15 00:53:22: Opening websockets listen socket on port 8884.
2024-06-15 00:53:22: mosquitto version 2.0.18 running
2024-06-15 00:53:22: New connection from ::1:55680 on port 1883.
2024-06-15 00:53:22: Client <unknown> disconnected due to protocol error.
[00:53:23] INFO: Successfully send discovery information to Home Assistant.
[00:53:23] INFO: Successfully send service information to the Supervisor.
2024-06-15 00:53:28: New connection from 217.87.170.124:64443 on port 8883.
2024-06-15 00:53:28: New client connected from 217.87.170.124:64443 as 897aac36 (p2, c1, k15, u'justins_pc_mqtt').
2024-06-15 00:53:29: New connection from 172.30.32.1:52679 on port 1883.
2024-06-15 00:53:29: New client connected from 172.30.32.1:52679 as 4dACVaaX45wJDKcRua21A0 (p2, c1, k60, u'homeassistant').
2024-06-15 00:53:29: Client 4dACVaaX45wJDKcRua21A0 disconnected.
2024-06-15 00:53:29: New connection from 172.30.32.1:46353 on port 1883.
2024-06-15 00:53:29: New client connected from 172.30.32.1:46353 as 06g9o2oSgOJlY8jh5qmO5a (p2, c1, k60, u'homeassistant').
2024-06-15 00:53:37: New connection from 217.87.170.124:58154 on port 8883.
2024-06-15 00:53:37: New client connected from 217.87.170.124:58154 as DVES_AAE670 (p2, c1, k30, u'stromzähler_mqtt').
2024-06-15 00:53:40: New connection from 217.87.170.124:64463 on port 8883.
2024-06-15 00:53:40: New client connected from 217.87.170.124:64463 as auto-1AEA5DC8-82DE-268B-4653-B5AC20C9E129 (p2, c1, k60, u'mqtt_test').
2024-06-15 00:53:40: Client auto-1AEA5DC8-82DE-268B-4653-B5AC20C9E129 disconnected.
2024-06-15 00:53:40: New connection from 217.87.170.124:64467 on port 8883.
2024-06-15 00:53:40: New client connected from 217.87.170.124:64467 as auto-060EEAA0-B8C8-0863-5BD7-9FED304C5E50 (p2, c1, k60, u'mqtt_test').
2024-06-15 00:53:40: Client auto-060EEAA0-B8C8-0863-5BD7-9FED304C5E50 disconnected.
2024-06-15 00:53:46: New connection from 172.30.32.1:45053 on port 1883.
2024-06-15 00:53:46: New client connected from 172.30.32.1:45053 as 20UtPFxgpU8LmXjxAEtzlx (p2, c1, k60, u'homeassistant').
2024-06-15 00:54:02: New connection from 217.87.170.124:64489 on port 8883.
2024-06-15 00:54:02: New client connected from 217.87.170.124:64489 as auto-C1454D53-C009-A681-9827-0EFAF69E9BB8 (p2, c1, k60, u'mqtt_test').
2024-06-15 00:54:02: Client auto-C1454D53-C009-A681-9827-0EFAF69E9BB8 disconnected.
2024-06-15 00:54:02: New connection from 217.87.170.124:64492 on port 8883.
2024-06-15 00:54:02: New client connected from 217.87.170.124:64492 as auto-93CD3615-ACC5-360E-66C1-80870C910B91 (p2, c1, k60, u'mqtt_test').
2024-06-15 00:54:02: Client auto-93CD3615-ACC5-360E-66C1-80870C910B91 disconnected.
2024-06-15 00:54:05: New connection from 172.30.32.1:34849 on port 1883.
2024-06-15 00:54:05: New client connected from 172.30.32.1:34849 as 7ycUhqNk5KAaJNPwEfznMa (p2, c1, k60, u'homeassistant').
2024-06-15 00:54:20: New connection from 217.87.170.124:64513 on port 8883.
2024-06-15 00:54:20: New client connected from 217.87.170.124:64513 as auto-A49AEF5A-1E79-E75E-3801-66915040596D (p2, c1, k60, u'mqtt_test').
2024-06-15 00:54:20: Client auto-A49AEF5A-1E79-E75E-3801-66915040596D disconnected.
2024-06-15 00:54:21: New connection from 217.87.170.124:64516 on port 8883.
2024-06-15 00:54:21: New client connected from 217.87.170.124:64516 as auto-42AE8BDC-A389-9056-A8AC-D0FC227DDDF2 (p2, c1, k60, u'mqtt_test').
2024-06-15 00:54:21: Client auto-42AE8BDC-A389-9056-A8AC-D0FC227DDDF2 disconnected.
2024-06-15 00:54:26: New connection from 172.30.32.1:44471 on port 1883.
2024-06-15 00:54:26: New client connected from 172.30.32.1:44471 as 7I5Ec3LbYSdPE7PvkCYLsp (p2, c1, k60, u'homeassistant').
2024-06-15 00:55:21: Client 06g9o2oSgOJlY8jh5qmO5a has exceeded timeout, disconnecting.
2024-06-15 00:55:39: Client 20UtPFxgpU8LmXjxAEtzlx has exceeded timeout, disconnecting.
2024-06-15 00:55:57: Client 7ycUhqNk5KAaJNPwEfznMa has exceeded timeout, disconnecting.

Hi,
Let’s unravel what is going on here:

  1. You are writing client device code in Python, which connects to a MQTT broker via the paho.mqtt.client. (I had to go to the Github page.)
  2. You are using HASS Discovery, so by publishing specific topics to the MQTT broker, HASS creates entities automatically.
  3. There seems to be a loop which registers two devices, twice.
  4. The second loop iteration does not register +2 devices.

As I’m not keen to read and understand many lines of Python, my advice is to break this down.

What I did when writing a complex client device in Python using the same Paho libraries and HASS Discovery, was to create a test rig on the command line. This allows FAST and IMMEDIATE testing.

Prototype what you want in a script on the command line, check it instantly configures HASS, and THEN write code. You’ll find misunderstandings in the discovery paylods much faster.

Here’s an explanation and example:

Note that entity naming changed slightly in 2023.8, which impacts HASS Discovery naming;

The Mosquitto broker log only shows the client MQTT connections coming and going - not what the MQTT integration is doing with the payloads as it interprets HASS Discovery.

I’d suggest MQTT Explorer to dump the actual HASS Discovery payload topics so we can interpret what is going on.

If this helps, :heart: this post!