One of the metrics I am interested in having in my Home Assistant dashboard is the number of Android ADB devices are connected at a given time. There are occasionally times when devices reboot and connections are not automatically re-established. This poses a problem when attempting to control these devices.

One way to combat this is to have a sensor that is able to get the status of the total number of connected ADB devices and display the in a gauge in HA.

type: gauge

entity: sensor.adb_total_devices_connected

min: 0

theme: default

name: ADB Connected

severity:

green: 2

yellow: 1

red: 0

max: 2



In this example I have two devices that should be connected, setting green, yellow, red and max accordingly just makes for a prettier display.

Under the Hood

Now that we went over how to display the data, let’s discuss how to actually obtain it. For this I use a command line sensor.

- platform: command_line

command: adb devices | sed '1d;$d' | sed 's/\<device\>//g' | wc -l

name: ADB Total Devices Connected

scan_interval: 300



If we run this command directly from the command line we can see the information broken down from the standard output of adb devices.

While this is just a rudimentary display of the devices connected, future uses could be to trigger a script to attempt to re-connect devices and notify of potential errors. The sky is the limit.