# Correct way to define/use Action cards

**URL:** <https://community.homey.app/t/correct-way-to-define-use-action-cards/96068>\
**Category:** Developers\
**Tags:** flow, homey-pro\
**Created:** [November 28, 2023, 7:36am UTC](https://community.homey.app/t/correct-way-to-define-use-action-cards/96068 "2023-11-28T07:36:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johan\_Rossouw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/johan_rossouw/32/79349_2.png) [@Johan\_Rossouw](https://community.homey.app/u/Johan_Rossouw)\
**Post date:** [November 28, 2023, 7:36am UTC](https://community.homey.app/t/correct-way-to-define-use-action-cards/96068/1 "2023-11-28T07:36:05Z")

</div>

Hello,

I am struggeling a bit with geting action cards to work correctly. It works fine as long as there is only one device for the specific driver, but the moment I add a second device things go wrong. It appears that my listeners are attached to the driver and not the individual device instances.

I define the action in the driver.flow.compose.json file:

```auto
"actions": [
  {
            "id": "alarm_duration",
            "title": {
                "en": "Switch on siren for X Seconds"
            },
            "titleFormatted": {
                "en": "Switch on siren for [[duration]] Seconds"
            },
            "args": [
                {
                    "type": "number",
                    "name": "duration",
                    "min": 0,
                    "max": 65535,
                    "step": 1,
                    "label": {
                        "en": "Seconds"
                    },
                    "placeholder": {
                        "en": "120"
                    },
                    "title": {
                        "en": "Duration of alarm in seconds"
                    }
                }
            ]
        },

```

Then in the device.js in the onNodeInit method I register the callback like this:

```auto
const actionAlarmDuration = this.homey.flow.getActionCard('alarm_duration');
    actionAlarmDuration.registerRunListener(async (args, state) => {
      this.log('FlowCardAction Set Alarm Duration to: ', args.duration);
      this.sendAlarmDuration(args.duration);
    });

```

This works correctly for the 1st device, but once the second device initialises there is a warning that the listener was already registered, so it seems the listener is attached to the driver and not the device instance.

When I then call this action card from a flow - the setting is always updated against one instance of this device type, irrespective of the device i reference in the flow.

I would appreciate any help - I could not find much documentation describing how this should work.

The source code for the driver can be found here:

[https://github.com/jrossouw/page.rossouw.homey.test](https://github.com/jrossouw/page.rossouw.homey.test)

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [November 28, 2023, 11:40am UTC](https://community.homey.app/t/correct-way-to-define-use-action-cards/96068/2 "2023-11-28T11:40:08Z")

</div>

You need to check the state vs args to check if its a certain device or not.

---

<div class="post-metadata">

**Author:** ![OH2TH](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/oh2th/32/111414_2.png) [@OH2TH](https://community.homey.app/u/OH2TH)\
**Post date:** [November 28, 2023, 11:43am UTC](https://community.homey.app/t/correct-way-to-define-use-action-cards/96068/3 "2023-11-28T11:43:36Z")

</div>

You might want to move this topic to the “Development” category, and remove the `[APP][Pro]` from the topic title, as that is reserved for app specific topics.

---

<div class="post-metadata">

**Author:** ![Johan\_Rossouw](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/johan_rossouw/32/79349_2.png) [@Johan\_Rossouw](https://community.homey.app/u/Johan_Rossouw)\
**Post date:** [November 28, 2023, 12:44pm UTC](https://community.homey.app/t/correct-way-to-define-use-action-cards/96068/4 "2023-11-28T12:44:28Z")

</div>

Thank you @Arie_J_Godschalk - you put me on the right track. I needed to use args.device - it works now.

```auto
const actionAlarmDuration = this.homey.flow.getActionCard('alarm_duration');
    actionAlarmDuration.registerRunListener(async (args, state) => {
      this.log('FlowCardAction Set Alarm Duration to: ', args.duration);
      args.device.sendAlarmDuration(args.duration);
    });

```
