Page 1 of 1

How I removed old devices from config.json

Posted: 04 Mar 2019 18:11
by piet66
Seems that more people have a lot of old devices in config.json and want to remove them.

It's not really necessary, but it's a kind of ballast. You can follow the way I did it, but on your own risk :) .

Start with installing my MxInfo tools and start ConfigJson.html. Tapping on the last button in first line you see if you have old devices.

The first idea I had was to remove the old devices in the program buffer, download the new config.json onto the local PC and exchange it manually on server PC. But I'm not sure whether it is save to do so.

So I did it in this way:
- in ConfigJson.html tap on the last button in the second row to store the list of old devices into a file on server.
- add the lines below to updateBackendConfig.js at to appropiate place.
- start zway server again and the old devices mentioned in the file are deleted

If you have too many old devices, only the first part is stored. There is any limit, maybe buffer length. So you must repeat the whole procedure (store file + restart server) maybe several times.

Code: Select all

// ----------- code added 2019-02-11 start ---------------------------------
// remove old devices from config.json
        var fil = 'MxInfoOldDevices.json';
        try {
            var devOldArray = loadObject(fil);
	    console.log("Info: cleaning config.json via "+fil);
            if (devOldArray.length > 0) {
                devOldArray.forEach(function(oldDevId) {
                    delete config.vdevInfo[oldDevId];
                    config.profiles.forEach(function(profile, ix) {
                        var indx;
                        indx = config.profiles[ix].dashboard.indexOf(oldDevId);
                        if (indx >= 0) {
                            config.profiles[ix].dashboard.splice(indx, 1);
                        indx = config.profiles[ix].hide_single_device_events.indexOf(oldDevId);
                        if (indx >= 0) {
                            config.profiles[ix].hide_single_device_events.splice(indx, 1);

                saveObject(fil, '[]');

        } catch(e) {
	    console.log("Error: cleaning config.json via "+fil+".json: ", e);
// ----------- code added 2019-02-11 end ---------------------------------

	  // Save changes
	  if (oldConfigJSON !== JSON.stringify(config)) { // do we need to update the config?
		try {
		  saveObject("config.json", config);
		} catch (e) {
		  console.log("Error: can not write back config.json to storage: ", e);
You can test this code before changing updateBackendConfig.js in ConfigJson.html tapping onto button 'Remove all old Devices'. This does it only in program buffer.

I added my enhanced version of updateBackendConfig.js for V2.3.7 to this post.

Re: How I removed old devices from config.json

Posted: 05 Mar 2019 14:49
by enbemokel
Hi Piet, I will try this sometimes, thanks for your work.

Re: How I removed old devices from config.json

Posted: 05 Mar 2019 18:15
by micky1500
Hello Piet,
I had 607 old devices.
It took 200 out each time, I had to run it 4 times.
Has tidied things up nicely.

Thank you.

If anyone wants the 2.3.8 modified file. Here it is. /opt/z-way-server/automation/updateBackendConfig.js
(5.83 KiB) Downloaded 110 times