Look at the payload in MQTT Explorer.
If the text is in color, the JSON is correct.
If not, there is a JSON error.
Next none of those topics are in the homeassistant/ name space. They appear to be in their own topics. In that case you need to set the entities in YAML and tell HA where to look.
Thanks for the answer but I am not sure I understand your points
The JSON I provided is correct
The discovery topic is homeassistant/select/backup_AAA_BBB_CCC/config(which is the namespace provided in the docyumentation and that works for the test cases I mentioned), the state topic can be anything (it does not have to be in the homeassistant namespace – when you look at what actually gets registered you see all kinds of conventions)
I fixed the problem, but I do not have a definite solution.
In my initial code I used a sub/pub approach to connecting to the MQTT broker. I connected, subscribed to a topic, checked the subscription and from then on was sending the messages (first discovery, then updates)
The messages were visible in MQTT Explorer in the correct place, but were not used by HA to create the entity.
I changed the approach and decided to just publish one-off (ad-hoc) messages. This is, of course, a very ineffective way, but I only publish individual messages every few hours so this is negligible.
And here, a big change: the messages are visible in MQTT Explorer in exactly the same place, structure and content as above, but this time HA correctly registers the entities.
Why is it so - I do not know. The broker has in both cases the correct, persistent topics published but only in one case HA decides to make use of them.
So my problem is fixed, this can be useful if someone stumbles upon the same issue but I am not happy with the outcome.
A pub/sub architecture is optimised for high volume and high velocity events (well, usually at least but then you can have fronts such as Kafka to buffer the flow). This is done by keeping a continuous connection between the client and the broker because the subscription is the most expensive operation.
When you go for ad-hoc messages, you have a synchronous sequence of connection setup → subscription → loop → message → ack → deconnection (again, in a synchronous, expensive mode). This is absolutely fine for setups such as mine, but would not fly for a heavy load.
In some way, the ad-hoc message bypasses the asynchronous nature of MQTT by constantly creating new event loops.
can’t help with the puzzle, just let me clarify:
it doesn’t work like this. It’s not a queue with a message to be consumed only once. The message is published to all clients subscribed at time of publishing.
You can publish to discovery without the retain flag and it will work. The downside is, the entities won’t be recreated after HA or mqtt restart. Thus it’s not advised.
though, what the OP is reporting is quite interesting.
I suspect some confusion between the discovery messages and state messages, here.
I think I noticed before that, although a discovery message exists, the HA entities are not actually created until a state message is published at least once. Not 100% sure, I haven’t done any tests to confirm that…
I’m sure state data are not needed to register entities. I worked with discovery 2 days ago, experiencing immediate entities creation with undefined state until I published the data.
It doesn’t mean, that cases where HA behaves differently are not possible.
This is indeed how HA works. As I mentioned elsewhere, the existence of the homeassistant/... entry is reflected in the existence of the entity.
When you remove it from the broeker, it disappears in HA.
This is also the reason why there is a persistent flag: it works without after a restart of the broker, but the entity is undefined until the next discovery message