Mqtt auto-discovery from external mqtt msg (Entity works, Device does not)

I see quite a few posts on this topic. After studying all, I still am no closer to solving it.

I want to publish auto-discovery info from switches etc from a separate Node-RED system, to register switches etc. The example below is for an irrigation system built on Node-RED.

Publish topic: homeassistant/switch/Irrigation/config
Publish payload:

{“name”:“Garden”,
“command_topic”:“homie/rh02/L1/set”,
“state_topic”:“homie/rh02/L1”,
“device”:{“identifiers”:“14253c35”,“manufacturer”:“Raspberry Pi”,“model”:“RPi 3 Model B Rev 1.2”,“name”:“rh02”}
}

When publishing the above, nothing happens. I tried it with auto-discovery set in the configuration.yaml, and without - as the documentation says it is on by default.

I also tested the message with the topic on Configuration > Integration > MQTT > CONFIGURE > Publish a packet on the topic, and listening to it - it does publish.

What else should I try?

Using Developer Tools > Services, I published your payload to the desired discovery topic. The only difference is that I changed its name because I happen to already have a switch.garden entity.

service: mqtt.publish
data:
  topic: homeassistant/switch/Irrigation/config
  payload_template: >
    {"name":"Garden Test",
    "command_topic":"homie/rh02/L1/set",
    "state_topic":"homie/rh02/L1",
    "device":{"identifiers":"14253c35","manufacturer":"Raspberry Pi","model":"RPi 3 Model B Rev 1.2","name":"rh02"}
    }

The entity was instantly created and displayed in Developer Tools > States:
Screenshot from 2021-03-22 17-00-15

So the good news is that there’s nothing wrong with the payload’s structure. Try repeating the steps I just described and see if it works for you as well.

Yeah, it works. But I do not see the device in Configuration > Devices, neither in > Entities.

I switched on logging, and this is whah I get:

2021-03-22 23:13:29 DEBUG (MainThread) [homeassistant.components.mqtt.discovery] Process discovery payload {‘name’: ‘Garden’, ‘command_topic’: ‘homie/rh02/L1/set’, ‘state_topic’: ‘homie/rh02/L1’, ‘device’: {‘identifiers’: ‘14253c35’, ‘manufacturer’: ‘Raspberry Pi’, ‘model’: ‘RPi 3 Model B Rev 1.2’, ‘name’: ‘rh02’}, ‘platform’: ‘mqtt’}

2021-03-22 23:13:29 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: switch Irrigation, sending update

2021-03-22 23:13:29 INFO (MainThread) [homeassistant.components.mqtt.mixins] Got update for entity with hash: (‘switch’, ‘Irrigation’) ‘{‘name’: ‘Garden’, ‘command_topic’: ‘homie/rh02/L1/set’, ‘state_topic’: ‘homie/rh02/L1’, ‘device’: {‘identifiers’: ‘14253c35’, ‘manufacturer’: ‘Raspberry Pi’, ‘model’: ‘RPi 3 Model B Rev 1.2’, ‘name’: ‘rh02’}, ‘platform’: ‘mqtt’}’

According to the log it looks like it should appear in Devices & Entities, but does not?

No, wait, switch.garden appreared, but not the device.

And, I want it to work with an mqtt message, not using Developer Tools.

Actually - sending the mqtt message from Node-RED DOES work - for the Entity only !!!

I think my issue is - why does it not also create the Device?

You’re right and that’s because of a small mistake in the payload’s device definition. For identifiers, the value’s type must be a list.

service: mqtt.publish
data:
  topic: homeassistant/switch/Irrigation/config
  payload_template: >
    {"name":"Garden Test", "command_topic":"homie/rh02/L1/set",
    "state_topic":"homie/rh02/L1",
    "device":{"identifiers":["14253c35"],"manufacturer":"Raspberry
    Pi","model":"RPi 3 Model B Rev 1.2","name":"rh02"} }

re-publishing the corrected payload generates a device:

You may have misunderstood the purpose of the exercise. The goal was to confirm the payload’s validity. What is used to publish the payload is not important; Developer Tools > Services is just a handy way to do it. Now you can use whatever you want, including Node-Red, to publish the corrected payload.

Be advised that for the purpose of this exercise I did not publish the payload as a retained message but, for production use, you should. If you don’t, the discovered entity will disappear after Home Assistant restarts.

1 Like

Ahhhh, the LIST in identifiers, thank you !!!

1 Like

I cannot seam to replicate the creation of a device & entity - like I am - with your help - do it with ** Developer Tools > Services ** - with an MQTT message from Node-RED.

Node-RED creates the Entity, but not the Device. Here is my Node-RED mqtt message again:

Topic: homeassistant/switch/Irrigation/config
Payload:

{“name”:“Irrigation_L1”,“command_topic”:“homie/rh02/L1/set”,“state_topic”:“homie/rh02/L1”,“device”:{“identifiers”:[“14253c35”],“manufacturer”:“Raspberry Pi”,“model”:“RPi3B_Rev1.2”,“name”:“RPi_rh02”}}

Wonder if this is a bug?

Okay - I got the answer, it needs to have an unique_id as well, else it does not work !

So, to others, the payload now is:

{“name”:“Irrigation_L1”,“command_topic”:“homie/rh02/L1/set”,“state_topic”:“homie/rh02/L1”,“unique_id”:“Irri_Ch1”,“device”:{“identifiers”:[“14253c35”],“manufacturer”:“Raspberry Pi”,“model”:“RPi3B_Rev1.2”,“name”:“RPi_rh02”}}

Then the Entity will also be published.

You have misunderstood the purpose of the Solution tag. It is for marking the post that answers/resolves the original question/problem. It helps other users who are seeking answers to similar questions. The original problem was the device was not created and I identified, and explained, the cause to be a malformed payload (wrong value type). You had correctly marked that post as containing the Solution.

Subsequently you tagged another post as the Solution but it neither identifies nor explains anything (in fact, it asks another question related to Node-Red).

unique_id is not required for MQTT Discovery. The proof is that I didn’t use it and the entity was created (see screenshot above). That option serves a completely different purpose, entirely unrelated as to whether an entity is created (or not) via MQTT Discovery.

To be clear, where it is required is for associating it to a device. However, that’s a separate issue from MQTT Discovery (and not everyone needs discovered entities to be associated with a device). Anyway, that’s a separate issue from the original question.


For future reference, when you post JSON strings in this forum, it’s important to format them correctly. Every one of your posts contains malformed JSON because it uses fancy quotes instead of standard quotes. It makes it tedious for anyone trying to help you because they first have to correct all the quotes before they can use it for testing purposes.

When you paste code or JSON strings, format it using the </> icon in Editor’s menu.

Dear @123, without your help, I would definitely not bing able to solve this. And, I was also learning the problem as I was learning the solution. You are correct, it did work for one entity - without the unique_id -= it was when I escalated the solution to its extent I wanted - which I did not know at the beginning, adding more entities that I realised that one needs it.

And you are correct, I should probably have closed this request, and opened a new one - because your solution was the right ons - for that question (prior to throwing more entities at it).

I apologies.

Also, thanks for the pointers on show in json - did not know that !

I started with HASS 2 weeks ago - have put in tonnes of hours to get going, have scratched the config 4 times already, so busy ramping up my knowledge - for HASS - I am a NOOB!!!

Apologies again - and thank you for steering me to the right solution.

Jéan.

You’re welcome!

Don’t forget to mark the correct post with the Solution tag. At the moment, the post you have marked as the Solution (post #8) doesn’t actually contain a solution.

Done. Is it correct?

1 Like