MQTT Client - Beta

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

I have been having fun trying to make it work.

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

1 Like

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
1 Like

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:

1 Like

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

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.

Looks like they needed unique clientid's, which makes sense. Mosquitto was doing this as four different clients w/ the same client id were connecting and reconnecting:

New connection from XX on port 1883
Client hubitat already connected, closing old connection

Are you saying that multiple connections to MQTT are being made by my MQTT HE app using the same clientID ?? This would of course cause problems but I only use one broker connection for all devices, publish topics and subscriptions so this shouldn't happen.

... Or are you saying you were connecting your own external 'distinct devices' to the MQTT broker using the same clientID's, which also wouldn't work ...and so you've now fixed your devices clientID's to be unique ?

Or maybe this is not do with my MQTT app at all ?

K

Hi
I have an Arduino (WeMosD1mini) sketch for my Garage with a 'smart' relay (on/off - not toggle), and two GarageDoorSensors
GaragePortSensor1 - triggers when the door is fully closed (0/1)
GaragePortSensor2 - triggers when the door is fully open (0/1)
The sensors are picked up by the MQTT Client, and shows up correctly in Hubitat.
How can I program a GarageTile so that it shows correctly even if I use the physical switch or the key-chain remote on the garage-system not connected to Hubitat?
I'd like to pick up the sensor data (closed), and have the tile turn green, and vice-versa - automatically.
I also have a temp-probe on the Arduino. How can I have this show up on a Tile.
In MQTT I can name the sensors whatever I choose, it that is of any help.

@chuck.schwer

Quick question about the publish parameters based on documentation. I am attempting to execute the following command:

interfaces.mqtt.publish(settings?.topicPub, s, true)

But I am getting errors due to the "true." Based on documentation this should be the retained option boolean of true/false but I get this error in my logs:

dev:28712019-09-11 08:51:29.181 am errorgroovy.lang.MissingMethodException: No signature of method: hubitat.helper.interfaces.Mqtt.publish() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.Boolean) values: [wardhome/salt-tank/delay, 1, true]

Is there a bug in the MQTT client beta?

You are missing a parameter, there are 4 parameters for that call:
void publish(String topic, String payload, int qos = 1, boolean retained = false)

1 - topic
2 - payload
3 - qos
4 - retained

The last 2 are defaults, so if you leave them off they default to 1 and false.. but if you want to change the 4th parameter you also have to pass the 3rd parameter

1 Like