Errors in the log

Hi All,
I see multiple instances of following error in the home assistant logs, not able to figure out what the issue is or how to troubleshoot, any help will be truly appreciated

2022-06-02 11:19:36 ERROR (MainThread) [homeassistant.util.logging] Exception in async_discover when dispatching 'mqtt_discovery_new_sensor_mqtt': ({'command': 'update', 'platform': 'mqtt'},)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 299, in async_discover
    config = schema(discovery_payload)
  File "/usr/local/lib/python3.9/site-packages/voluptuous/validators.py", line 232, in __call__
    return self._exec((Schema(val) for val in self.validators), v)
  File "/usr/local/lib/python3.9/site-packages/voluptuous/validators.py", line 355, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.9/site-packages/voluptuous/validators.py", line 351, in _exec
    v = func(v)
  File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 818, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 595, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 433, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: required key not provided @ data['state_topic']

I believe this is caused by a malformed MQTT discovery message. It doesn’t need to be a new message that some client sends, it could be an old one with the retain flag set.
Subscribe to homeassistant/# and try to find a message that tries to create an mqtt sensor without specifying state_topic. You can then get rid of it e.g. by executing mosquitto_pub -t homeassistant/bad/topic -n -r (with the appropriate -h host, username, password and so on).

Thanks @ondras12345, I have around 17000 topics, hard to find which one is causing the issue, Is there any way to clear the entire db, this is a mosquito broker running on raspberry pi4

I did another try, I subscribed as you suggested and published a blank message to all retained messages, and deleted the remaining, (cleared all topics under homeassistant/) then restarted HA, Still seeing this issue coming back,
Logger: homeassistant.util.logging
Source: util/logging.py:116
First occurred: 11:37:00 AM (159 occurrences)
Last logged: 11:46:06 AM

Try greping for the message:

mosquitto_sub -v -t "homeassistant/sensor/#" -W 2 | grep -v "state_topic"

@ondras12345 thanks again for the help, I am getting an error while trying the command that you sent

mosquitto_pub -h 127.0.0.1 -v -t "homeassistant/sensor/#" -W 2  -u user -P secret |grep -v "state_topic" 

Error: Unknown option ‘-v’

That’d be because you are running mosquitto_pub instead of mosquitto_sub.

Opps My mistake, now its timing out :frowning:

mosquitto_sub -h 127.0.0.1 -p 1883 -v -t "homeassistant/sensor/#" -W 2  -u user -P secret |grep -v "state_topic" 

I’m not sure what you mean by it timing out.
-W 2 means mosquitto_sub should exit after 2 seconds. That should be enough to receive retained messages.
Please post the verbatim output of the command.

Just to note, a very useful tool to check and manipulate MQTT messages (or their retaining flag) is

If you have a topic with a retain flag, you can just delete the topic in MQTT-Explorer and it’s really gone. :slight_smile: