Bulk renaming of related entities

I find it bothersome when I have to rename devices with many entities such as the one in the picture. It would be nice to have a “bulk renaming feature” that could rename all the related entities at once. I suggest the largest common naming-overlap between all entities is considered the “base name”, in this case (see picture) this would be “RT-AC1200G+” or “rt_ac1200g_”. I could then enter a new base-name, e.g. “office_router” and the base would have been replaced in all related entities all at once.

Often the base name of sensors and the like is just a seemingly random string anyway that does not make a lot of sense without renaming.

I think this already exists? I use it all the time, unless I’m misunderstanding what you’re asking for.

Go to Devices, pick a device. Click the pencil icon next to the device. Enter a new name, click Update. It will ask you if you want to rename all of the entity IDs, click “Rename” to do so. If the original name of the device has a sub-string match with the entity IDs, it will rename the IDs and their friendly name, according to the new name. Is that what you are looking for?

Note that for some reason this is considered “advanced” functionality, so you must have “Advanced Mode” enabled in your user profile in order to get the entity ID rename prompt. If advanced mode is off, you won’t get the extra prompt.

6 Likes

This renames the ‘friendly name’, not the entity.

1 Like

If I rename the device, then the entities are renamed too.
That is if the entities are still using there autogenerated name.
If I have renamed the entities manually then its not renamed with the device.

1 Like

It renames all of the entity friendly names, plus the IDs, as long as the sub-string match criteria is met.

1 Like

Oh damn! You are right! I cannot believe i have been renaming each entity name and entity ID individually for every entity! Thanks for the heads up

I can’t seem to find where to do that. Do you mean this one?

because there is no pencil icon:


(sorry for two posts, im not allowed to do it in one)

You have to go to the Device page of the device you want to rename. You are showing the Integrations and Device registry.

2 Likes

Does this take awhile to happen? I bough some WiZ bulbs and have renamed the devices. As initially mentioned, when I changed the device name, I selected “rename” to also rename the device entities. The friendly name of the entities is updating but not the entity_id. I’ve restarted my Yellow a few times now.

I’m curious too, I did a typo entering the name of a device in the companion app… Renaming the device didn’t seem to update the entities Ids.

I’m having the same issue. Even though clicking ‘rename’ on the Rename all the entities? This will break automations!-question, the entities of some devices refuse to rename.

I thought it could be related to the entities having ‘history’ records (e.g. graphs for temperature etc) but this is wrong. Renaming them one-by-one seems to work.

My other hunch is that this ‘sub-string match criteria’ @freshcoast spoke about does not always work out. For example, one of my devices is called Frient A/S Smoke Sensor SMSZB-120 with entities like sensor.frient_a_s_smoke_sensor_smszb_120_temperature. Perhaps it chokes on the / in that name trying to find entities to rename?

Another device i cant rename ‘all at once’ is called lumi.lumi Weather.

And one that did work was called ‘IKEA of Sweden TRADFRI Remote Control’ without any special characters.

Are we on to a little improvement here?

1 Like

For the cases where the automatic entity_id-renaming via changing the device name does not work, another way to bulk rename entities is to edit some text/yaml files manually.
I don’t know if this has any negative side-effects, but I’ve done this multiple times and have not noticed anything bad. You only lose the entity’s history, but that also happens when auto-renaming via the device name.

  1. Using a method of your choice, get access to the “config” folder (e.g. using the add-on “Samba share” or “Terminal & SSH”)
  2. Open .storage/core.entity_registry in a text editor
  3. Use search and replace to rename your entity.
    • e.g. I had many lines of this format: "entity_id": "binary_sensor.lumi_lumi_sensor_magnet_aq2_opening",, so I searched for lumi_lumi_sensor_magnet_aq2 and replaced with kitchen_window
  4. If needed, perform the same search and replace on automations.yaml, scripts.yaml, and whatever other yaml files (outside of .storage) you are using the renamed entities in. I usually do a grep/search for the old entity-prefix to find the files I need to modify.
  5. Restart home assistant. I use the shell command ha core restart for this.
  6. Enjoy
7 Likes

I now developed a python script to bulk rename entities with regular expressions and without losing their history:

3 Likes

I just finish renaming lots of my entities with the “Rename all the entities” feature when renaming a device. I actually had to go through the sources of the frontend to find out, what exactly the “sub-string match criteria” means. So, I thought, I’d come back here and drop a note:

The criterion means that all entities will be renamed, if they (a) belong to the device and (b) contain the slugified device name in their entity id. They will be renamed by replacing the old device name by the new device name. So, some_domain.bla_olddevice_battery_2 will become some_domain.bla_newdevice_battery_2.

2 Likes

I was able to install run this from one of my WSL instances and it works great! Thanks.

I do have one question, trying to run this for a friend of mine who only has access via SSH in HassOS, but doing that reveles that Python is not installed. Is it advisible to install Python3 in HassOS? Any other options?

image

Amazing! Thank you so much! :+1:

I’m having this issue, I click on the pencil icon in the device name to rename, but I never get the question to rename all entities at all. Using hass 2024.2.2. Any ideas of what could be wrong? I want to do a mass rename of devices to have a common naming scheme and it would be impossible to do if this doesn’t work. I’d rather not run an “old script posted in the internet” for reliability reasons, don’t want to end up with a messed up installation.

Edit: OK, I see @five5607’s script actually uses the API, it doesn’t mess with the YAML files, so I guess it should be safe, but still if there is supposed to be an official way to do it I’d rather do that.

I don’t really get how he uses Home Assistant if he only has access via SSH, but of the top of my head I see two options:

  1. Install python into the container either temporarily with apk add python3 or permanently as documented here.
  2. He could use ssh port forwarding:
    • Change AllowTcpForwarding no to yes in /etc/ssh/sshd_config (I recommend to revert this change afterwards)
    • Restart the ssh server (I didn’t see a nice way of doing this. killall sshd worked. :wink: )
    • forward homeassistant.local:8123 to localhost:8123: ssh -N -L 8123:homeassistant.local:8123 your-hassos-server
    • Point my script to localhost:8123 and use it like normal.

Sometimes I used to have an issue when trying to change bulk entity ids by changing the device name due to the "Do you also want to rename the entity IDs of your entities?" feature not working. This seems to happen when the device name and the device name portion of the entity id do not match. For anyone running into the same issue the solution is to rename the device name so it matches the device name portion of the entity id and then rename it again the way you want.

  1. Click on one of the device entities and copy the device name portion of the entity id. Ex: In sensor.echo_dot_next_reminder copy the echo_dot portion.
  2. Rename the device to the copied name. Ex: echo_dot.
  3. If you check one of the device entity ids it should now match the device name.
  4. Rename your device again but this time use the name you want.
  5. When you get the prompt "Do you also want to rename the entity IDs of your entities?" make sure you click RENAME.
  6. All of the device entity ids should now include the device name.
1 Like