MQTT Client - Beta


#22

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}"
}

#23

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


#24

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


#25

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


#26

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.


#27

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)


#28

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.


#29

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.


#30

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")
}
}
}


#31

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


#32

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


#33

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


#34

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:


#35

I have been having fun trying to make it work.

I'm just itching to finally get my Sense energy monitor working.


#36

The following works!

So the code is just for one-way sending of messages. There is no publishing, only subscribing. I have a Sense Energy Monitor that tells me when my devices turn on and off. No device control.

This takes the MQTT message from Home Assistant with the Sense component and send the ON/OFF status of my Sense devices.

The payload message is: OFF or ON.

Sample Home Assistant Automation code to control the MQTT messages to Hubitat

- id: '1562365442134'
  alias: Freezer Publish Off
  trigger:
  - entity_id: binary_sensor.freezer
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      payload: 'OFF'
      retain: 'true'
      topic: /home/freezer/state
    service: mqtt.publish
- id: '1562371629250'
  alias: Freezer Publish ON
  trigger:
  - entity_id: binary_sensor.freezer
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      payload: 'ON'
      retain: 'true'
      topic: /home/freezer/state
    service: mqtt.publish

#37

Is there a post or thread about this anywhere? I was thinking about writing a Home Assistant component myself (a component seems like the best way--it's what their ST integration uses, but with their new cloud API), but I would much rather use or contribute to another if that is the case. :slight_smile:


#38

Make a request. The developer may be looking for additional testers.


#39

Hi - the MQTT app will connect to HA one of two ways. Either using the HA statetream capability via MQTT with a handful of small automation scripts on HA to make it two way, or using HA's MQTT auto discovery.

It also supports publishing HE devices to MQTT and bringing 'adhoc' MQTT devices into HE. Lastly it supports the homie3 MQTT discovery protocol both for advertising it's own devices so things like OpenHAB can discover them and for HE's own discovery of devices.

If you are using this just for Home Assistant a component might be tidier but the MQTT app is aimed for generalised but feature rich support of MQTT in HE.

There's probably just one more alpha release hopefully just after this weekend supporting Hubitat's latest release of the beta driver and last will and testament, and then there will be a public beta after a bit of code refactoring.

K

PS. Sent you an invite


#40

BTW anyone can just PM me to get at the alpha code for the MQTT app. However you should be familiar and already using MQTT. The thread isn't there to familiarise users or get MQTT setup and working, it's purpose is to find any bugs and make sure the features offered match peoples needs.

If you're wanting to get into MQTT it's probably better to await the beta release which I'll post on here, where more people can help ease you into MQTT.

K


#41

Is anyone able to successfully have multiple distinct MQTT devices connected to the same queue? I am seeing lots of disconnects - "Error: Connection lost: Connection lost". Wondering if there is only one MQTT connection behind the scenes and the multiple devices are fighting for the channel.