Setting Logic Variable by HomeyScript

Homey.logic controls Homey’s built-in logic system, not Better Logic (which is a third-party app). See @JPe4619’s comment about how to set BL variables.

Also, HomeyScript has certain API limitations that the API Playground doesn’t have, hence the “You have no access to do this” error.

I’m really sorry, it was my mistake …
All i wrote is referred to the standard homey logic not “better logic”

Thank you anyway

Enrico

1 Like

Can someone supply me with an example code how I can change a global variable or “tag” inside HomeyScript? I’m trying to change these tags:
homey-tags
I’ve managed to read the variables:
HomeyScript-GlobalVariable
But I have no clue how to change them inside HomeyScript, as you can see from the console window, the value does not get updated. I’ve seen several posts about it, but all the scripts doesn’t seem to work or the thread is abandoned.
Homey.logic.updateVariable({id: Logic.id, variable: {'value' : 'TEST!'}})

Doesn’t seem to work… but maybe I have mistaken the syntax. Anyone can provide me a working syntax?

Homey script isn’t allowed to change variable values, only retrieve them.

Seriously? There isn’t a way to store / read a global variable out of homey script? (it doesn’t have to be these tags inside homey).

@Satoer The good news is, you can make your own variables in HomeyScript and they also can be used in your Flows, here is my example:

// my12HoursClock
let sys = await Homey.system.getInfo();
var theTime = new Date(sys.date);
var theHour = theTime.getHours();
if (theHour > 12) { theHour = theHour - 12};
await setTagValue("klok12h", {type:"number", title: "Klok12H"}, theHour );
//console.log('TheHour',theHour);
return(true);

image

Not store, you can read, like you needed to do :stuck_out_tongue_winking_eye:

So with the Syntax:
await setTagValue(“klok12h”, {type:“number”, title: “Klok12H”}, theHour );
it will create a variable and store the data in that… And if it already exists it will update the value? And I can retrieve this value at a later stage? (with another HomeyScript).
How can I retrieve the value?

No, I need to store a variable (integer / string / boolean). And retrieve this data in a later stage with another HomeyScript.

My example is running every hour to speak the time via Homey speaker:

Yes, but isn’t the tag / variable only created when you use it in the flow… Can I retrieve this tag / variable with another script at a later time?

try to find out, I’ve never tried this.

The code:
await setTagValue(“klok12h”, {type:“number”, title: “Klok12H”}, theHour );
in your script, gives the error:

Invalid or unexpected token: Invalid or unexpected token

:thinking:

did you define this?

Yes, I copied the complete script. tried to run it. Or did I need to add something?

just tried to run it, no problem:
image

found the error… The pasted quotes : " where not the same. The compiler didn’t see the code between the quotes as strings. :see_no_evil:

Anyway, the code did run. Sadly I cannot find any trace of stored tags if I enumerate all the devices. I think these tags are only created at runtime. Makes more sense also, otherwise it would be a memory leak if these things never get destroyed.

So if anyone know a solution to store a variable, and retrieve that variable at a later stage with a different Homeyscript… I’m all ears. Thanks anyway JPE4619 for the support.

1 Like

You can also create the variable in BetterLogic and update it like:

let BLApp = await Homey.apps.getApp({id:“net.i-dev.betterlogic” });
BLApp.apiPut(‘Klok12h/’ + theHour);

Oh YES, that seems to work.
How do I retrieve a variable? is there some kind of BLApp.apiGet function?

I have a hard time retrieving the Beter Logic variable. In my search I stumbled on this example:

the code:
let allTokens = await Homey.flow.getTokens();
Should retrieve Beter Logic variable data… but when I try te code I get the following error in the console:

Script Error: Homey.flow.getTokens is not a function
Anyone who knows how to retrieve these better logic variables inside HomeyScript?