Step-by-step presence detection with bluetooth tag (e.g., GTags) and ESPresense on ESP32

Hi,

I’m also starting to use espresense but I think I found an easier way :slight_smile:

I added a channel to my broker to get all MQTT updates in the devices topic for a special device:

“14_Pro_Max” is my in the espresense configured and monitored iPhone. With the “#” I receive alle keys and values for handling later in a rule.

After that I created a dummy String item for visualising the room name.

I created following rule to check for the room name and set it the previous created dummy String item:

var klausLastRoom = ""
var klausLastDistance = 10.0

rule "espresense_room_klaus"
    when
        Channel "mqtt:broker:Matt_broker:c_14" triggered
    then
        
        val json = receivedEvent.split('#').get(1)
        
        val distance =  transform("JSONPATH", "$.distance", json)
        
        val distanceDouble =  Double.parseDouble(distance)
        
        val room = receivedEvent.split('#').get(0).split('/').get(3)
              
        //checking if the room is the same current
        if(klausLastRoom != room){
        
        	logInfo("espresense_log", "Checking new room: " + room)
        	
        	//checking if new room is closer than current one
        	if(distanceDouble < klausLastDistance){
        	
        		klausLastRoom = room
        		klausLastDistance = distanceDouble
        		
        		// translating the technical espresense names to human readable ones (hard coded)
        		if(klausLastRoom == "esp_og_eltern"){
        			iphone14Room.sendCommand("Schlafzimmer")
        		}else if(klausLastRoom == "esp_eg_living_room"){
        			iphone14Room.sendCommand("Wohnbereich")
        		}else if(klausLastRoom == "esp_ug_office"){
        			iphone14Room.sendCommand("Büro")
        		}else if(klausLastRoom == "esp_ug_cellar"){
        			iphone14Room.sendCommand("Keller")
        		}else{
        			iphone14Room.sendCommand("Unbekannter Raum")
        		}
        		
        		logInfo("espresense_log", "New room for device detected: " + room)
        	}else{
        		logInfo("espresense_log", "Room ist too far away: " + room + ". Current distance is " + klausLastDistance + " vs. " +  distanceDouble)
        	}
        	
        // update distance for current room	
        }else{
       	 	logInfo("espresense_log", "Current room info for: " + room)
       	 	klausLastDistance = distanceDouble
       	 	logInfo("espresense_log", "Updated distance for current room: " + klausLastDistance)
        }
        
end

For other devices a new channel for the broker, a new dummy String item and a new rule is needed (easy copy paste modify)

Hope it helps and saves you some time :slight_smile:

1 Like