Hi,
I’m trying to create a new sensor with MQTT. HA can get the information.
But does NOT create the sensor.
Am I doing something wrong ?
Hi,
I’m trying to create a new sensor with MQTT. HA can get the information.
But does NOT create the sensor.
Am I doing something wrong ?
From the documentation on MQTT Discovery at: MQTT Discovery - Home Assistant
It states for the config-topic the following format:
<discovery_prefix>/<component>/[<node_id>/]<object_id>/config
The node_id
is optional, but object_id
should be used.
Your configuration is now:
topic: homeassistant/sensor/dht_temperature/config
{
"name": "dht_temperature",
"state_class": "measurement",
"unit_of_measurement": "°C",
"device_class": "temperature",
"state_topic": "homeassistant/sensor/dht_temperature/state"
}
Based on what I have got working, after a lot (!) of trying I would try :
topic: homeassistant/sensor/dht_temperature/config
{
"name": "My DHT-Temperature Sensor",
"unique_id":"dht_temperature",
"object_id":"dht_temperature",
"state_class": "measurement",
"unit_of_measurement": "°C",
"device_class": "temperature",
"state_topic": "homeassistant/sensor/dht_temperature/state"
}
I left in your line
"state_class": "measurement",
But I do not use it. It might have to be removed, if it still does not work.
And then send values for the temperature to homeassistant/sensor/dht_temperature/state
, which I believe you already did successfully.
You now have like a Formal name on the basis of which normally the entity_id
is made inside HA.
To have some friendly name for reference one can use object_id
instead which then should be exactly like the one in your json-string. I have specified that formal name for both the unique_id
and object_id
, just to take the guesswork out what HA comes up with for the Entity-ID
. That leaves the name
for something more poetic.
So I would use your dht_temperature
for those and use the name
for something else. What then is important is to use that in the config-topic, which you already did.
Maybe one of the two (object_id
/ unique_id
) can be left out. For me it works fine like this.
If you have more than one sensor I would go for object_id
to be dht_temperature_1
, dht_temperature_2
. So it is expandable in the future. But then note to also use that one literally in the config-topic
I guess you are already using the MQTT Integration
http://<your_ip>:8123/config/integrations
There you can see messages coming in. And you can also fire off Messages to try to find the right format.
In the process I found out that the Logs give some information of what goes wrong with the MQTT Discovery although it is not very explanatory. But at least you know that it arrived, but was not acceptable.
http://<your_ip>:8123/config/logs
PS: It took me a lot of coffee to get it finally working, so take some extra on that front.
Add unique_id
to the discovery payload. Set the value to whatever you want (can be identical to what you are currently using for name
).
Well, good. And did you use object_id
as well? For me it is was not clear how they unique_id
and object_id
relate to each other.
These kind of explanations in the documentation got me puzzled:
Best practice for entities with a `unique_id` is to set `<object_id>` to `unique_id` and omit the `<node_id>`
I started out with no knowledge of these names and it is difficult to understand the difference between unique_id
and object_id
. Maybe one is a synonym for the other but they are both heavily used in the explanations. Just to be sure I put the both in the json-string.
I already wrote ‘the people’ of HA concerning this confusion. It is good to know from you that this is indeed confusing.
But what do you use in the config-topic then. There it is supposed to be the object_id
as part of the topic-path. Consequently I was asking myself when it is part of the config-topic, why does it need a unique_id
or object_id
as well in the JSON-String, since it is already mentioned and can only lead to problems by specifying this twice. object_id
could be used as unique_id
by HA since it is already mentioned in the config-topic. But for some reason the name
value is then used to create the entity_id
and it is better to use name
for something more poetic than a tipically boring formal id-name.
What I did is just also include it in the JSON-String, which I believe is OK to do, as far I could overlook the log-files not complaining about it. I can thus leave it (object_id
) out of the JSON String apparently according to you and the asker of the question.