monotux.tech


Mass remove devices in Home Assistant

home-assistant, javascript, 433MHz

At one point I though letting RFXTRX listen to all 433 MHz devices around, and add them to Home Assistant, was a good idea. I thought I could eavesdrop on all temperature sensors and combine them into an approximation of the real outdoor temperature, or so I thought.

Now I’ve finally found a piece of code that lets me remove the hundreds and hundreds of devices RFXTRX had autocreated in my Home Assistant.

I found the following piece of code at the Home Assistant community forums:

async function deleteDevices(model_to_delete) {
    let hass = document.querySelector("home-assistant").hass;
    let message = { type: "config/device_registry/list" };
    let count = 0;

    await hass.callWS(message).then(async(response) => {
        for (let i = 0; i < response.length; i++) {
            device = response[i];
            if (device["model"] === model_to_delete) {
                await hass.callWS({
                    "type":"config/device_registry/remove_config_entry",
                    "device_id": device["id"],
                    "config_entry_id": device["config_entries"][0],
                }).then((response) => {
                    console.log(`Deleted device ${JSON.stringify(device)}`);
                    count++;
                })
            }
        }
        console.log(`Deleted ${count} devices`);
    })
}
// replace the string with the model name you want to remove
await deleteDevices("RM174RF Smoke Detector");

To use it, just paste in the code snippet in your browsers developer console (F12 or such) and run the last line until you are done.

Patrick Decat, thank you very, very much!