Known_devices.yaml needs a good cleaning!

I use the unifi device_tracker platform, and find that I get lots of random entries added over time to my known_devices.yaml file. Last I check, it was 3.4 megabytes in size and filled with random crap. Where these are coming from is a different problem; I wonder if I have a device that’s randomly selecting new MAC addresses behind my back. I live out in the sticks, so there just are not thousands of random devices that happen to drive past my home.

It was too difficult to go through and clean out manually… so I did a quick one-liner that deletes all the components where the device name is just the MAC address, which seemed to be an indicator of “this is garbage”.

This might or might not be of interest to others. This is using sed on my MacOS box where I run Home Assistant… probably the Linux GNU sed can be convinced to use the same style regular expressions (note these are not shell file wildcard “glob” patterns):

sed -E -e "/'*[0-9a-f]{12}'*:/,/^ *\$/d" < known_devices.yaml > known_devices-stripped.yaml

This deletes all lines between a 12 character string starting at the beginning of the line, where every character is a valid hexidecimal digit, though the next blank line between entries. This should also catch those entries that are all numeric and have ' characters around the identifier.

Of course, this doesn’t modify your known_devices.yaml file; it creates a new one for you to sanity check for your circumstances.

Perhaps this might be useful to others.

1 Like