[HOW-TO][Pro][Cloud] - Tool to FIX (advanced) flows after removing and re-adding devices

Use at own risk!


Hi,
So I just go a new house and wanted to make my Zigbee network a bit better by removing an re-adding my devices so I get a stable network.

But the issue with this is you have to fix all your flows. (Ofcourse Flow Checker App for Homey | Homey can indicate the broken flows)

I made a small script which can help with fixing the flows.

So what do you need:

  1. Device ID you would like to update - You can get this via the webAPI playground:

Homey.devices.getDevices();

  1. Write down the Device ID
  2. Remove the Device
  3. Re-add the Device
  4. Get the Device ID of the new paired device: - You can get this via the webAPI playground:

Homey.devices.getDevices();

  1. Run this script in the WebAPI playground: (multiple devices possible)
const oldIds = ['e63c7964-2e67-4347-b2e0-d17a9e4df7e9'];
const newIds = ['221b7559-aeb9-4fbb-a053-acc859dcde6c'];

const main = async function () {
    const flows = await Homey.flow.getFlows();

    Object.values(flows).forEach(async (f) => {
        if (f.trigger && f.trigger.uri) {
            const trigger = f.trigger;
            
            const replaceTrigger = trigger.uri.replace('homey:device:', '');
            
            if(oldIds.includes(replaceTrigger)) {
              const index = oldIds.findIndex(o => o.includes(replaceTrigger));
              trigger.uri = trigger.uri.replace(oldIds[index], newIds[index]);
              
              await Homey.flow.updateFlow({ id: f.id, flow: { id: f.id, trigger: trigger } });
            }
            
            
        }

        f.actions.forEach(async (a, i) => {
            const replaceAction = a.uri.replace('homey:device:', '');
            if(oldIds.includes(replaceAction)) {
                const actions = f.actions;
                const index = oldIds.findIndex(o => o.includes(replaceAction));
                actions[i].uri = actions[i].uri.replace(oldIds[index], newIds[index]);
                await Homey.flow.updateFlow({ id: f.id, flow: { id: f.id, actions: actions } });
            }
        });

        f.conditions.forEach(async (a, i) => {
            const replaceCondition = a.uri.replace('homey:device:', '');
            if(oldIds.includes(replaceCondition)) {
                const conditions = f.conditions;
                const index = oldIds.findIndex(o => o.includes(replaceCondition));
                conditions[i].uri = conditions[i].uri.replace(oldIds[index], newIds[index]);
                await Homey.flow.updateFlow({ id: f.id, flow: { id: f.id, conditions: conditions } });
            }
        });
    });


    const advanced_flows = Object.values(await Homey.flow.getAdvancedFlows());

    advanced_flows.forEach(async (af) => { 
            const cards = af.cards;
            for (const c in af.cards) {
                const replaceId = cards[c].ownerUri ? cards[c].ownerUri.replace('homey:device:', '') : null;
                if(replaceId && oldIds.includes(replaceId)) {
                    const index = oldIds.findIndex(o => o.includes(replaceId));
                    cards[c].ownerUri = cards[c].ownerUri.replace(oldIds[index], newIds[index]);   
                }
            }

            await Homey.flow.updateAdvancedFlow({ id: af.id, advancedflow: { cards: cards } });
    });
};

main();


Contributions
If you appreciate what I do, contribute to future development by making a paypal contribution
or buy me a coffee on Ko-Fi or Bunq.me

image ko-fiimage

17 Likes

Maestro, :pray::pray::pray:

Congrats with your new Home(y).

My P1 dongle did not respond anymore. In short, only option was to remove and re-add it. That’s a PITA when lots of flows are involved.
BUT, your brilliant script saved the day, Martijn.

I saved the dongle’s ID and removed it.
(To me, the device ID is easily / easier found by using the devices search function.)
After re-adding the dongle, I searched for its new ID and I updated old & new ID’s in your script, and ran it.

:white_check_mark::tada: it did the trick! 20 flows got fixed!

Note: The flows with broken variables did not fix themselves, so maybe here’s is a new challenge :grimacing: (Warning: it does not have to be fixed today, or tomorrow :upside_down_face::crazy_face:)

Oh, a suggestion to the topic subject:
[HOW-TO][Pro][Cloud] - Tool to FIX flows after removing and re-adding devices
And it’s not limited to zigbee devices btw.

All the best,
Peter

1 Like

If this works with remove and re-ad the device this also can work with a compleet new device?
Like a broken Aqara temperature or doorsensor and you change it for the same type of device.
Use this script and you’re ready again.

1 Like

@Mike1233 yes it does, as long as the devices are using the same flowcards

2 Likes

Hi Martijn,

Is it possible to run it with HomeyScript?
(Well, I tried but nothing changes hehe).
I just don’t understand why the web api playground and HS can’t run the same scripts :grimacing::nerd_face:

1 Like

Depends on the access roles. Web API has more rights

1 Like

Added advanced flows support :slight_smile:

2 Likes

This is great! Thanks a lot!

1 Like

Hi Martijn,
If I want to fix multiple devices at once, is this the correct way to enter the ID’s?

const oldIds = ['e63c7964-2e67-4347-b2e0-d17a9e4df7e9',
'e63c7964-2e67-4347-b2e0-d17a9e4df7e8',
'e63c7964-2e67-4347-b2e0-d17a9e4df7e7'
];

@Peter_Kawa yes that’s correct.

Make sure that the first ID of the old array is the same device as the first ID of the new array :wink:

:call_me_hand:that makes perfectly sense, Martijn. Thanks.

Great Tool!! Thanks a lot !!

1 Like

Really nice Martijn!
Saved me allot of time today!

1 Like

Thanks,!!!