Mqtt Binding

702 views
Skip to first unread message

Paul

unread,
Apr 12, 2014, 4:12:20 AM4/12/14
to ope...@googlegroups.com
Hello all,

I'm trying to implements an Mqtt Binding in order to get data from Mqtt stream and then to handle rules based on Mqtt data (send an email when Temperature is increasing)

The Temperature sensor already send data to Mqtt stream.
My issue would be to get Mqtt flow on openHAB.

I have setup the "MQTT Transport" from openhab.cfg file:
mqtt:mosquitto.url=tcp://localhost:1883 ... ...

On the runtime console, I can see logs, and it looks like openHAB has been successfully connected to Mqtt broker.

Then, I guess I must add a new item to the list, I've tryed to add a basic item:
String testItem (Mqtt) {mqtt="<[mosquitto:/temperature/test/temp:state:*:default]"}

End finally, I inserted a rule
rule "Temp test increase"
    when
        Item testItem received command INCREASE
    then
        println("Temp has increased!")
end


What's wrong and/or missing.

Thank you for your help.

Ben Jones

unread,
Apr 12, 2014, 6:14:58 PM4/12/14
to ope...@googlegroups.com
There is no command 'INCREASE' for a string item.

You need to trigger on 'received update' and write the logic to detect increases inside the rule, probably using persistence (in order to retrieve the earlier values).

Paul

unread,
Apr 13, 2014, 3:58:28 PM4/13/14
to ope...@googlegroups.com
Thank you Ben Jones.
I thin it can be trivial to store a variable and then to know if it hs incresed or decreased since previous storage date..

BTW, my main issue is currently to get data from an Mqtt flow. I have setup few month ago a mosquitto broker, and looks to be working fine. I can publish data to a Mqtt topic (e.g: temperature/test/temp).. and then I have setup openHAB with the paramete listed on my previous post.

I'm quite sure I have forgot something, but I don't know what.. Could you dvice me?

thanks.

Ben Jones

unread,
Apr 13, 2014, 4:29:01 PM4/13/14
to ope...@googlegroups.com
Here is an example of a rule I use to detect a rapid rate of change in humidity in my bathroom, which then turns the bathroom fan on/off;

var Number humidity2_LastValue = 0
var Number humidity2_LastUpdated = 0

rule "Bathroom fan"
when
    Item Sensor_Humidity2 changed
then
    // rule parameters
    var int turnOnThreshold = 5     // rate of change per min
    var int turnOffThreshold = 50   // static value
    
    // get the current value
    var Number humidity = Sensor_Humidity2.state as DecimalType

    // check if the humidity is rising or falling...
    if (humidity2_LastValue > 0) {
        if (humidity > humidity2_LastValue) {
            // get the difference since our last update
            var Number humidityDelta = humidity - humidity2_LastValue
            var Number timeDeltaMs = now.millis - humidity2_LastUpdated
        
            // calculate the rate of change - % per minute
            var Number rateOfChange = humidityDelta / timeDeltaMs
            var Number rateOfChangePerMin = rateOfChange * 60000

            // check if rising rapidly, faster than our threshold
            if (rateOfChangePerMin >= turnOnThreshold && Heating_BathroomFan.state == OFF) {
                Notify_Info.postUpdate("Bathroom humidity increasing at " + rateOfChangePerMin + "%/min - turning on fan")
                Heating_BathroomFan.sendCommand(ON)
            }
        } else {
            // check if humidity is falling and drops below our threshold
            if (humidity <= turnOffThreshold && Heating_BathroomFan.state == ON) {
                Notify_Info.postUpdate("Bathroom humidity dropped to " + humidity + "% - turning off fan")
                Heating_BathroomFan.sendCommand(OFF)
            }
        }
    }
    
    // update our 'last' value/update dttm so we can track the rate of increase
    humidity2_LastValue = humidity
    humidity2_LastUpdated = now.millis
end

Thorsten Lanfer

unread,
Apr 14, 2014, 1:54:11 AM4/14/14
to ope...@googlegroups.com
I started playing around with the mqtt binding yesterday and initially struggled too.
I changed my openhab.conf, create some items, changed my sitemap. I started openhab and the logs said something about the connection to the mqtt broker being successful. But when i sent commands to the items, nothing happened.
Only after about an hour of searching i realized that i had forgotten to actually add the mqtt binding bundle to the addons-Folder of openhab.

Downloaded it, put it in there and everything worked like a charm.

Ben Jones

unread,
Apr 14, 2014, 4:52:02 AM4/14/14
to ope...@googlegroups.com
Yeah the MQTT IO transport is builtin I believe - which allows you to configure a connection to a broker. However you need the MQTT binding in order to 'bind' items to MQTT topics, or use something like the Mqttitude binding to track presence (via OwnTracks).

So in short, there are two parts, you need the IO transport always, and then whatever binding you want to use on top of that. Usually the MQTT binding.

Paul

unread,
Apr 14, 2014, 11:25:43 AM4/14/14
to ope...@googlegroups.com
Hello Thorsten Lanfer,

Could yo explain me how do you send commands to item using Mqtt.
Do you means about publishing data to Mqtt broker ?
or did you use something else?

On my end, my goel is to use mosquito_pub" over a specific topic (e.g. /temperature/test/temp) ; and then, I don't understand how to setup openHAB to subscribe to that specific topic and do the rule accordingly..

hans.jo...@gmail.com

unread,
Apr 16, 2014, 5:26:51 PM4/16/14
to ope...@googlegroups.com
Hey,

I would be very interessted in knowing what mqtt broker people use, also the version number and what version openhab..

As I have written in a few other threads I've had major problems getting the mqtt/mqttitude binding to work without hanging openhab.. :/

I'm using openhab from git (it has been updated at least 4-5 times this year and I've had the problem since I first started trying to use MQTT).
And I'm using Mosquitto 1.2.1-1 (Debian Wheezy)

What versions are you guys using? Are you using the Zwave plugin as well?

Ben Jones

unread,
Apr 16, 2014, 5:28:49 PM4/16/14
to ope...@googlegroups.com, hans.jo...@gmail.com
I initially tried using mosquitto from the Ubuntu repos but it was very old.

I am now running mosquitto version 1.3.1 (build date 2014-03-25 00:22:28+0000).

I have the ZWave binding as well as many others and everything is running sweet with MQTT.

hans.jo...@gmail.com

unread,
Apr 16, 2014, 5:33:35 PM4/16/14
to ope...@googlegroups.com, hans.jo...@gmail.com
Okay.. So about the same..

I can't figure out why it keeps crashing..

I'm using this configuration in OpenHAB;
mqtt:mqtt.url=tcp://localhost:1883

And username/password is configured also.

And I've been using these items;
Switch  PresenceHans_PhoneMqtt   "Hans@Home"   (Mosquitto) { mqttitude="mqtt:/mqttitude/hans:home" }
Switch  PresenceHans_PhoneMqtt_Work   "Hans@Work"   (Mosquitto) { mqttitude="mqtt:/mqttitude/hans:work" }
Switch  PresenceHans_PhoneMqtt_50   "Hans@50km"   (Mosquitto) { mqttitude="mqtt:/mqttitude/hans:50km" }
Switch  PresenceHans_PhoneMqtt_100   "Hans@100km"   (Mosquitto) { mqttitude="mqtt:/mqttitude/hans:100km" }
Switch  PresenceKatrine_PhoneMqtt   "Katrine@Home" (Mosquitto)   { mqttitude="mqtt:/mqttitude/katrine:home" }
Switch  PresenceKatrine_PhoneMqtt_50   "Katrine@50km" (Mosquitto)   { mqttitude="mqtt:/mqttitude/katrine:50km" }
Switch  PresenceKatrine_PhoneMqtt_100   "Katrine@100km" (Mosquitto)   { mqttitude="mqtt:/mqttitude/katrine:100km" }
Number PresenceHans_Lat       "Hans Latitude [%.6f]"        <gps> (Mosquitto)    { mqtt="<[mqtt:/mqttitude/hans:state:REGEX(.*\"lat\"\\S\\s\"(-?\\d+\\.\\d+)\".*)]" }
Number PresenceHans_Long      "Hans Longitude [%.6f]"       <gps> (Mosquitto)    { mqtt="<[mqtt:/mqttitude/hans:state:REGEX(.*\"lon\"\\S\\s\"(-?\\d+\\.\\d+)\".*)]" }
Number PresenceKatrine_Lat       "Katrine Latitude [%.6f]"       <gps> (Mosquitto)    { mqtt="<[mqtt:/mqttitude/katrine:state:REGEX(.*\"lat\"\\S\\s\"(-?\\d+\\.\\d+)\".*)]" }
Number PresenceKatrine_Long      "Katrine Longitude [%.6f]"       <gps> (Mosquitto)    { mqtt="<[mqtt:/mqttitude/katrine:state:REGEX(.*\"lon\"\\S\\s\"(-?\\d+\\.\\d+)\".*)]" }

Perhaps it's just too many items on the same subjects? Could that be it?

The reason I'm doing it this way was that I was thinking that I'd automatically turn down the heat if we are more than 50km away, and turn it even more down if more than 100km away, and use the home location to park my Roomba if it's running ;)

Ben Jones

unread,
Apr 16, 2014, 5:35:46 PM4/16/14
to ope...@googlegroups.com, hans.jo...@gmail.com
My config is;

############################## Mosquitto Binding ###################################
#
mqtt:mosquitto.url=tcp://127.0.0.1:1883
mqtt:mosquitto.clientId=openhab
mqtt:mosquitto.user=username
mqtt:mosquitto.pwd=password
mqtt:mosquitto.qos=0
mqtt:mosquitto.retain=false
mqtt:mosquitto.async=true

I have quite a few topics as well, so I don't think that will be an issue.

Is it worth upgrading to the latest version (1.3.1)?

Jan-Piet Mens

unread,
Apr 17, 2014, 6:33:26 AM4/17/14
to ope...@googlegroups.com
> And I'm using Mosquitto 1.2.1-1 (Debian Wheezy)

I'd like to suggest upgrading to Mosquitto 1.2.3 at least (1.3.1 if
possible); it's easy to do; see mosquitto.org on how to add the
repository for Debian.

Also, from a second message of yours on this thread: your regexes for
determining location are ... suboptimal; do have a look at Ben's
MQTTitude binding for OwnTracks; I think that'll be much for satisfying
than parsing regexes from JSON. (That.Will.Break.)

-JP

Greg

unread,
Apr 17, 2014, 10:22:33 AM4/17/14
to ope...@googlegroups.com, hans.jo...@gmail.com
Try removing your number items and starting there.  When I upgraded mqttitude to owntracks, mqtt binding stopped working as well.  As soon as I removed those numbered items, it worked fine.  I haven't had a chance to investigate it, I suspect something with the regex changed.  I would also recommend to make sure you are using the latest mosquitto.
Reply all
Reply to author
Forward
0 new messages