Integration Solutions using MQTT

My Raspberry Pi is publishing these MQTT messages and I want to create a routine to do something when it receives the message.

I have three of your MQTT drivers installed (MQTT Handler V1, MQTT Devices V1.1, and MQTT Test Driver 1)
but I can’t figure out how to do what I want.

I’ve included two screenshots of the configuration on the MQTT Devices V1.1 driver.

wptracy@wptracy:~mosquitto_sub -h 10.0.0.3 -t # -d-d
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending SUBSCRIBE (Mid: 1, Topic: #, QoS: 0, Options: 0x00)
Client (null) received SUBACK
Subscribed (mid: 1): 0
Client (null) received PUBLISH (d0, q0, r0, m0, ‘monitor/Rpi400/status’, … (6 bytes))
online
Client (null) received PUBLISH (d0, q0, r0, m0, ‘monitor/Rpi400/status’, … (6 bytes))
online
Client (null) received PUBLISH (d0, q0, r0, m0, ‘monitor/Rpi400/pat_tracy_samsung’, … (227 bytes))
{“id”:“00:00:00:00:00”,“confidence”:“100”,“name”:“Pat Tracy Samsung”,“manufacturer”:“Samsung Electronics CoLtd”,“type”:“KNOWN_MAC”,“retained”:“false”,“timestamp”:“Sun Jan 08 2023 09:57:27 GMT-0700 (MST)”,“version”:“0.2.200”}
online
Client (null) received PUBLISH (d0, q0, r0, m0, ‘monitor/Rpi400/pat_tracy_iphone’, … (210 bytes))
{“id”:“00:00:00:00:00”,“confidence”:“100”,“name”:“Pat Tracy iPhone”,“manufacturer”:“Apple Inc”,“type”:“KNOWN_MAC”,“retained”:“false”,“timestamp”:“Sun Jan 08 2023 09:57:39 GMT-0700 (MST)”,“version”:“0.2.200”}
Client (null) sending PINGREQ
Client (null) received PINGRESP


I’m not positive I’m reading your MQTT trace correctly, but it looks like there are two different kinds of messages being published.

  • One is a simple text message of "online’.
  • Another is a JSON-formatted object with various key/value items.

So the question would be what field are you wanting to trigger off of, and what action do you want to take.

I’m guessing by the data this is a status of your iphone presence. So one thing you could do is use the HTTP Devices driver and create a virtual presence device. Then you could configure that device to go into ‘present’ state when the string ‘online’ is received.

If you are trying to key off of something in the JSON-formatted messages, you need to set the “Expected Message Format” device Setting value to ‘JSON’. Then you could base the ‘present’ and ‘not present’ state off of one of the key-values in the JSON message. For example, if you are using the ‘confidence’ field, then your PRESENT value setting would be ‘confidence’, because that’s the key of the value you want.

Never mind. I got it subscribed by configuring the Device creator with the Broker.




Like this, in the screenshot?

When I try to refresh, I get a Refresh requested message in logcat, below, but the Driver Status shows Not Subscribed.

How would it know where the broker is. Or does that matter.


2023-01-08T22:22:23.770217280+00:00 TRACE MQTT Devices V1.1 Received event with handler capability
2023-01-08T22:22:23.775895072+00:00 INFO MQTT Devices V1.1 <Device: 91037e3d-6c9a-4611-a609-5c99f7d4fa17 (MQTT Switch)> received command: {“args”:{},“capability”:“partyvoice23922.refresh”,“command”:“push”,“component”:“main”,“positional_args”:{}}
2023-01-08T22:22:23.779458155+00:00 TRACE MQTT Devices V1.1 Found CapabilityCommandDispatcher handler in MQTT Devices
2023-01-08T22:22:23.782494072+00:00 INFO MQTT Devices V1.1 Refresh requested
2023-01-08T22:22:23.827190405+00:00 DEBUG MQTT Devices V1.1 MQTT Switch device thread event handled

So is it working as you expect now?

Yes, It works great. But I’m still testing, before I post what I did.

Thanks for the great drivers.

1 Like

Thanks @TAustin, for instructions on how to configure your MQTT Devices V1.1 driver.

I got it configured and working to monitor MQTT messages from Andrew J Freyer’s Raspberry Pi blue-tooth presence App

I configured the device as a switch to trigger two rules when iPhone presence is Away and Home.

In the rules, I call your google sheets app via your Webrequestor app to log the events in google sheets.

For viewers wishing to create the Raspberry Pi blue-tooth presence device and to monitor and record events, I’ve added the following screenshots.







1 Like

Great project; thanks for sharing. What has been your experience so far with the Bluetooth presence app?

The app was working, now it stops scanning for the presence of the iphone and samsung.

I had it installed on a Rpi400 running ubuntu. Now I’ve installed it on Rpi 4 4G. It just now detected that the samsung phone has departed (confidence 0). It took 6 minutes to scan for departures.

Andrew Freyer wrote the installation procedure and app for a $15 Pi Zero. Pre-orders sold out 2 years ago.

I pulled the samsung phone out of the Faraday Bag a few minutes ago. It detected presence 12 minutes later.

I’ll use the verbose debug option next to see if it
reveals anything.

[-] 0.2.200 09-01-2023 10:06:21 AM [CMD-MQTT] monitor/Rpi400/pat_tracy_samsung { … confidence : 100 … }
[-] 0.2.200 09-01-2023 10:06:24 AM [CMD-MQTT] monitor/Rpi400/pat_tracy_iphone { … confidence : 100 … }
[-] 0.2.200 09-01-2023 10:06:30 AM [CMD-NAME] 00:00:00:00:00:00 William’s Galaxy S9 Samsung Electronics CoLtd
[-] 0.2.200 09-01-2023 10:06:30 AM [CMD-NAME] 00:00:00:00:00:00 iPhone (5) Apple Inc
[-] 0.2.200 09-01-2023 10:12:16 AM [DEL-RAND] RAND 00:00:00:00:00:00 expired after 160 seconds
[-] 0.2.200 09-01-2023 10:12:21 AM [CMD-MQTT] monitor/Rpi400/pat_tracy_samsung { … confidence : 90 … }
[-] 0.2.200 09-01-2023 10:12:33 AM [CMD-MQTT] monitor/Rpi400/pat_tracy_samsung { … confidence : 45 … }
[-] 0.2.200 09-01-2023 10:12:36 AM [CMD-MQTT] monitor/Rpi400/pat_tracy_samsung { … confidence : 0 … }
[-] 0.2.200 09-01-2023 10:12:36 AM [CMD-NAME] 00:00:00:00:00:00 Pat Tracy Samsung Samsung Electronics CoLtd
[-] 0.2.200 09-01-2023 10:14:45 AM [CMD-NAME] 00:00:00:00:00:00 Pat Tracy Samsung Samsung Electronics CoLtd
[-] 0.2.200 09-01-2023 10:16:23 AM [CMD-NAME] 00:00:00:00:00:00 Pat Tracy Samsung Samsung Electronics CoLtd
[-] 0.2.200 09-01-2023 10:16:36 AM [DEL-RAND] RAND 00:00:00:00:00:00 expired after 162 seconds
[-] 0.2.200 09-01-2023 10:18:36 AM [DEL-RAND] RAND 00:00:00:00:00:00 expired after 152 seconds
[-] 0.2.200 09-01-2023 10:16:36 AM [DEL-RAND] RAND 00:00:00:00:00:00 expired after 162 seconds
[-] 0.2.200 09-01-2023 10:18:36 AM [DEL-RAND] RAND 00:00:00:00:00:00 expired after 152 seconds
[-] 0.2.200 09-01-2023 10:24:44 AM [CMD-MQTT] monitor/Rpi400/pat_tracy_samsung { … confidence : 100 … }
[-] 0.2.200 09-01-2023 10:24:45 AM [CMD-NAME] 00:00:00:00:00:00 William’s Galaxy S9 Samsung Electronics CoL

If you are in the mood to experiment, there is another solution that attempts to report phone presence by yours truly: phonepresence/README.md at main · toddaustin07/phonepresence · GitHub

This uses a different approach than the one you are trying. This scans the local ARP table to see if the device IP is present.

Several folks are using it and have said it’s been reliable for them. Your mileage may vary. Android phones work quite reliably, whereas iPhones can sometimes be problematic because of the way they deep sleep. You also have to be careful the way you have some iPhone settings configured to be make sure your phone is reporting and responding to a consistent IP address: Private Wifi Address turned off; Limit IP Address Tracking turned off.

I’ll try this. Thanks. I’ll get back to you.

Both the iPhone and Samsung have reserved IPs in my router and they both show offline status in router. But I know they’re both working on wi-fi.

I’m not too pleased with the Rpi blue-tooth presence app. It’s erratic.

I probable don’t have something configured right.

wptracy@wptracy:~/Downloads/phonepresence-main$ python3 phonetrack.py
Mon Jan 9 12:51:32 2023 Using ‘IP’ to find tracked devices
Mon Jan 9 12:51:32 2023 Pinging
Mon Jan 9 12:51:32 2023 iphone Presence changed to = True
Mon Jan 9 12:51:32 2023 samsung Presence changed to = True
Mon Jan 9 12:51:32 2023 Scan complete
Mon Jan 9 12:51:44 2023 Pinging
Mon Jan 9 12:51:44 2023 Scan complete
Mon Jan 9 12:51:56 2023 Pinging
Mon Jan 9 12:51:56 2023 Scan complete

So far so good. Now you can monitor it to see how accurate it is for you.

You may need to adjust the retry and ping interval parameters in the config file to optimize results.

I’ll also do the smartthings device settings

When I install LAN Presence Device Driver V1, I get what’s on the screenshot below. The Driver is grayed out. and LAN presence device is offline.

I configure it with what’s on the screenshot below and what the README.md says to put in the configuration.

If the LAN Presence Device can only be configured with one phone IP address name, why are there places for 2 names in the phonetrack.cfg

LAN Device Name: this must match the NAME you used to identify the cellphone IP you specified in the phonetrack.cfg file; avoid blanks and special characters

Rpi edgebridge server:
pi@raspberrypi:~/Documents/PhonePresence/phonepresence-main/SmartThingsIntegration $ python3 phonetrack_st.py
Mon Jan 9 14:52:06 2023 Using ‘IP’ to find tracked devices
Mon Jan 9 14:52:06 2023 Pinging
Mon Jan 9 14:52:06 2023 iphone Presence changed to = True
Mon Jan 9 14:52:06 2023 HTTP ERROR 400 sending: http://10.0.0.3:8088/iphone/presence/present
Mon Jan 9 14:52:06 2023 samsung Presence changed to = True
Mon Jan 9 14:52:06 2023 HTTP ERROR 400 sending: http://10.0.0.3:8088/samsung/presence/present
Mon Jan 9 14:52:06 2023 Scan complete


Looks like one too many '0’s in your LAN Device Address port number.

I assume you have edgebridge running?

One LAN Presence device per phone device. So use the ‘Create new device’ button in the Controls screen of your current (configured) LAN Presence device to create another one and configure that one separately.

that fixed it. I should have caught that and should have known better.
Ports don’t go to 500001.

Thanks, now they’re working

[config]
phone_ips = 10.0.0.130, 10.0.0.251
phone_names = iphone, samsung
phone_offline_retries = 7, 10

ping_interval = 12

port = 50001
bridge_address = 10.0.0.3:8088
console_output = yes
logfile_output = n0
logfile = phonetrack.log

pi@raspberrypi:~/Documents/PhonePresence/phonepresence-main/SmartThingsIntegration $ python3 phonetrack_st.py
Mon Jan 9 15:18:21 2023 Using ‘IP’ to find tracked devices
Mon Jan 9 15:18:21 2023 Pinging
Mon Jan 9 15:18:22 2023 iphone Presence changed to = True
Mon Jan 9 15:18:22 2023 samsung Presence changed to = True
Mon Jan 9 15:18:22 2023 Scan complete

1 Like

Driver Shutdown? I noticed that my MQTT devices stopped working and checked the app and the status says: “Driver Shutdown”. I deleted the mqtt creator and re-installed and it connected fine. When hitting refresh to populate subscribed topics I got the Driver Shutdown status again. Hitting refresh again goes into permanent “Connecting” status. I’m running the 2003-01-05 driver. The only thing I’ve done with ST today is install the WebRequestor app. Any thoughts?

Does it seem to happen only when you are pressing the refresh button?

How many devices do you have and what type are they?

I’ll have to see if I can recreate the problem, but why are you needing to press refresh in the first place? Are your devices not automatically getting subscribed?

Actually I haven’t touched the app in 2 weeks. This morning I noticed my mqtt devices were dead so I checked the app and that’s when I noticed the Driver Shutdown status. First step to resolve was Refresh and it went into constant Connecting… Next step was to delete and reinstall the app. Came up fine but after 5 minutes still no subscribed topics. Hit Refresh again, constant Connecting. After maybe 4 deinstall/reinstalls it seems to be fine. 32 topics: 14 contact, 14 switch and 4 momentary. Maybe just a one-off glitch since it has been perfect for weeks.