Tokens
Flow Tokens allow users to pass information between different Flow cards when creating their Flows.
Last updated
Was this helpful?
Was this helpful?
{
"title": { "en": "It starts raining" },
"tokens": [
{
"name": "mm_per_hour",
"type": "number",
"title": { "en": "mm/h" },
"example": 5
},
{
"name": "city",
"type": "string",
"title": { "en": "City" },
"example": { "en": "Amsterdam" }
}
]
}const Homey = require('homey');
const RainApi = require('rain-api');
class App extends Homey.App {
async onInit() {
const rainStartTrigger = this.homey.flow.getTriggerCard("rain_start");
RainApi.on('raining', (city, amount) => {
const tokens = {
mm_per_hour: amount,
city: city
};
rainStartTrigger.trigger(tokens)
.then(this.log)
.catch(this.error);
});
}
}
module.exports = App;import Homey from "homey";
import RainApi from "rain-api";
export default class App extends Homey.App {
async onInit(): Promise<void> {
const rainStartTrigger = this.homey.flow.getTriggerCard("rain_start");
RainApi.on("raining", (city: string, amount: number) => {
const tokens = {
mm_per_hour: amount,
city: city
};
rainStartTrigger.trigger(tokens)
.then(this.log)
.catch(this.error);
});
}
}
from homey import app
from rain_api import RainApi
class App(app.App):
async def on_init(self) -> None:
rain_start_trigger = self.homey.flow.get_trigger_card("rain_start")
async def on_raining(city: str, amount: float) -> None:
tokens = {
"mm_per_hour": amount,
"city": city
}
try:
self.log(await rain_start_trigger.trigger(tokens))
except Exception as e:
self.error(e)
RainApi.on("raining", on_raining)
homey_export = App
{
"title": {
"en": "Make it stop raining"
},
"hint": {
"en": "Hires a shaman to do a sunny dance. Might cost some money."
},
"tokens": [
{
"name": "shamanName",
"type": "string",
"title": {
"en": "Name of the Shaman"
}
},
{
"name": "shamanCost",
"type": "number",
"title": {
"en": "Cost of the Shaman (β¬)"
}
}
]
} const stopRainingAction = this.homey.flow.getActionCard('stop_raining');
stopRainingAction.registerRunListener(async (args, state) => {
await RainApi.makeItStopRaining();
// Return the Tokens for Advanced Flow
return {
shamanName: 'Alumbrada',
shamanCost: 10,
};
}); type StopRainingTokens = {
shamanName: string;
shamanCost: number;
};
const stopRainingAction = this.homey.flow.getActionCard("stop_raining");
stopRainingAction.registerRunListener(async (args, state): Promise<StopRainingTokens> => {
await RainApi.makeItStopRaining();
// Return the Tokens for Advanced Flow
return {
shamanName: "Alumbrada",
shamanCost: 10,
};
});from homey import app
from rain_api import RainApi
class App(app.App):
async def on_init(self) -> None:
stop_raining_action = self.homey.flow.get_action_card("stop_raining")
async def run_listener(card_arguments, **trigger_kwargs) -> dict:
await RainApi.make_it_stop_raining()
# Return the Tokens for Advanced Flow
return {
"shamanName": "Alumbrada",
"shamanCost": 10,
}
stop_raining_action.register_run_listener(run_listener)
homey_export = App
const Homey = require('homey');
class App extends Homey.App {
async onInit() {
const myToken = await this.homey.flow.createToken("my_token", {
type: "number",
title: "My Token",
});
await myToken.setValue(23.5);
}
}
module.exports = App;import Homey from "homey";
export default class App extends Homey.App {
async onInit(): Promise<void> {
const myToken = await this.homey.flow.createToken("my_token", {
type: "number",
title: "My Token",
});
await myToken.setValue(23.5);
}
}
from homey import app
class App(app.App):
async def on_init(self) -> None:
my_token = await self.homey.flow.create_token("my_token", "number", "My Token")
await my_token.set_value(23.5)
homey_export = App
const Homey = require('homey');
class App extends Homey.App {
async onInit() {
const myImage = await this.homey.images.createImage();
myImage.setPath(path.join(__dirname, "assets", "images", "kitten.jpg"));
// create a token & register it
const myImageToken = await this.homey.flow.createToken("my_token", {
type: "image",
title: "My Image Token",
});
await myImageToken.setValue(myImage);
// listen for a Flow action
const myActionCard = this.homey.flow.getActionCard("image_action");
myActionCard.registerRunListener(async (args, state) => {
// get the contents of the image
const imageStream = await args.droptoken.getStream();
this.log(`saving ${imageStream.meta.contentType} to: ${imageStream.meta.filename}`);
// save the image
const targetFile = fs.createWriteStream(
path.join("/userdata", imageStream.meta.filename)
);
imageStream.pipe(targetFile);
return true;
});
const myTriggerCard = this.homey.flow.getTriggerCard("image_trigger");
// pass the image to the trigger call
await myTriggerCard.trigger({ my_image: myImage });
}
}
module.exports = App;import Homey, { type Image } from "homey";
import path from "node:path";
import * as fs from "node:fs";
type ImageActionArgs = {
droptoken: Image;
};
export default class App extends Homey.App {
async onInit(): Promise<void> {
const myImage = await this.homey.images.createImage();
myImage.setPath(path.join(__dirname, "assets", "images", "kitten.jpg"));
// create a token & register it
const myImageToken = await this.homey.flow.createToken("my_token", {
type: "image",
title: "My Image Token",
value: myImage,
});
// listen for a Flow action
const myActionCard = this.homey.flow.getActionCard("image_action");
myActionCard.registerRunListener(async (args: ImageActionArgs, state) => {
// get the contents of the image
const imageStream = await args.droptoken.getStream();
this.log(`saving ${imageStream.meta.contentType} to: ${imageStream.meta.filename}`);
// save the image
const targetFile = fs.createWriteStream(path.join("/userdata", imageStream.meta.filename));
imageStream.pipe(targetFile);
return true;
});
const myTriggerCard = this.homey.flow.getTriggerCard("image_trigger");
// pass the image to the trigger call
await myTriggerCard.trigger({ my_image: myImage });
}
}
import os
from typing import TypedDict
from homey import app
from homey.image import Image
class ImageActionArgs(TypedDict):
droptoken: Image
class App(app.App):
async def on_init(self) -> None:
my_image = await self.homey.images.create_image()
my_image.set_path(
os.path.join(os.path.dirname(__file__), "assets", "images", "kitten.jpg")
)
my_image_token = await self.homey.flow.create_token(
"my_token", "image", "My Image Token", my_image
)
my_action_card = self.homey.flow.get_action_card("image_action")
async def run_listener(card_arguments: ImageActionArgs, **trigger_kwargs):
image_stream = await card_arguments.get("droptoken").get_stream()
self.log(
f"saving {image_stream['meta']['contentType']} to {image_stream['meta']['filename']}"
)
# save the image
with open(
os.path.join("/userdata", image_stream["meta"]["filename"]), "wb"
) as target_file:
target_file.write(image_stream["data"].buffer.read())
my_action_card.register_run_listener(run_listener)
my_trigger_card = self.homey.flow.get_trigger_card("image_trigger")
# pass the image to the trigger call
await my_trigger_card.trigger({"my_image": my_image})
homey_export = App