Homey Community Forum

How to call flows by name in an action card with a logic variable via homeyscript

I tried to start a flow (Flowstart) with its name in homeyscript and got the following error:

Homey.flow.runFlowCardAction({
uri: ‘homey:manager:flow’,
id: ‘programmatic_trigger’,
args: {
name: ‘Flowstart’,
type: ‘autocomplete’,
},
});

Script Error
:warning: Cannot read property ‘id’ of undefined: Cannot read property ‘id’ of undefined
at /node_modules/athom-api/dist/index.js:1:1186403
at processTicksAndRejections (internal/process/task_queues.js:97:5)

I tried to find a solution in this forum and documentation, but I am a little bit lost.

Is it even possible to do it this way?
What did I wrong?

THX

Can you give me a hint where you found this command? In AppAPI documentation?

I just found this:

If I’m right, you get he flowcard with Homey.flow.getTriggerCard(id) and call method trigger() to trigger the flow.

I didn’t test it, so it’s just my thought about the Api…perhaps it’s gettimg you on the right way.

1 Like

Hi Ronny,

  1. I start with the examples, on github (same as homey->webapp->homeyscript):
    com.athom.homeyscript/flowcard-run.js at master · athombv/com.athom.homeyscript · GitHub
// Run 'Show Animation' Flow 'Then' Card
// This will pulse Green (#00FF00) when it's a weekend
// or pulse Red (#FF0000) when it's a weekday
await Homey.flow.runFlowCardAction({
  uri: 'homey:manager:ledring',
  id: 'show_animation',
  args: {
    animation: 'pulse',
    color: isWeekend
      ? '#00FF00'
      : '#FF0000',
  },
});
  1. with the following example it’s possible to show all options for and+then cards of the homey environment:
    com.athom.homeyscript/flowcard-list.js at master · athombv/com.athom.homeyscript · GitHub

there I found:

Start [Flow start]
{
  "uri": "homey:manager:flow",
  "id": "programmatic_trigger",
  "args": [
    {
      "name": "flow",
      "type": "autocomplete"
    }
  ]
}
  1. My adaption of both examples was:
await Homey.flow.runFlowCardAction({
  uri: 'homey:manager:flow',
  id: 'programmatic_trigger',
  args: {
          name: 'Flowstart',
          type: 'autocomplete',
  },
});

But I think because the flow execution card is from type “autocomplete” (a catalog of all flows) it doesen’t work. Maybe the api doesn’t provide it.

await Homey.flow.runFlowCardAction({
  uri: 'homey:manager:flow',
  id: 'programmatic_trigger',
  args: { flow: { id : ID_OF_FLOW_TO_RUN } }
});
2 Likes

Indeed @robertklep this works very well:

await Homey.flow.runFlowCardAction({

  uri: 'homey:manager:flow',
  id: 'programmatic_trigger',

  args: { flow: { id : 'd86acee8-59b1-4fdf-948f-xxxx'} }

});

Thank you very much! Could you provide us with the information were you find this in the documentation, please.

And for the others who are not so familiar with homeyscript like me. You can find out the ID of the flows with:

Homey.flow.getFlows();

I didn’t, I used reasoning to find out how it’s supposed to be called:

  • the declaration of the action states that is has an argument named flow of type autocomplete
  • if you know the “Start a flow” card from the flow editor, you know that you have to pick a flow to start with an autocomplete form
  • I assumed that it would store the “flow object”, or at least the unique identifier for a flow (id), to be able to start the flow
  • hence, I tried calling the action card using an argument called flow with an object that contains (at least) id, which worked
1 Like

Ok, it’s your experience, thx for your description, I learnt a lot.

And reached my goal to call flows by name in an action card with a logic variable via homeyscript:

/*
 * This script starts a flow by Flow Name with a logic variable in a 'Then' Flow Card.
 * This script overcomes the disadvantage of the manual Flow Start Card,
 * which currently (6-17-2021) does not allow you to use tags to start other flows.
 */

// homeyscript action card argument -> flow name
const flow_name = (typeof args[0] === 'string')
  ? args[0]
  : 'dummy';

// filter flow object by flow name
const flow_obj = await Homey.flow.getFlows({ filter: {name: flow_name}});

// fetch flow id from flow object
const flow_id = Object.keys(flow_obj)[0];

// start flow
if (flow_id){
  await Homey.flow.runFlowCardAction({
    uri: 'homey:manager:flow',
    id: 'programmatic_trigger',
    args: { flow: { id: flow_id } }
  });
}

Edit: improved code a little bit

image

flow-start = homeyscript name
BW* = logic text variable with flow name

3 Likes

Very interesting. So, from what I understand your goal is:
you can start flow X from flow A, while the name of flow X depends on the value of a variable?

Exactly, that’s the charm vs. the manual configuration of a flow start because in this case you can’t use tags.

Reduces the flow amount immense. My use case is an irrigation system with a lot of cascading flows which are reused for each of 12 cycles.

So with this configuration the finish of a timer flow starts other flows dynamically. Which means I need only this one timer flow and not 12 of them which start distinct other flows.

1 Like