Unifi integration - large number of unnamed devices

And will my current unnamed ones disappear?

I don’t know. Probably

Unfortunately they haven’t.
How can I remove them? I’m ‘happy’ to edit .storage files if that is the only way.

Thanks

You can edit the .storage/core.device_registry, make sure to back it up first

It’s been a while but it seemed sensible to continue here rather than start a new thread.

Why is my Unifi Network integration creating new but disabled device_tracker entities but not creating actual devices?

e.g.
My new Hall Panel in Unifi:

Devices in HA (List filtered by Unifi Network and sorted by device name):

Entities in HA, note the default MAC address device tracker(s) which are disabled by integration:

System Options for Unifi Network:
image

Unifi Network config options:
image

Thanks for any help…

.

This is a global device tracker change introduced with HA 2022.2. A device tracker entity will not automatically create devices nor enable the tracker without another related entity already enabled in your Home Assistant instance.

Ok thanks, (that device tracker change does ring a bell now you mention it).
Does that mean that I need to manually enable the device tracker created by Unifi Network?
I can then rename it and assign an area etc.

As an aside, it probably doesn’t matter but this presumably means we will have devices now that wouldn’t normally have been created? Correct?

EDIT: And what then does the configuration option mean?
image

Yes

Not sure, they might be cleaned up. Do you observe any?

It still does what it says. I will probably rework many of these options in the future.

1 Like

Yes I do have one…

to be honest I don’t even know what this one relates to (I’m tempted to edit the storage files to remove it):
image

And while I’ve got you here… :wink:
Would it be possible to have the default device trackers take on the name (alias) given in Unifi Network rather than the MAC address?

EDIT: I take that back. It already happens once you enable it. Nice.
It would be nicer still if it would take the name without it being enabled.

Om not sure of that is possible. Else it would pro a ly already work

1 Like

I built a quick and dirty regex expression to select all the “junk” json objects in the .strorage/core.device_registry and .stroage/core.entity_registry files for deletion. Note these expressions only select the json objects. I used https://regexr.com/ and pasted the contents of each file as text with the matched regex expression and then selected the replace function under tools. For the replace expression I just used \n to replace the objects with a newline character.

Regex expression to select unkown Devices in .strorage/core.device_registry
(\{\s*"area_id":\s*null,\s*"config_entries":\s*\[\s*".*"\s*\],\s*"configuration_url":\s*null,\s*"connections":\s*\[\s*\[\s*"mac",\s*".*"\s*\]\s*\],\s*"disabled_by":\s*null,\s*"entry_type":\s*null,\s*"hw_version": null,\s*"id":\s*".*",\s*"identifiers":\s*\[\],\s*"manufacturer":\s*"",\s*"model":\s*null,\s*"name_by_user":\s*null,\s*"name":\s*"",\s*"sw_version":\s*null,\s*"via_device_id":\s*null\s*\},)

Regex expression to select unkown RX, TX & Uptime entities in .strorage/core.entity_registry
(\{\s*"aliases":\s*\[\],\s*"area_id":\s*null,\s*"capabilities":\s*null,\s*"config_entry_id":\s*".*",\s*"device_class":\s*null,\s*"device_id":\s*".*",\s*"disabled_by":\s*(".*"|null),\s*"entity_category":\s*"diagnostic",\s*"entity_id":\s*"sensor\.(uptime_.*|rx_.*|tx_.*)",\s*"hidden_by":\s*null,\s*"icon":\s*null,\s*"id":\s*".*",\s*"has_entity_name":\s*true,\s*"name":\s*null,\s*"options":\s*\{\},\s*"original_device_class":\s*("timestamp"|null),\s*"original_icon":\s*null,\s*"original_name":\s*"(Uptime|RX|TX)",\s*"platform":\s*"unifi",\s*"supported_features":\s*0,\s*"translation_key":\s*null,\s*"unique_id":\s*".*",\s*"unit_of_measurement":\s*(null|"MB")\s*\},)

EDIT: Use at own risk as homeassistant could freak out and not start if there are any errors in the json files.

Any idea or suggestion on how this can be rewritten to be used in Visual Studio Code rather than an online tool?

At first I did try and write it for VS Code, but was unsuccessful due to the parser not having all the features of regex. (see bellow) The main issue I ran into was unable to search for multi-line text. VS Code only supports JS regex.

Ah, that makes sense. Thank you for your explanation!

I ran into this last night and with the help of chatgpt I had a script composed that is supposed to clean things up. I input the the reply I replied to and went from there.

The script’s primary function is remove_junk_objects(file_path), which is designed to process a given JSON file (specified by file_path). The function reads the file, identifies and removes specific “junk” JSON objects based on certain criteria, and then writes the cleaned data back to the file. Additionally, a backup of the original file is created before any modifications.

Here’s the transcript of my struggle. https://chat.openai.com/share/26b43d32-025b-4467-8345-9685f734f01f

Here’s the script. It appears to work. You may have to adjust your path at the end (Example Usage) Use at your own risk.

#!/usr/bin/env python3
import json
import shutil
import os


def remove_junk_objects(file_path):
    try:
        print(f'Processing file: {file_path}')
        
        if not os.path.exists(file_path):
            print(f'Error: File {file_path} does not exist.')
            return
        
        # Create a backup of the original file
        backup_path = file_path + '.backup'
        shutil.copy(file_path, backup_path)
        print(f'Backup created at: {backup_path}')
        
        with open(file_path, 'r') as f:
            data = json.load(f)
        
        devices = data.get('data', {}).get('devices', [])
        original_length = len(devices)
        
        # Check if the file is the device registry
        if 'core.device_registry' in file_path:
            devices = [obj for obj in devices if not (
                obj.get('area_id') is None and
                obj.get('configuration_url') is None and
                obj.get('entry_type') is None and
                obj.get('hw_version') is None and
                obj.get('manufacturer') == "" and
                obj.get('model') is None and
                obj.get('name_by_user') is None and
                obj.get('name') == "" and
                obj.get('sw_version') is None and
                obj.get('via_device_id') is None
            )]
        
        data['data']['devices'] = devices
        
        with open(file_path, 'w') as f:
            json.dump(data, f, indent=4)
        
        print(f'Removed {original_length - len(devices)} junk objects from {file_path}')
    except Exception as e:
        print(f'Error encountered: {e}')
        print('Debugging information:')
        import traceback
        traceback.print_exc()


# Example usage
remove_junk_objects('/usr/share/hassio/homeassistant/.storage/core.device_registry')
remove_junk_objects('/usr/share/hassio/homeassistant/.storage/core.entity_registry')