MQTT Client - Beta

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