SIAP…What is the proper way to integrate AppDaemon and MQTT?
Publish - I assume just call HASS mqtt.publish service
Subscribe - ? Setup a sensor in HASS then have AppDaemon listen for it? Anyway to eliminate the HASS step?
AppDaemon currently does not have the code to integrate directly with MQTT, although it might make sense to do that at some point.
You can certainly call the MQTT publish service in HASS, and I guess your idea of setting up an MQTT sensor then listen for state changes should work well.
You can listen for mqtt_message_received on the event bus, but I think you have to have a sensor defined to subscribe to the topic.
Here is something I use to subscribe to the mqtt message of my motion sensors.
If you don’t want to wait for the plugin, you can send mqtt messages to AD now, be sending events from HA. Here is my automation to send OpenMQTT gateway messages
I did some testing using the files attached to the pull request on github. I had some initial errors that I was able to resolve but for some reason the mqtt plugin doesn’t try to connect to my local mqtt broker. (It’s not the built-in broker that comes with Homeassistant.)
After an hour or so other responsibilities called…
Errors:
There was a mqtt error in the appdaemon.log which required that I:
comment out lines 45 and 46.
line 46 needed to start with self. mqtt_client…
I then copied those two lines down to line 60, after the “self.mqtt_client.tls_set” function call because:
the mqtt_client object didn’t exist at lines 45/46
the tls_insecure_set() function couldn’t be called until the client’s certificate had been added to the mqtt_client object.
After those changes there weren’t anymore errors.
I setup a demo app that runs without errors. It’s basically the demo app that’s with the pull request without the jarvis code. The app is created and enters data into the log file.
As mentioned above, for some reason the mqtt server never receives a connection request.
The mqtt server’s logs show a connection from Homeassistant and will show mqtt data posted throught the Homeassistant front end, but nothing from Appdaemon seems to try and connect to it.
This is the the mqtt section from the appdaemon config file:
The client password isn’t supplied because certificates are being used.
I don’t know a lot about mqtt but it seems that there’s a broker certificate missing.
The mqtt section from the Homeassistnat configuration file has 3 lines:
certificate: /home/homeassistant/.homeassistant/certs/ca.crt
client_key: /home/homeassistant/.homeassistant/certs/hass.key
client_cert: /home/homeassistant/.homeassistant/certs/hass.crt
The first certificate doesn’t seem to be required by the Appdaemon mqtt plugin. My guess is that it verifies the broker to the client. Which made me think that mqtt_verify_cert needs to be “true” - but changing it to true and restarting Appdaemon didn’t change the results.
Hopefully this is of some help.
This is a busy time for our business so I won’t have time to work on this until (maybe) next weekend. Focus on getting the mqtt plugin into Appdaemon without certificates. They can be added later.
Certificate in that case is the ca certificates used to verify trust with the broker. I’t implemented a little odd in HA but in any case I added the option mqtt_ca_certs to configure it. I assume you are not using a standard letsencrypt or otherwise standard trusted certificate? But in any case that should get to the same place as the HA connection.
Unfortunately, there doesn’t seem to be a way to test if the connection is actually made, just the fact that it did not actually make the connection!