Flow Tokens allow users to pass information between different Flow cards when creating their Flows.
Tokens are typed variables that can be used throughout a Flow. A token is either:
Local — Attached to a Flow's trigger card, for example the name of the user that came home.
Global — Available anywhere, for example the current time.
Both tokens can be used in a Flow card's argument, such as a textfield.
In the User Interface, tokens are called Tags.
Local Tokens
When triggering a Flow, some information might be useful to use in that Flow. For example, when it starts raining, the mm/hour could be a token.
Tokens have a pre-defined type, which can be either string, number, boolean or image.
Users can use Flow Tokens in compatible Flow Arguments, such as a text field for string tokens. This way, the user can make advanced flows with variables. To add tokens to a Flow card, just add a property tokens to your Flow card manifest.
A Flow card can also have adroptoken field, droptokens are Flow Arguments that require the input to be a token. You can read more about droptokens in the Flow Arguments documentation.
A Then-card can optionally return tokens in an Advanced Flow. Similar to a When-card, specify the tokens property as an array, and return an object in the run-listener of your Flow card.
{"title": {"en":"Make it stop raining" },"hint": {"en":"Hires a shaman to do a sunny dance. Might cost some money." },"tokens": [ {"name":"shamanName","type":"text","title": {"en":"Name of the Shaman" } }, {"name":"shamanCost","type":"number","title": {"en":"Cost of the Shaman (€)" } } ]}
/app.js
conststopRainingAction=this.homey.flow.getActionCard('stop_raining');stopRainingAction.registerRunListener(async (args, state) => {awaitRainApi.makeItStopRaining();// Return the Tokens for Advanced Flowreturn { shamanName:'Alumbrada', shamanCost:10, }; });
When specifying a tokens array in your Then-card's JSON, that card will not be visible when creating & editing a standard Flow.
Global Tokens
A token can also be registered globally, so they can be used in any Flow. This means the token can be used without requiring the app to trigger the Flow. For example a weather app could expose the current temperature as a global token.
By default a device's capability are registered as global tokens.
Certain applications might want to share images between Flows, e.g. a webcam that made a snapshot, or a Chromecast that wants to cast an image. A token with type image can be used here.
/app.js
constHomey=require('homey');classAppextendsHomey.App {asynconInit() {constmyImage=awaitthis.homey.images.createImage();myImage.setPath(path.join(__dirname,"assets","images","kitten.jpg"));// create a token & register itconstmyImageToken=awaitthis.homey.flow.createToken("my_token", { type:"image", title:"My Image Token", });awaitmyImageToken.setValue(myImage);// listen for a Flow actionconstmyActionCard=this.homey.flow.getActionCard("image_action");myActionCard.registerRunListener(async (args, state) => {// get the contents of the imageconstimageStream=awaitargs.droptoken.getStream();this.log(`saving ${imageStream.contentType} to: ${imageStream.filename}`);// save the imageconsttargetFile=fs.createWriteStream(path.join(__dirname,"userdata", filename) );imageStream.pipe(targetFile);returntrue; });constmyTriggerCard=this.homey.flow.getTriggerCard("image_trigger");// pass the image to the trigger callawaitmyTriggerCard.trigger({ my_image: myImage }); }}module.exports= App;