Unable to catch Mosquitto MQTT Events from event listener

Hello! :slight_smile: I’ve been having issues trying to capture MQTT events from my Mosquitto broker. I can confirm messages are arriving by checking the Mosquitto configuration page. I also set up an external MQTT client app to listen for those topics. Publishing a test message straight from the configuration console generates a call_service response, but not the MQTT_MESSAGE event that I’m looking for. I’m thinking it’s related to a configuration issue, but I’ve had no luck getting past this point. Any thoughts or suggestions would be greatly appreciated. :grin:

Quick edit:
I’ve also tried listening to events with the appropriate event type and namespace specified for mqtt, however I’ve seen no responses whatsoever doing this. Sorry, should’ve made that clear in my initial post.

self.listen_event(self.msg_callback, "MQTT_MESSAGE", namespace = "mqtt")

appdaemon.yaml

secrets: /config/secrets.yaml
appdaemon:
  plugins:
    HASS:
      type: hass
    MQTT:
      type: mqtt
      namespace: mqtt
      client_host: core-mosquitto
      user_name: user
      client_password: pass
      event_name: MQTT_MESSAGE
http:
  url: http://127.0.0.1:5050
admin:
api:
hadashboard:
logs:
  main_log:
    filename: /config/appdaemon/logs/appdaemon.log
  error_log:
    filename: /config/appdaemon/logs/error.log
  access_log:
    filename: /config/appdaemon/logs/access.log
  diag_log:
    filename: /config/appdaemon/logs/diag.log

lora.py

import hassapi as hass
import mqttapi as mqtt

class LoRaApp(mqtt.Mqtt, hass.Hass):
  def initialize(self):
    self.log("Initializing LoRa app")
    self.call_service("mqtt/subscribe", namespace = "mqtt", topic = "application/#")

    self.listen_event(self.msg_callback)
    self.mqtt_publish("application/1/light", "ON")
    self.log("Initialization complete")

  def msg_callback(self, event_name, data, kwargs):
    self.log("Test Message received " + event_name)
    self.log(data)

  def join_callback(self, event_name, data, kwargs):
    self.log("Join message received")

appdaemon.log

2021-05-25 17:20:40.679037 INFO AppDaemon: Starting API
2021-05-25 17:20:40.700453 INFO AppDaemon: Starting Admin Interface
2021-05-25 17:20:40.705472 INFO AppDaemon: Starting Dashboards
2021-05-25 17:20:40.877127 INFO HASS: Connected to Home Assistant 2021.5.5
2021-05-25 17:20:40.995012 INFO MQTT: Connected to Broker at URL core-mosquitto:1883
2021-05-25 17:20:41.292628 INFO HASS: Evaluating startup conditions
2021-05-25 17:20:41.364217 INFO HASS: Startup condition met: hass state=RUNNING
2021-05-25 17:20:41.368526 INFO HASS: All startup conditions met
2021-05-25 17:20:41.805619 INFO AppDaemon: Got initial state from namespace mqtt
2021-05-25 17:20:41.823738 INFO MQTT: MQTT Plugin initialization complete
2021-05-25 17:20:41.828030 INFO AppDaemon: Got initial state from namespace default
2021-05-25 17:20:41.841452 INFO AppDaemon: App 'hello_world' added
2021-05-25 17:20:42.799457 INFO AppDaemon: App 'lora_app' added
2021-05-25 17:20:42.805265 INFO AppDaemon: Found 2 total apps
2021-05-25 17:20:42.808998 INFO AppDaemon: Starting Apps with 2 workers and 2 pins
2021-05-25 17:20:42.814690 INFO AppDaemon: Running on port 5050
2021-05-25 17:20:43.843105 INFO AppDaemon: Scheduler running in realtime
2021-05-25 17:20:43.856465 INFO AppDaemon: Adding /config/appdaemon/apps to module import path
2021-05-25 17:20:43.869543 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/hello.py
2021-05-25 17:20:43.921649 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/lora.py
2021-05-25 17:20:45.032799 INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
2021-05-25 17:20:45.048854 INFO AppDaemon: Initializing app lora_app using class LoRaApp from module lora
2021-05-25 17:20:47.558678 INFO hello_world: Hello from AppDaemon
2021-05-25 17:20:47.566813 INFO hello_world: You are now ready to run Apps!
2021-05-25 17:20:47.583225 INFO lora_app: Initializing LoRa app
2021-05-25 17:20:47.696264 INFO lora_app: Initialization complete
2021-05-25 17:20:47.705482 INFO AppDaemon: App initialization complete
2021-05-25 17:20:48.773850 INFO lora_app: Test Message received call_service
2021-05-25 17:20:49.881943 INFO lora_app: {'domain': 'mqtt', 'service': 'publish', 'service_data': {'topic': 'application/1/light', 'payload': 'ON'}, 'metadata': {'origin': 'LOCAL', 'time_fired': '2021-05-26T00:20:47.648233+00:00', 'context': {'id': '8d59de088cf986252c45750c81415f13', 'parent_id': None, 'user_id': '6ecf68d6bc334414966220ca2358715e'}}}

If anyone’s interested, the issue I had disappeared the moment I set up my own custom client_id within the MQTT configuration. Not sure how that caused the issue. :thinking:

Reopening this topic.
I have a similar situation.
I have EMQX as mqtt broker.
I see it is correctly connected in appdaemon:
State Plugins
plugin.MQTT active 0 52.6 0B/s 5.89K/s

my configuration is as follows:

    MQTT:
      type: mqtt
      namespace: mqtt
      client_host: 192.168.2.100
      client_id: appdaemon
      client_user: admin
      client_password: mypass
      birth_topic: appdaemon
      will_topic: appdaemon
      event_name: MQTT_MESSAGE

and here is my app (so far):

import hassapi as hass
import json
import mqttapi as mqtt
from datetime import datetime, timedelta

MQTT_MSG="MQTT_MESSAGE"

class scrypted2ha(hass.Hass):
  def initialize(self):
    """Read parameters."""
    try:
        self.loglevel = self.args.get("loglevel",3)
    except:
      self.log("Error opening parameters!")
      return
    self.mylog("*************************",0) 
    self.mylog("Initializing scrypted2ha.",0) 
    self.mylog("*************************",0) 
    self.mylog("Log Level: {}".format(self.loglevel),0)
    try:
      self.prefix = self.args.get("prefix")
      self.camera = self.args.get("camera")
      self.objects = list(self.args["objects"])
    except:
      self.log("Error opening parameters")
      return
    
    self.mylog("*************************",0) 
    self.mylog("Prefix: {}.".format(self.prefix),3)
    self.mylog("Camera: {}.".format(self.camera),3)
    self.mylog("Objects to report: ".format(self.prefix),3)
    for object in self.objects:
      self.mylog("Object: {}".format(object),3)
    self.mylog("Sensors Created: sensor.scrycam_{}".format(self.camera),3)

    """Set topic Listener"""
    self.set_namespace('mqtt') 
    self.listen_event(self.on_cameraupdate, event=MQTT_MSG, topic=self.prefix+"/"+self.camera+"/#")
    self.mylog("*************************",0) 
    self.mylog("scrypted2ha for camera {} initialized and listeing to events {}".format(self.camera,self.prefix+"/"+self.camera),1)
    
    
  def on_cameraupdate(self, event_name, data, kwargs):
      """Update the sensor based on payload"""
      self.mylog("*************************",3) 
      self.mylog("mqtt load {}".format(data),3) 
      self.set_state(
          entity_id = "sensor.scrycam_"+self.name, 
          state=json.loads(data["motionDetected"]), 
          attributes = { 
              "device_class": None,
              "entity_id": "sensor.scrypted_cam_"+self.name,
              "friendly_name": self.camera,
              "audio_detected": json.loads(data["audioDetected"]), 
              "online": json.loads(data["online"]), 
              "last_changed": self.datetime().replace(microsecond=0).isoformat(),
          }
      )      
  def mylog(self,logtxt,thislevel):
      if self.loglevel >= thislevel:
          self.log(logtxt,log="dev_log")

I have no callback triggered and, indeed I cannot see any event on the bus while listening on the event bus for MQTT_MESSAGE in dev tools.
That is consistent.

However I see a plenty of mqtt messages that should trigger the event using mqtt explorer.

So I think basically no event is being triggered on mqtt message.

Any advice?
Thank You

I believe you need to pass it through the class i.e. class scrypted2ha(hass.Hass, mqtt.Mqtt):

Likewise you might need to add the namespace to the listen_event i.e. self.listen_event(self.something, "MQTT_MESSAGE", topic="dopeaftopic", namespace="mqtt")