Homeyscript functionality

I’m struggling regularly with the shortcomings of the homey flows. For instance the lack of nested if or elsif functionality has driven me to several workarounds.
Also not being to control float and integer has driven me nuts.
Now I understood that Homeyscript should solve that and I am trying to understand how it works. Is for instance the below assumption true?
I can have a flow triggered by an event(eg variable changing or a time) then execute a script in which I have access to all variables and device parameters which are also available in Homey Flow. Use these in if then else statements and write values to it like in Homey flow

1 Like

AFAIK you don’t have access to variables, unless you pass them as argument to the Homeyscript. Getting device capability values is possible, not sure if you can set them though.

Sure you can :slight_smile:

let flowTokens = await Homey.flowToken.getFlowTokens();
console.log(flowTokens);

let variables = await Homey.logic.getVariables();
console.log(variables);

3 Likes

Nice, thanks for the correction :smiley:

1 Like

Nice indeed. I’m going to play around with it.
Thanks a lot.

Sure you can :slight_smile:

let flowTokens = await Homey.flowToken.getFlowTokens();
console.log(flowTokens);

@johan_bendz
How can i read the value of a specific token within flowTokens, if i know the id?

I would like to write a tag / flow token using await tag(“TageName”,value)
and use your method to read them back.

Bart

You will have to select the token uri and id like this:

  await Homey.flowToken.getFlowTokens().then(function(tokens) {
    for (let token in tokens) {
      if ( tokens[token].uri == "homey:manager:weather" && tokens[token].id == "state"  ) {
        var mytoken = tokens[token].value;
        console.log(mytoken);
      }
    }
  }).catch(console.error);
2 Likes

Is it also possible to change the value and save it. It is possible with better logic, would be nice if it is also possible with logic

If you want to work with Homey Logic’s variables they are found here: Homey.logic.getVariables()

To work with tokens in other apps this is a short guide:

// Define the token you want to add to the app
let tokenName = “mytoken”;
let tokenValue = 12345;
// Define the app you want to add the token to
let tokenApp = “net.i-dev.betterlogic”;
// get the current tokens from the app
let token = await Homey.apps.getApp({id:tokenApp});
// save the new token to the app
token.apiPut("/" + tokenName + “/” + tokenValue );
// write the output info
console.log(“The token: “+tokenName+” saved to app “+tokenApp+” with the value:”+tokenValue);

Or use the getFlowToken of getVariable function?

const FlowToken = await Homey.flowToken.getFlowToken({uri: '<uri here>', id: '<id here>'});
console.log(FlowToken.value);

const Variable = await Homey.logic.getVariable({id: '<id-here>'});
console.log(Variable.value);
1 Like

thx Johan, i already use better logic “net-i-dev.betterlogic” but what should i use when i want to save them in homey logic. com.athom.logic?

Yes, that works too. Many ways to do this. : )

1 Like

I posted the question on GitHub as well and this is Jeroen Wienk’s answer. I thought good to share here because “there are many ways to do this’ :grinning: and because as Jeroen mentioned values are cached and he gives the solution to circumvent this.

It’s possible with the API but it’s not a very clear solution since the values are cached. But you can do this;

await tag('my-tag', false);

// value is false
console.log(await Homey.flowToken.getFlowToken({
  uri: 'homey:app:com.athom.homeyscript',
  id: 'my-tag'
}));

await tag('my-tag', true);

// value is false (cached)
console.log(await Homey.flowToken.getFlowToken({
  uri: 'homey:app:com.athom.homeyscript',
  id: 'my-tag'
}));

// value is true (skipped cache)
console.log(await Homey.flowToken.getFlowToken({
  $skipCache: true,
  uri: 'homey:app:com.athom.homeyscript',
  id: 'my-tag'
}));

Notice the last call with $skipCache.

So apparently it depends on the use case if you want to call the value between multiple executions or call the value you just updated within the script.

6 Likes

Too bad this isn’t documented anywhere :frowning: But thanks for letting us know!

1 Like

Hello, could you elaborate on passing a variable (string, integer) to Homeyscript? Could you show an example?

Use the card " start a script with an argument "

2 Likes