MQTT autodiscovery problems

Hi,

I’m struggling with MQTT auto-discovery. I’ve read the documentation, and all of the posts I could find about similar problems, and still nothing.

I’ve got a topic emon/power/import that carries numeric data. I’m advertising this to Home Assistant using a retained message published to homeassistant/sensor/emon/power-import/config, with the payload

{
    "device_class" : "power",
    "name" : "Imported power",
    "state_topic" : "emon/power/import",
    "unit_of_measurement" : "W"
}

I have verified that the MQTT integration is connected to the broker, and subscribing to homeassistant/# from within Home Assistant regurgitates the retained configuration message immediately, as expected. I have verified that discovery is enabled in the MQTT integration configuration. But nothing happens - no entities or devices are created, nothing.

I’m not sure what to try next. Can I get any more debug information from Home Assistant, which appears to be receiving and ignoring my discovery messages?

In case it’s important I’m running Home Assistant Container, and the info screen reports version 2021.2.0.dev20210119.

Any pointers gratefully received…

Something isn’t quite as perfect as you reported because when I publish the same payload to the same topic I instantly get a sensor created:

Screenshot from 2021-04-20 18-42-33

Basically, I’ve confirmed there’s nothing wrong with your choice of discovery topic or discovery payload. What it suggests is that something may be amiss elsewhere.

I assume you’ve already seen this in the MQTT integration and it’s enabled:

Screenshot from 2021-04-20 18-50-43

Plus you confirmed that you saw the discovery payload published while monitoring it here:

Screenshot from 2021-04-20 18-53-32

Is that correct?

Absolutely correct on both counts:


Please excuse the weirdly joined image, as a new user I’m not allowed to post more than one picture. Note that there are actually 6 sensors, I just chose one for the purposes of the original question, and a different one is visible at the top of the screenshot.

After a night’s sleep though it did finally occur to me to check the logs :man_facepalming: and I found this:

Exception in discovery_done when dispatching 'mqtt_discovery_done_('sensor', 'emon power-import')': (None,) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/discovery.py", line 170, in discovery_done pending = hass.data[PENDING_DISCOVERED][discovery_hash]["pending"] KeyError: ('sensor', 'emon power-import') 

Edit: this is the better-formatted version from the log file on the container itself:

2021-04-21 08:10:11 ERROR (MainThread) [homeassistant.util.logging] Exception in discovery_done when dispatching 'mqtt_discovery_done_('sensor', 'emon power-import')': (None,)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/discovery.py", line 170, in discovery_done
    pending = hass.data[PENDING_DISCOVERED][discovery_hash]["pending"]
KeyError: ('sensor', 'emon power-import')

This seems relevant. Unfortunately, though I’m a reasonable Python programmer, I’m not sufficiently familiar with the internals of Home Assistant to have any idea what it means.

Any ideas?

I might have the same issue as yours … look at this thread here : Multiple MQTT topics in one entity - #4 by catmembert

try
in config.yaml:

mqtt:
  discovery: true
  discovery_prefix: homeassistant

Well, how unsatisfactory. I’m sorry to say that it now works, without me doing anything at all - I was messing with something else entirely and suddenly my entity list grew significantly. In fact I got multiple copies of each entity, which took me a while to sort out.

I wish I’d been able to get to the bottom of this. “It works fine now” is fine as an outcome, but understanding what was wrong would have been significantly more satisfactory. Just to clarify, I didn’t change anything at all. It’s possible that I did a restart of Home Assistant - I certainly have done so since.

Not really a “Solution”, is it?

The whole idea of a Solution post is that it explains how to solve the problem. Unless the solution here is ‘restart and cross your fingers’. :man_shrugging:

FWIW, if the error message ever recurs, you should report it in the GitHub Core repository.

Well ok, but “restart and cross my fingers” appears to be exactly what I did, and there is now no opportunity to gather any more debugging data so there’s no chance of fixing whatever the underlying problem was.

On some forums I’m used to, marking the thread as solved would be encouraged so as not to waste the time of people looking to help. On others, it would be discouraged so as not to waste the time of other people looking for solutions. Whatever is the accepted practice here I will happily go along with it. Would you rather I removed the “Solution” mark?

It’s your topic so you are free to mark whatever post you want as the Solution.

Having spent a few years here, my understanding is that the Solution tag isn’t the either/or situation you described but both; it indicates the problem is solved and shows how to fix it.

In this case, the symptoms simply disappeared, the root cause is unknown, and there are no instructions to help anyone else fix it.

2 Likes

Well then you’ve spent a few more years here than me. :slight_smile: I’ve unmarked it.

To anyone coming here because they had the same problem: sorry, no solution! Try restarting and crossing your fingers. :wink:

1 Like

I had problem, when auto-discovery didn’t work with examples from manual, but zwave2mqtt worked perfectly fine,
so i took one of topics and modified to my needs:

{
  "state_topic": "ha/sensor/sensorLivingroom/state",
  "value_template": "{{ value_json.temperature }}",
  "device": {
    "identifiers": [
      "rpisensorgateway"
    ],
    "manufacturer": "Raspberry",
    "model": "RPI 3B",
    "name": "Livingroom temperature",
    "sw_version": "MCU9808"
  },
  "name": "Livingroom temperature",
  "unique_id": "rpisensorgateway_0xc191322e"
}

I think it has something to do with unique ID
it works fine now.