Homey Community Forum

How do I use Homey.flow.updateFlow() in a homeyscript

I’m trying to re-enable disabled flows and here I have a simplified sample of the script:

// Get all flows
let flows = await Homey.flow.getFlows();

// Iterate all flows
_.forEach(flows, flow => {
    // Find flows that are disabled
    if(!flow.enabled) {
        console.log(flow.id + ' ' + flow.name + ' ' + flow.enabled);
        // Enable the flow
        flow.enabled = true;
        Homey.flow.updateFlow({id: flow.id});
    }
});

return true;

Now that prints out the disabled flows nicely, but how do i set flow.enabled = true in that loop and update the flow. The Web API documentation doesn’t really give me the clue how to use updateFlow().
http://developer.athom.com/docs/api/HomeyAPI.ManagerFlow.html#updateFlow

Somehow I do not understand the document syntax and how to use those methods and opts.

Is it even possible to enable said flows from homeyscript?

The documentation states that updateFlow take an optional (I have no idea why it’s optional, doesn’t make sense) argument opts, which is an object that should have two parameters (“properties” is the correct term): id and flow.

Which means this should work:

Homey.flow.updateFlow({ id : flow.id, flow })

(where flow is short for flow: flow)

Since the method is async, you may run into performance issues if you have lots of flows that need to be enabled in the same loop. In that case, you can do this to update each flow sequentially (instead of in parallel, which your code is doing now):

_.forEach(flows, async flow => {
  ...
       await Homey.flow.updateFlow({id: flow.id});
  ...
});

This was possible before Homey 2.0
even with the correct code you can’t as HomeyScript has not the correct Permission in Homey >= v2.0:

You need:
Required OAuth scopes:

  • homey.flow

but only have Readonly as stated in HomeyScript scopes / authorization

o, and BTW: You can from the Web API Playground, and see teh Disable/enable all Apps fe.

1 Like