Homey Community Forum

Setting Logic Variable by HomeyScript

I Try to set value on a better logic variable follow other forum.
I find out the varible’s ID and on “Web API Playground” i wrote the follow instruction:

Homey.logic.updateVariable({id: “2de431c4-ddc1-49d8-b98b-536d9acd1d59”, variable: {“value” :48}})

Response
“root”:{5 items
“__athom_api_type”:string"HomeyAPI.ManagerLogic.Variable"
“id”:string"2de431c4-ddc1-49d8-b98b-536d9acd1d59"
“name”:string"Power_Surplus"
“type”:string"number"
“value”:int48}

The variable is set . …
Instead, Writing same instruction into Homeyscript the script is executed but variable do not change .

if i write the isctruction with await the script end with an error

await Homey.logic.updateVariable({id: “2de431c4-ddc1-49d8-b98b-536d9acd1d59”, variable: {“value” :48}})


Script Error:
You have no access to do this.

May someone help me …
Best regards

Enrico

Here is an example for the use of change a BetterLogic variable:
// my12HoursClock // BeterLogic
let sys = await Homey.system.getInfo();
var theTime = new Date(sys.date);
var theHour = theTime.getHours();
if (theHour > 12) { theHour = theHour - 12};
let BLApp = await Homey.apps.getApp({id:“net.i-dev.betterlogic” });
BLApp.apiPut(‘Klok12h/’ + theHour);
console.log(‘TheHour’,theHour);
return(true);

(I use this, in a flow, to let Homey speak the time every hour.)

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);