Why is the MQTT integration in Home Assistant not receiving messages?

The MQTT integration appears unable to receive subscribed messages, preventing the light status from updating. Additionally, the “listening to topics” functionality in the MQTT configuration options seems to not work properly.

mqtt:
  light:
    - name: "Esp_esp01s"
      unique_id: "Esp_esp01s"
      optimistic: true
      qos: 1
      retain: true
      command_topic: "esp01s"
      state_topic: "esp01s/state"
      
      payload_on: '{"cmd":"ledon"}'
      payload_off: '{"cmd":"ledoff"}'
      state_value_template: >
        {% if value_json.cmd == 'ledon' %}
          {"cmd":"ledon"}
        {% elif value_json.cmd == 'ledoff' %}
          {"cmd":"ledoff"}
        {% endif %}
      
      brightness_command_topic: "esp01s"
      brightness_state_topic: "esp01s/state"
      brightness_command_template: '{"cmd":"ledpwm","value":{{value}}}'
      brightness_scale: 255
      brightness_value_template: >
        {{ value_json.value }}

Too little information to help.

Do your devices connect to the same MQTT broker as HA ?

Did you check with mqtt explorer ( https://mqtt-explorer.com/ ) on the MQTT broker HA is connected to ?

I have installed Home Assistant on both a chroot container and Hyper-Ubuntu, and encountered this issue in both environments. Even when using MQTT services without security authentication, the problem persists. I tested subscribed messages with MQTTX software, and the ESP device can receive/execute commands and successfully publish status messages to the “esp01s/state” topic. Did I miss any configuration steps?

Did you check this ?

Yes, the ESP device can receive MQTT messages sent by HA, but HA appears unable to receive subscribed messages. Both MQTT messages sent via the MQTTX software and the ESP device remain undetectable. The “Listening to Topics” feature in the MQTT integration fails to monitor any topics even when enabled.

I manually added a subscription for HA in the MQTT server console, but it was not successfully added.

You’re making your life harder than it should, here.

Replace simply by

      state_value_template: >
        {% if value_json.cmd == 'ledon' %}
          ON
        {% elif value_json.cmd == 'ledoff' %}
          OFF
        {% endif %}

and show us a screenshot of what is actually in the esp01s/state topic.

1 Like

I modified HA’s configuration file, but it still cannot recognize the ESP status.
Here is a screenshot of the MQTT messages: the “esp01s” topic messages are sent by HA, while the “esp01s/state” topic contains status information published by the ESP device.

I will try simpler code.

I’ve simplified HA’s configuration file to the most basic level, but the issue persists.

mqtt:
  light:
    - name: "Esp_esp01s"
      unique_id: "Esp_esp01s"
      optimistic: false
      qos: 1
      retain: true
      command_topic: "esp01s"
      state_topic: "esp01s/state"
      
      payload_on: on
      payload_off: off

MQTT message screenshot:

This is the MQTT log from HA:
2025-04-01 10:08:42.633 DEBUG (MainThread) [homeassistant.components.mqtt.client] g3f966ed.ala.cn-hangzhou.emqxsl.cn: register write 16
2025-04-01 10:08:42.633 DEBUG (MainThread) [homeassistant.components.mqtt.client] Transmitting retained message on esp01s: ‘{“cmd”:“ledon”}’, mid: 350, qos: 1
2025-04-01 10:08:42.633 DEBUG (MainThread) [homeassistant.components.mqtt.client] g3f966ed.ala.cn-hangzhou.emqxsl.cn: unregister write 16
2025-04-01 10:08:44.658 DEBUG (MainThread) [homeassistant.components.mqtt.client] g3f966ed.ala.cn-hangzhou.emqxsl.cn: register write 16
2025-04-01 10:08:44.659 DEBUG (MainThread) [homeassistant.components.mqtt.client] Transmitting retained message on esp01s: ‘{“cmd”:“ledoff”}’, mid: 351, qos: 1
2025-04-01 10:08:44.659 DEBUG (MainThread) [homeassistant.components.mqtt.client] g3f966ed.ala.cn-hangzhou.emqxsl.cn: unregister write 16
2025-04-01 10:08:46.084 DEBUG (MainThread) [homeassistant.components.mqtt.client] g3f966ed.ala.cn-hangzhou.emqxsl.cn: register write 16
2025-04-01 10:08:46.084 DEBUG (MainThread) [homeassistant.components.mqtt.client] Transmitting retained message on esp01s: ‘{“cmd”:“ledon”}’, mid: 352, qos: 1
2025-04-01 10:08:46.084 DEBUG (MainThread) [homeassistant.components.mqtt.client] g3f966ed.ala.cn-hangzhou.emqxsl.cn: unregister write 16

Seems you are using an external mqtt broker. Did you check your firewall ?

1 Like

The firewall is functioning normally, and the MQTTX software can detect both the status messages published by the ESP device and the command messages published by Home Assistant.

OK, I think I’ve identified the root cause of the issue. The problem was with my MQTT service provider. After switching to other providers and even trying a locally deployed MQTT service, everything is now functioning properly. :sweat: