AppDaemon not talking to MQTT

I’m having trouble getting my AppDaemon app to talk to the MQTT service. Here’s the details of my config;

AppDaemon config

secrets: /config/secrets.yaml
log:
  logfile: STDOUT
  errorfile: STDERR
appdaemon:
  threads: 10
  app_dir: /config/appdaemon/apps
  plugins:
    HASS:
      type: hass
      ha_url: http://hassio/homeassistant
      token: <removed>
    MQTT:
      type: mqtt
      namespace: mqtt
      client_host: 192.168.86.125

My app is as follows (I’ve also used MQTT_MESSAGE);

import appdaemon.plugins.mqtt.mqttapi as mqtt
import json

class SendSMS(mqtt.Mqtt):

  def initialize(self):
     self.log("SendSMS Ready")
     self.listen_event(self.mqtt_message, "MQTT_EVENT", namespace = "mqtt", topic = 'homeassistant/sms/send_message')

  def mqtt_message(self, event_name, data, kwargs):
      json_data = json.loads(data['payload'])

      self.log('Sending "' + json_data['message'] + '" to ' + json_data['number'])     

And the log shows the following;

2019-11-15 21:25:01.794885 INFO AppDaemon: Loading Plugin MQTT using class MqttPlugin from module mqttplugin
2019-11-15 21:25:01.827799 INFO AppDaemon: MQTT: MQTT Plugin Initializing
2019-11-15 21:25:01.828369 INFO AppDaemon: MQTT: Using 'mqtt client status' as Will Topic
2019-11-15 21:25:01.828913 INFO AppDaemon: MQTT: Using 'mqtt client status' as Birth Topic
2019-11-15 21:25:01.829494 INFO AppDaemon: MQTT: Using 'appdaemon_mqtt_client' as Client ID
2019-11-15 21:25:01.895857 INFO AppDaemon: Got initial state from namespace mqtt
2019-11-15 21:25:01.883919 INFO AppDaemon: MQTT: Connected to Broker at URL 192.168.86.125:1883
2019-11-15 21:25:01.897103 INFO AppDaemon: MQTT: MQTT Plugin initialization complete

I’ve got an MQTT client checking the broker and the message is getting picked up by the broker, but the app is not responding. Have I missed anything obvious?

Do you see the log message “Send SMS Ready”? I use MQTT_MESSAGE and it works.

1 Like

Yes, the Send SMS ready message is in the log.

I think MQTT_EVENT should be MQTT_MESSAGE. This is my listen_event line

        self.listen_event(self.at_home, "MQTT_MESSAGE",
                    namespace = "remote_mqtt", topic = self.args["rcv_topic"])

Are you sure you are using the correct topic?

As @Burningstone and @gpbenton said, unless you change it in your config (which it doesn’t seem you have) the correct event to listen for is “MQTT_MESSAGE”.

You’d see errors in your log if MQTT wasn’t connecting. Instead you’re getting a “Connected” message.

To make sure it isn’t a topic issue, take the topic part out of your listen_event() call and just log that a message was received in the callback. Like this:

  self.listen_event(self.mqtt_cb, event="MQTT_MESSAGE", namespace="mqtt")

def mqtt_cb(self, event, event_data, kwargs):
  self.log('MESSAGE RECEIVED')

If you see that working when you publish a message to MQTT, then you’re good.

Also to note, it doesn’t send the event on a retained message. So you need to be sure you publish to the topic AFTER the listener has started.

1 Like

Hi @ajcooper72! Hopefully you have already solved this, but maybe in the code is missing the ‘mqtt_subscribe’ service? isn’t?