How to sort ble.beacons by signal strength?

Sort objects in ble.beacons array according to the field rssi from highest to lowest

When working with Bluetooth Low Energy (BLE) beacons in flespi, you might need to sort the beacons array by signal strength (RSSI) to prioritize the closest or strongest beacons. This is particularly useful for indoor positioning, asset tracking, or proximity-based applications.

The `ble.beacons` parameter in device messages contains an array of detected beacons, each with its own RSSI value. However, this array is not sorted by default, making it difficult to quickly identify the strongest signal or closest beacon.
You can use a msg-pvm-code plugin to sort the `ble.beacons` array by RSSI value, with the strongest signal (highest RSSI value) first. Here's a complete PVM code implementation:

optional .ble.beacons ==> array:
    array ==> #ble.beacons.sorted:
        1000 ==> $previous_strongest_rssi
        0 ==> $sorted_count

        repeat[array_length]:
            -999 ==> $strongest_rssi

            repeat[array_length, counter=$i, from=0]:
                [$i] ==> object:
                    if .rssi > $strongest_rssi && .rssi < $previous_strongest_rssi:
                        .rssi ==> $strongest_rssi

            repeat[array_length, counter=$i, from=0]:
                [$i] ==> object:
                    if .rssi == $strongest_rssi:
                        object ==> append:
                            foreach[$key]:
                                {$key} ==> {$key}
                        $sorted_count + 1 ==> $sorted_count

            $strongest_rssi ==> $previous_strongest_rssi

            if $sorted_count == array_length:
                break

move optional .ble.beacons.sorted ==> #ble.beacons

How It Works

1. The code first checks if the `ble.beacons` parameter exists in the message
2. It creates a temporary array `ble.beacons.sorted` to store the sorted beacons
3. It initializes variables to track the sorting process:
   - `$previous_strongest_rssi` starts at 1000 (higher than any realistic RSSI value)
   - `$sorted_count` tracks how many beacons have been sorted
4. For each iteration of the outer loop:
   - It finds the strongest signal (highest RSSI value) that is weaker than the previously found strongest signal
   - It adds all beacons with that RSSI value to the sorted array
   - It updates the `$previous_strongest_rssi` to the current strongest value
   - It continues until all beacons are sorted
5. Finally, it replaces the original `ble.beacons` array with the sorted one

Input Message example

{
    "ble.beacons": [
        {
            "battery.low": false,
            "battery.voltage": 3.01,
            "id": "10A8CFB2-2BC9-55AA-BB52-7A5DD6730055",
            "rssi": -76,
            "state": "found"
        },
        {
            "battery.low": false,
            "battery.voltage": 3.06,
            "id": "E66B2019-61C7-55AA-9116-CBC30C720055",
            "rssi": -38,
            "state": "found"
        },
        {
            "battery.low": false,
            "battery.voltage": 2.98,
            "id": "00000000-0000-0000-0000-000000000005",
            "rssi": -74,
            "state": "found"
        }
    ]
}

Output Message

{
    "ble.beacons": [
        {
            "battery.low": false,
            "battery.voltage": 3.06,
            "id": "E66B2019-61C7-55AA-9116-CBC30C720055",
            "rssi": -38,
            "state": "found"
        },
        {
            "battery.low": false,
            "battery.voltage": 2.98,
            "id": "00000000-0000-0000-0000-000000000005",
            "rssi": -74,
            "state": "found"
        },
        {
            "battery.low": false,
            "battery.voltage": 3.01,
            "id": "10A8CFB2-2BC9-55AA-BB52-7A5DD6730055",
            "rssi": -76,
            "state": "found"
        }
    ]
}

You can test this PVM code using the PVM plugin sandbox before implementing it in your production environment.

For more information on working with BLE beacons in flespi, see our article on working with BLE beacons in flespi.


See also
Article explains how to automatically store device command execution results in device messages
How to add data generated by calculators to device messages