HomeyScript runFlowCardAction for Home Assitant

I am trying to use a Home Assistant flowcard in a HomeyScript using the runFlowCardAction to call the ‘chime_tts.say’ service on HA from HomeyScript, but so far I have not gotten it to function.
I tried several things so far, but always get an error.

//Attempt 1

const MediaPlayer = "media_player.woonkamer"

action ={
    "id": "homey:app:io.home-assistant.community:callServiceEntity",
    "args": [
        {"service":"chime_tts.say"},
        {"entity":MediaPlayer},
        {"data":"{\"volume_level\": 0.56,\"fade_audio\": true,\"message\": \"Dit is een testje vanuit Homey\"}"}
    ]
}

test = await Homey.flow.runFlowCardAction(action)
console.log(test)

//Attempt 2

action ={
    "id": "homey:app:io.home-assistant.community:callServiceEntity",
    "args": {
        "service":"chime_tts.say",
        "entity":MediaPlayer,
        "data":"{\"volume_level\": 0.56,\"fade_audio\": true,\"message\": \"Dit is een testje vanuit Homey\"}"
    }
}

test = await Homey.flow.runFlowCardAction(action)
console.log(test)

//Attempt 2

action ={
    "id": "homey:app:io.home-assistant.community:callServiceEntity",
    "args": [
        {"variable":{"name":"service"},"value":"chime_tts.say"},
        {"variable":{"name":"entity"},"value":MediaPlayer},
        {"variable":{"name:":"data"},"value":"{\"volume_level\": 0.56,\"fade_audio\": true,\"message\": \"Dit is een testje vanuit Homey\"}"}
    ]
}

test = await Homey.flow.runFlowCardAction(action)
console.log(test)

From Homey web api:
→ runFlowCardAction
(async) runFlowCardAction(opts): Promise. then
POST /api/manager/flow/flowcardaction/:uri/:id/run

It expects 2 keys, so in your case i would assume it should be:

uri: ‘homey:app:io.home-assistant.community’,
id: ‘callServiceEntity’,

and not both in id:..

Hi, thanks, but that didn’t work.

When I run

log('===========');
log('Then-cards:');
log('===========');
const actionCards = await Homey.flow.getFlowCardActions();
for( const card of Object.values(actionCards) ) {
  log(card.titleFormatted || card.title)
  log(JSON.stringify({
    uri: card.uri,
    id: card.id,
    args: card.args,
    droptoken: card.droptoken,
    duration: card.duration,
  }, false, 2));
  log('-----------')
}

it returns the following:

Roep service [[service]] aan voor entiteit [[entity]] met data [[data]]
{
  "uri": "homey:flowcardaction:homey:app:io.home-assistant.community:callServiceEntity",
  "id": "homey:app:io.home-assistant.community:callServiceEntity",
  "args": [
    {
      "type": "autocomplete",
      "name": "service",
      "title": "Service",
      "placeholder": "Selecteer de dienst"
    },
    {
      "type": "autocomplete",
      "name": "entity",
      "required": false,
      "title": "Entiteit",
      "placeholder": "selecteer de entiteit"
    },
    {
      "type": "text",
      "name": "data",
      "required": false,
      "title": "Gegevens",
      "placeholder": "{\"parameter\": \"waarde\"}"
    }
  ],
  "droptoken": null,
  "duration": false
}

So I ran:

const MediaPlayer = "media_player.woonkamer"

action ={
    "uri": "homey:flowcardaction:homey:app:io.home-assistant.community:callServiceEntity",
    "id": "homey:app:io.home-assistant.community:callServiceEntity",
    "args": [
        {"service":"chime_tts.say"},
        {"entity":MediaPlayer},
        {"data":"{\"volume_level\": 0.56,\"fade_audio\": true,\"message\": \"Dit is een testje vanuit Homey\"}"}
    ]
}

test = await Homey.flow.runFlowCardAction(action)
console.log(test)

This returns the following:

{
  returnTokens: null,
  usedTokens: {},
  error: { message: "Cannot read properties of undefined (reading 'id')" },
  elapsedTime: 4.5726789981126785
}

I have been playing around with Homeyscript lately, but have done things different than you,
so having a bit hard time following the code.

Dont have HA so no chance testing myself, but regarding the Homey web api and the runFlowcardsAction the id and uri should be something like this:

const MEDIA_PLAYER = 'media_player.woonkamer';

(async () => {
  try {
    const result = await Homey.flow.runFlowCardAction({
      uri: 'homey:app:io.home-assistant.community',
      id:  'callServiceEntity',
      args: {
        service : 'chime_tts.say',
        entity  : MEDIA_PLAYER,
        data    : JSON.stringify({
          volume_level : 0.56,
          fade_audio   : true,
          message      : 'Dit is een testje vanuit Homey'
        })
      }
    });
    console.log('Service call OK ->', result);
  } catch (err) {
    console.error('Service call FAILED ->', err.message);
  }
})();

Play around with this, if still error, it is off my knowledge

Unfortunately this only works in ‘classic’ homeyscript.
Things have changed since HS uses homey-api instead of athom-api for new scripts (source)

uri and id are now combined as id:


(source of screenshot)

Second, I always first create the service (called ‘action’ nowadays) in the ‘Actions’ tab of HA Developer tools, to make sure it works:

Next, I check if this works with a real flow card:

So far, so good.
However, I can’t get the script to run successfully

This one errrors:

const test = await Homey.flow.runFlowCardAction({
    id: 'homey:app:io.home-assistant.community:callServiceEntity',
    args: {
      service: 'tts.google_translate_say',
      entity: 'media_player.yamaha_kamer',
      data: {
        'message':'hallo dit is homey',
        'language':'nl'
        }
    },
});

console.log(test);

Error:

{
  returnTokens: null,
  usedTokens: {},
  error: { message: 'Unexpected token o in JSON at position 1' },
 }

The issue seems to be the data part. The JSON should be converted to a string

So I tried toString, and the JSON.stringify Arnt used, but it’s not working yet;
This one errors as well:

const test = await Homey.flow.runFlowCardAction({
    id: 'homey:app:io.home-assistant.community:callServiceEntity',
    args: {
      service: 'tts.google_translate_say',
      entity: 'media_player.yamaha_kamer',
      data: JSON.stringify({
        'message': 'hallo dit is homey',
        'language': 'nl'})
    },
});

console.log(test);

Error:

{
  returnTokens: null,
  usedTokens: {},
  error: { message: "Cannot read properties of undefined (reading 'split')" },
}

I’m no coder or expert in this, I think I’m close but no cigar apparently :rofl:.
I’m out of ideas here.

This works fine however:

const test = Homey.flow.runFlowCardAction({
  id: 'homey:manager:mobile:push_text',
    args: {
        user: {
        id: 'xxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxx',
	    athomId: 'xxxxxxxxxxxxxxx',
	    },
	    text: 'Test push notification from HomeyScript'
        },
     });
console.log(test);