Correct way to define/use Action cards

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:

"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:

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

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

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.

1 Like

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

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);
    });
1 Like