diff --git a/docs/use/ble.md b/docs/use/ble.md index de9ec860db..fa3280c077 100644 --- a/docs/use/ble.md +++ b/docs/use/ble.md @@ -75,14 +75,12 @@ Once the data has been transmitted to the MQTT broker, it can be easily integrat Examples of compatible sensors among [our list](https://decoder.theengs.io/devices/devices_by_brand.html: Mi Flora, Mi jia, LYWDS02, LYWSD03MMC, ClearGrass, Mi scale, iBBQ, TPMS ## Receiving signals from BLE devices for Device Tracker detection -The gateway will detect BLE trackers from Tile, Nut, TagIt and iTag, as well as other devices with additional properties decoding like Mi Band, Amazfit, RuuviTag and others indicated as Device Trackers in the [compatible BLE devices list](https://decoder.theengs.io/devices/devices.html), and automatically create a device tracker entity following the Home Assistant discovery convention (if auto discovery is activated). +The gateway will detect BLE trackers from Tile, Nut, TagIt, iTAG, Gigaset G-Tag and TicWatch GTH (Pro), as well as other devices with additional properties decoding like Mi Band, Amazfit, RuuviTag and others indicated as Device Trackers in the [compatible BLE devices list](https://decoder.theengs.io/devices/devices.html), and automatically create a device tracker entity following the Home Assistant discovery convention (if auto discovery is activated). The devicen tracker entity created can be attached to a person to leverage presence detection. The `away` or `not home` state is triggered if the BLE tracker is not detected during the timer defined by `presenceawaytimer`. ![Away home Home assistant view](../img/OpenMQTTGateway-BLE-tracker-Home-Assistant.png) -If you have multiple gateways, your BLE trackers may not be detected temporary by one gateway but still by the others. In this case you will see the tracker appears offline briefly and online again once it is detected by the others gateways. - By default `presenceawaytimer` is set to 120s, you can change it from the slider in your controller or with the following command (ms) `mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"presenceawaytimer":66000}'` diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index ae0a63c931..eb340b28c9 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -1269,6 +1269,27 @@ void PublishDeviceData(JsonObject& BLEdata) { Log.notice(F("Not a sensor device filtered" CR)); return; } + +# if BLEDecoder + if (enableMultiGTWSync && BLEdata.containsKey("model_id") && BLEdata.containsKey("id")) { + // Publish tracker sync message + bool isTracker = false; + std::string tag = decoder.getTheengAttribute(BLEdata["model_id"].as(), "tag"); + if (tag.length() >= 4) { + isTracker = checkIfIsTracker(tag[3]); + } + + if (isTracker) { + StaticJsonDocument BLEdataBuffer; + JsonObject TrackerSyncdata = BLEdataBuffer.to(); + TrackerSyncdata["gatewayid"] = gateway_name; + TrackerSyncdata["trackerid"] = BLEdata["id"].as(); + String topic = String(mqtt_topic) + String(subjectTrackerSync); + TrackerSyncdata["topic"] = topic.c_str(); + enqueueJsonObject(TrackerSyncdata); + } + } +# endif } else { Log.notice(F("Low rssi, device filtered" CR)); return; @@ -1553,6 +1574,14 @@ void XtoBT(const char* topicOri, JsonObject& BTdata) { // json object decoding Log.error(F("BLE busy - command not sent" CR)); gatewayState = GatewayState::ERROR; } + } else if (strstr(topicOri, subjectTrackerSync) != NULL) { + if (BTdata.containsKey("gatewayid") && BTdata.containsKey("trackerid") && BTdata["gatewayid"] != gateway_name) { + BLEdevice* device = getDeviceByMac(BTdata["trackerid"].as()); + if (device != &NO_BT_DEVICE_FOUND && device->lastUpdate != 0) { + device->lastUpdate = 0; + Log.notice(F("Tracker %s disassociated by gateway %s" CR), BTdata["trackerid"].as(), BTdata["gatewayid"].as()); + } + } } } #endif diff --git a/main/config_BT.h b/main/config_BT.h index c51c2ebbe1..7d41b7ca6d 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -37,9 +37,10 @@ extern String stateBTMeasures(bool); #endif /*-----------BT TOPICS & COMPILATION PARAMETERS-----------*/ -#define subjectBTtoMQTT "/BTtoMQTT" -#define subjectMQTTtoBTset "/commands/MQTTtoBT/config" -#define subjectMQTTtoBT "/commands/MQTTtoBT" +#define subjectBTtoMQTT "/BTtoMQTT" +#define subjectMQTTtoBTset "/commands/MQTTtoBT/config" +#define subjectMQTTtoBT "/commands/MQTTtoBT" +#define subjectTrackerSync "trackersync" // Uncomment to send undecoded device data to another gateway device for decoding // #define MQTTDecodeTopic "undecoded" #ifndef UseExtDecoder @@ -143,6 +144,10 @@ unsigned long scanCount = 0; # define useBeaconUuidForTopic false // define true to use iBeacon UUID as topic, instead of sender (random) MAC address #endif +#ifndef enableMultiGTWSync +# define enableMultiGTWSync true // //define true to use tracker and closest control devices sync across OpenMQTTGateway and Theengs Gateway gateways +#endif + /*--------------HOME ASSISTANT ROOM PRESENCE--------------*/ #define subjectHomePresence "presence/" // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name) diff --git a/main/main.ino b/main/main.ino index 9464e455b6..b4ea58cc18 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1124,6 +1124,10 @@ void setupMQTT() { mqtt->subscribe(subjectMultiGTWIR, receivingDATA, mqtt_max_payload_size); # endif +# if enableMultiGTWSync + mqtt->subscribe(String(mqtt_topic) + subjectTrackerSync, receivingDATA, mqtt_max_payload_size); +# endif + mqtt->begin(); } #endif