MQTT Client - Beta

The MQTT client acts similarly to the telnet client in that you use a driver to create a connection to an MQTT broker and manage the connection and messages through that driver.

Documentation is available here: MQTT Interface - Hubitat Documentation

See post 22 for updated information

At this point it is use at your own risk !

11 Likes

I am hoping I have time this week to work on testing this with my current broker I use.

38 posts were merged into an existing topic: [Alpha] MQTT application

Awesome news! Thank you for developing this feature, it's going to make it much easier to link all my different IOT devices together with Hubitat. This also let's me drop a large chunk of Node-Red integration I have linking Hubitat events and Maker API with MQTT and Home Assistant.

Are there any performance considerations to take into account?

Thankyou for this feature!! This will set hubitat above all other hubs, if it isnt already. Can't wait to use it!

@chuck.schwer

This is working really well for me - thanks. Only omission seems to be LWT - Last Will and Testament - any chance ?

https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/

(Will be sending out alpha2 of my MQTT app today to all who have PM'd me)

1 Like

Thank you
I'll be testing this client with MQTT Tasmota enabled Tuya 1,2,3Gang Switches as well as Sonoff 4ch, Basic, POW2 with Tasmota firmware next week.
I'll probably share a use-case once everything settled.
Still waiting for the US post service to deliver my 2 HE to Ukraine

The new features of the MQTT client are documented here:

https://docs.hubitat.com/index.php?title=MQTT_Interface

The client is moving out of alpha, into beta at this point. A new way of accessing the interface is described in the documentation.

The existing methods will be deprecated and removed in a future release.

And here is an updated example driver with the new methods:

metadata {
    definition (name: "Test Mqtt", namespace: "test", author: "Test") {
        capability "Initialize"

        command "publishMsg", ["String"]
    }

    preferences {
        // put configuration here
    }

}

void installed() {
    log.warn "installed..."
}

// Parse incoming device messages to generate events
void parse(String description) {
    log.debug description
    log.debug interfaces.mqtt.parseMessage(description)
}

void publishMsg(String s) {
    interfaces.mqtt.publish("/test/hubitat", s)
}

void updated() {
    log.info "updated..."
    initialize()
}

void uninstalled() {
    log.info "disconnecting from mqtt"
    interfaces.mqtt.disconnect()
}

void initialize() {
    try {
        def mqttInt = interfaces.mqtt
        //open connection
        mqttInt.connect("tcp://test.mosquitto.org:1883", "hubitattest", null, null)
        //give it a chance to start
        pauseExecution(1000)
        log.info "connection established"
        mqttInt.subscribe("/test/hubitat")
    } catch(e) {
        log.debug "initialize error: ${e.message}"
    }
}

void mqttClientStatus(String message) {
	log.info "Received status message ${message}"
}
3 Likes

with this exact example (changing the IP to my local mosquitto) this on the log:

dev:16732019-07-03 10:24:13.290 pm debuginitialize error: Cannot get property 'mqtt' on null object

dev:16732019-07-03 10:24:13.288 pm infoconnection established

dev:16732019-07-03 10:24:12.278 pm infoReceived status message Status: Connection succeeded

also the parse ins not being call on a new message on the topic
any help will be appreciated

Are you running 2.1.2? Take a look at the documentation.

I'm running 2.1.2 and the example published in the topic but the parse method do not do anything when someone else published something

Ok, thanks for the report, we'll look into what is going wrong. In the meantime I updated the example above, please give it a try.

Although I am using the alpha version rather than beta the parse method is being called just fine. I'll be updating my app to beta very shortly so will report back if it breaks.

(I'm assuming of course you have subscribed to topics that are being updated)

After last hub update and the instance creation in the example is fully working. now I got a new toy to play.
this opens a new era for many people like me to integrate new devices. thanks for the update.

OMG! I got it working!

Finally I can connect my Hubitat and Home Assistant!

For those wanting to dive into this. I found this code to be very helpful
https://raw.githubusercontent.com/PrayerfulDrop/Hubitat/master/drivers/Generic%20MQTT%20Client.groovy

I had to change this well written code to a hard coded mess.

def parse(String description) {
log.debug description
log.debug interfaces.mqtt.parseMessage(description)

def onoff = description 
    if (onoff == "topic: L2hvbWUvZGlzaHdhc2hlci9zdGF0ZQ==, payload: T04=")
    	createEvent(name: "switch", value: "on")
    else if (onoff == "topic: L2hvbWUvZGlzaHdhc2hlci9zdGF0ZQ==, payload: T0ZG")
        createEvent(name: "switch", value: "off")
    }

The code is now a virtual switch controller using MQTT. When I send the payload of ON or OFF the switch changes state. Any help to make the driver more generic would be appreciated. The original code appears to be directed to a more specific topic/payload value.

3 Likes

I hope it helps:

void parse(String description) {
def response = interfaces.mqtt.parseMessage(description)
if (response.get('topic') == "Topic_you_are_using"{
if (response.get('payload').contains("on")){
sendEvent(name: "switch", value: "on")
}
if (responce.get('payload').contains("off")){
sendEvent(name: "switch", value: "off")
}
}
}

1 Like

I will try it out thanks! It is so nice to have help after banging my head on this for days lol

I been there, no wait..., I thing I live there :blush:

I am still having problems with the subscribe() method. :confused:

Cool my code was able to assist. I really created this Generic MQTT Driver to be used for outside of normal on/off/dim as there is an app coming from one of the community developers who has automagic for integration of HE and Hass.IO.

This driver is very dynamic and allows you to subscribe to any channel and post to any channel through the configuration. May even be too flexible. But hey, it works! :slight_smile:

3 Likes