Bluetooth binding missing "present" channel?

Unless I’ve misunderstood something, it seems the Bluetooth binding only reports change in rssi. If two consecutive readings of rssi report the same value, only the first result would trigger a change or update rule. If this is true, you can’t reliably know the last detection of presence of a BLE beacon. You can’t tell the difference between a beacon reporting repeatedly the same rssi from a beacon that left the house.

Does this sound familiar to anyone? Did anyone create some kind of work around, perhaps some kind of Exec binding with a script doing some hcitool lescan for detecting presence of a ble beacon?

Thanks!

I created my own BLE device presence control, it’s been extremely reliable during the week I’ve been using it. It makes use of the Exec Binding and a linux script doing some hcitool lescan, leaving a Switch On or Off. Together with a Network binding that detects when my phone register on my local network I’ve got a flawless Home/Away setup.


This is the script /home/openhabian/lescan.sh:

#!/bin/sh
#
# Need: sudo setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`
hcitool lescan > /tmp/lescan.txt &
pid=$!
sleep 11
kill -INT $pid
wait $pid

if grep -q $1 /tmp/lescan.txt; then
    echo "1"
else
    echo "0"
fi

(Credits: http://federicopfaffendorf.com.ar/hcitool-lescan-timeout/)


Then just create the exec thing, an Item and a Rule and your good to go. For instance:


  1. Thing (insert your BLE device address):
Thing exec:command:lescan-tile [command="/home/openhabian/lescan.sh AA:BB:CC:DD:EE:FF", interval=240, timeout=15, autorun=false] // Run every 4 minutes

  1. Items:
Switch AtHome "I'm home"
String LescanRaw "[%s]" {channel="exec:command:lescan-tile:output"}

  1. Rules:
var Number lastPresenceActivity = now.millis

// BLE Tile is scannable => I'm home
rule "PresenceActivityTileScan"
when
        Item LescanRaw received update "1"
then
        if (AtHome.state != ON) AtHome.sendCommand(ON)
        lastPresenceActivity = now.millis
end

// Check every 5 minutes for last AtHome event, 10 minutes with no activity sets away
rule "PresenceCheck"
when
        Time cron "0 0/5 * 1/1 * ? *"
then
        if (AtHome.state != OFF && now.millis - lastPresenceActivity > 600000) { // 10 minutes
                AtHome.sendCommand(OFF)
        }
end

The Network Binding makes sure the AtHome switch sets to ON when I’m in Wifi range and disarms the alarm before I open the door. The lescan script makes sure the switch stays ON even though the phone goes in sleep mode, is turned off, out of battery, etc. My BLE device is a “Tile Mate” on my keys.

2 Likes