Mqtt: if I understand correctly

Hi Everyone,

I am a newbie openhab user. I have been mostly playing with zwave binding and to a much lesser degree with nest and xbmc bindings.

I am trying to understand if I need mqtt. So far, I have been able to accomplish most of what I want using rules. What would I gain by using mqtt?

Sorry if this has been discussed before. Did not find something specifically to answer this.

Thanks

MQTT is a message bus that provides a convenient way to get messages into or out of openHAB for devices that don’t have a well supported binding, or if you want to use the Mqttitude location tracking binding. It isn’t a replacement for rules. It is particularly useful if you have remote devices (i.e. sensors and actuators that are not hosted on the same machine as openHAB such as a DIY Arduino based sensor) that are on the network.

Personally I initially set up an MQTT broker so I could experiment with Mqttitude but I kept it around and use(d) it to:

  • send alerts to mqttWarn so I could receive alerts from openHAB on my phone through Pushbullet
  • receive sensor readings from reed switches attached to my doors and wired up to a remote Raspberry Pi’s GPIO pins
  • receive Bluetooth device detection messages from the remotely deployed Raspberry Pis which helps me detect whether I or my wife is home

I plan on moving away from WebIOPi to trigger my garages to MQTT based messaging as well for consistency.

MQTT is no the only way to implement your system but it seems to be a very common and popular approach.

But in your case I see no need for MQTT. Everything you interact with and use has a good native openHAB binding so moving to MQTT would just complicate your setup without adding anything new.

Thanks @rlkoshak

Would you mind sharing the details of your bluetooth setup. I read about it in the other thread where you were helping someone troubleshoot their issues.

I would like to have the ability to see who is home based on bluetooth connection.

Thanks again.

I have three computers in the house which each support Bluetooth:

  • An old laptop (chimera) running Ubuntu 15 which is my openHAB server and Mosquitto server (MQTT broker) among other things (file server, VPN, Plex, etc). This machine is on the top floor of the house

  • A first gen Raspberry Pi (garage) in the garage which is wired to a dual relay for triggering the garage door openers and to a couple of reed switches that detect whether or not the garage doors are open. It has a bluetooth dongle attached and it is located on the ground floor of the house.

  • Another first gen Raspberry Pi (hydra) in the basement which is wired to the sensors the previous owner had installed on the doors and windows of the house. It too has a bluetooth dongle attached.

Having bluetooth sensing on each of the three floors guarantees that the whole house gets coverage.

I followed the tutorial here to get bluetooth support on all three machines and install access to bluetooth through Python. I used the inquiry.py script to discover the addresses of my and my wife’s phones and then adapted the
detect.py script to work with a script I already wrote for reporting the door and window sensor outputs over MQTT to do the same for whether or not the bluetooth address is detected nearby or not.

So all three devices are sensing and reporting whether or not the BT devices are present and only if all three report that the devices are not present does openHAB mark that person as away. If both people are way the house goes into “away” mode. However, a person has to be away for more than five minutes before the away states become active to prevent the house from flipping back and forth when going to get the mail.

The rules and Items are as follows. I make heavy use of lambdas and group filters in my rules which makes them short but they might seem daunting for someone not used to writing rules.

Items:

Group:Switch:OR(ON,OFF) gPresent     "Present" <present>
Group:Switch:OR(ON,OFF) gRichPresent "Rich Present" <present> (gPresent)
Group:Switch:OR(ON,OFF) gJennPresent "Jenn Present" <present> (gPresent)

Switch   S_V_RichChimeraBT   <bluetooth> (gRichPresent) { mqtt="<[mosquitto:presence_sensors/bluetooth/chimeraRich:state:default]" }
Switch   S_V_RichGarageaBT   <bluetooth> (gRichPresent)  { mqtt="<[mosquitto:presence_sensors/bluetooth/garageRich:state:default]" }
Switch   S_V_RichHydraBT   <bluetooth> (gRichPresent)     { mqtt="<[mosquitto:presence_sensors/bluetooth/hydraRich:state:default]" }

Switch  S_V_JennChimeraBT   <bluetooth> (gJennPresent)  { mqtt="<[mosquitto:presence_sensors/bluetooth/chimeraJenn:state:default]" }
Switch  S_V_JennGarageBT   <bluetooth> (gJennPresent)    { mqtt="<[mosquitto:presence_sensors/bluetooth/garageJenn:state:default]" }
Switch  S_V_JennHydraBT   <bluetooth> (gJennPresent)      { mqtt="<[mosquitto:presence_sensors/bluetooth/hydraJenn:state:default]" }

Rules:

import org.openhab.core.library.types.*
import org.openhab.core.library.items.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import org.eclipse.xtext.xbase.lib.*
import java.util.Map
import java.util.HashMap

val Map<SwitchItem, Timer> timers = newHashMap

val Functions$Function3 home = [ String name, SwitchItem homeSwitch, Map<SwitchItem, Timer> timers |
        logInfo("Presence", name + " was detected at home")
        var t = timers.get(homeSwitch)
        if(t != null) {
                t.cancel
                timers.put(homeSwitch, null)
        }
        homeSwitch.sendCommand(ON)
]

val Functions$Function4 away = [ String name, SwitchItem trigger, SwitchItem homeSwitch, Map<SwitchItem, Timer> timers |
        val timer = timers.get(homeSwitch)
        if(timer == null){
                logInfo("Presence", name + " is not home, setting timer before updating home state")
                timers.put(homeSwitch, createTimer(now.plusMinutes(5), [|
                        if(trigger.state == OFF) {
                                logInfo("Presence", name + " is still away after five minutes")
                                homeSwitch.sendCommand(OFF)
                        } else {
                                logInfo("Presence", name + " came back before timer went off")
                        }
                        timers.put(homeSwitch, null)
                ]))
        } else {
                logInfo("Presence", "Timer for " + name + " is already set")
        }
]

rule "Rich Presence Changed"
when
        Item gRichPresent changed or
        System started
then
        logInfo("Presence", "Rich Group state = " + gRichPresent.state)
        gRichPresent?.members.forEach(s|logDebug("Presence", s.name + " state = " + s.state))

        if(gRichPresent?.members.filter(s|s.state == ON).size > 0) home.apply("Rich", RichHome, timers)
        else away.apply("Rich", gRichPresent, RichHome, timers)
end

rule "Jenn Presence Changed"
when
        Item gJennPresent changed or
        System started
then
        logInfo("Presence", "Jenn Group state = " + gJennPresent.state)
        gJennPresent?.members.forEach(s|logDebug("Presence", s.name + " state = " + s.state))

        if(gJennPresent?.members.filter(s|s.state == ON).size > 0) home.apply("Jenn", JennHome, timers)
        else away.apply("Jenn", gJennPresent, JennHome, timers)
end
2 Likes

thank you so much.

one more question, is your property fairly big (am thinking that 3 detector might be overkill for most medium size houses)? I currently do not have any devices with bluetooth that are on continuously in the house (well maybe a tablet but cannot ensure that it is always on).

What kind of hardware would you recommend for me. I will be setting these up solely for the bluetooth detection purpose.

I think it is big but for around where I live it is average. The big problem is that it is three stories (including the basement) and my openHAB server is on the top floor and bluetooth won’t reach the basement.

However, in practice three sensors is indeed overkill. I could get away with one on the top floor and one in the basement. But I already had the dongles and the Raspberry Pis deployed on the other floors so I just added the bluetooth detection to all of them. It is also nice to have it detect I’m home when I pull into the driveway which having the one in the garage gives me.

Before worrying about deploying remote BT sensors I’d see if your one sensor is good enough to cover the whole house. No need to spend money you don’t need to. I already had all the hardware so for me it was just a little bit of time to get it up and running.

That being said, a Raspberry Pi with a bluetooth dongle will probably be your easiest to set up and configure.

Awesome. will be ordering that as soon as I am able to finishing installing my garage door opener and light switches in the bedroom and closets.

Rich, I’m trying to use your script for some reason it always returns OFF for my bluetooth. The inquiry script returns my bluetooth. I was hoping maybe you can help lead me in the right direction. BTW I’m not running this on a RPI, it’s a Ubuntu machine if that makes a difference.

[Sensor1]
Type: Bluetooth
Address: A4:E4:B8:63:3F:3B
Destination: ShawnPhoneBT
ReportType: MQTT
; Needs to be greater than 1 if using the RSSI method, 25 if using the other method.
Poll: 2

2016-02-26 21:04:39,716 INFO - ---------------Started
2016-02-26 21:04:39,717 INFO - REST URL set to: http://srv0.local:8083/rest/items/
2016-02-26 21:04:39,717 INFO - Configuring the MQTT Broker srv0.local
2016-02-26 21:04:39,721 INFO - Populating the sensor's list...
2016-02-26 21:04:39,722 INFO - ----------Configuring BluetoothSensor: Address = A4:E4:B8:63:3F:3B Destination = ShawnPhoneBT
2016-02-26 21:04:39,723 INFO - Connected with result code 0, subscribing to command topic sensors/getUpdate
2016-02-26 21:04:39,723 INFO - Published message OFF to ShawnPhoneBT
2016-02-26 21:04:39,723 INFO - Kicking off polling threads...
2016-02-26 21:04:39,723 INFO - Received a request for current state, publishing
2016-02-26 21:04:43,497 INFO - Published message OFF to ShawnPhoneBT

root@ubuntu:/opt/sensorReporter/config# ./inquiry.py 
performing inquiry...
found 2 devices
  A4:E4:B8:63:3F:3B - BLACKBERRY-2621

I am very new to this forum and home automation but I really like MQTT and its ability to extend communication via the cloud. I am using it to get a notification on my desk at work when my dog goes upstairs. :slightly_smiling:

Having lots of fun with it for sure!

I have read about sensors with binding specs but I dont know what that means or how it can help me integrate other sensors.

I myself have been having a little bit of trouble with the script as well. When did you download it? I ask because there has been an update to it that changes how it works. The update was written by another developer (@LennySh). He has had a lot of success with this new approach but I myself have not and we haven’t yet worked out why. The original approach is still in the code, controlled by a flag in the code but I haven’t had much luck getting my original approach to work with my wife’s new iPhone.

Now, my problems may be hardware caused as my BT dongles are pretty old.

See the wiki for an explanation of the various parts of openHAB and how they work together.

In short, if you have a sensor that communicates with the protocol “Foo” look to see if there is a Foo Binding for openHAB. If there is openHAB can communicate with that sensor.

Thanks! I will definitely review the info! Just to be clear…if a sensor supports “Foo” bindings there is no need for MQTT communication?

Mostly correct. If both OH and the device support the same technology to communicate they can talk to each other directly.

However there are some technologies that require additional hardware on the OH server (e.g. a USB zwave controller). Also there may be other reasons why you might want your remote devices to report via MQTT (e.g. the sensor is wired to the GPIO pins of a computer where OH is not running).

1 Like

I cloned the git on Thursday or Friday I believe. My wife has a newer iphone so the old method might not work for her either. I’ll give it a shot chaning the flag (I have to find it first) to see if it works. I’m doing it another way but it’s a bit heavy and would love to intgerate it with MQTT instead. I could probably implment that way I’m doing it now to MQTT I just don’t know how to interface with the client side to talk to the server.

Thanks for the reply, I’ll keep an eye on the git for any changes if you do get it working for yourself.

See the mqttConn.py file for how to do client side MQTT from Python at least. Or you can create a new sensor type if what you have now works. I’d love to have another alternative for Bluetooth in the script. Just use what is there as a template for how to build it. I tried to make it pretty flexible and easy to add to.

After reading the code more your lookup method is exactly how I was doing it but more rudimentary. I’m also having issues with my wifes iPhone but I was able to solve it in my script by runing two seperate variables and two seperate calls to the bluetooth.lookup.name. With your script if I call it seperately as two seperate scripts it works if I add two bluetooth sensors it shows both as OFF. You can try and see if you get similar results.

I’m using a newer BT Dongle, and I can find all 4 of my devices without any issue, one of which is my wife’s iPhone 6…

And I’m doing it from a Raspberry Pi 2…

I reworked how the trending works and I’m getting improved detection. Both phones have been detected frequently enough to show them home. However, it is still the case that it does not reliably keep detecting the iPhone, showing it present for a while then losing it for awhile even though both phones have been present and on all day. Clearly there is some more tuning needed. I might need to expose some additional parameters so it can be tuned on a per device basis.

That’s a similar behaviour I was getting, I was wondering if the Bluetooth lookup python module gets “stuck” to a mac and if there’s a flush function? As when I’m testing it seems to get stuck on one of my two phones and both phones will report if one is OFF even though only one is off. Running separate thread/process as you mention seems to help this. Let me know if you find anything more and if you update your code I’ll give it a try.

I checked the updates I made so far into github. I don’t think it gets stuck because when I add some logging to see exactly what the RSSI returns I find that it does see the phone intermittently. It just never sees it enough times in a row to decide that the phone is actually there.