# Setting Logic Variable by HomeyScript

**URL:** <https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819>\
**Category:** Questions & Help\
**Created:** [June 25, 2019, 3:11pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819 "2019-06-25T15:11:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![enrico\_cocchi](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/enrico_cocchi/32/12475_2.png) [@enrico\_cocchi](https://community.homey.app/u/enrico_cocchi)\
**Post date:** [June 25, 2019, 3:11pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/1 "2019-06-25T15:11:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [June 26, 2019, 1:33pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/2 "2019-06-26T13:33:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [June 26, 2019, 1:36pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/3 "2019-06-26T13:36:35Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![enrico\_cocchi](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/enrico_cocchi/32/12475_2.png) [@enrico\_cocchi](https://community.homey.app/u/enrico_cocchi)\
**Post date:** [June 26, 2019, 2:19pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/4 "2019-06-26T14:19:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [July 1, 2019, 7:58am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/5 "2019-07-01T07:58:52Z")

</div>

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](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex025/uploads/athom/original/2X/8/8dd5de16b5c868803fe1f3df5fc034bb1ac9adf3.png)  
I’ve managed to read the variables:  
 ![HomeyScript-GlobalVariable](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex025/uploads/athom/original/2X/f/f1645f0a32f08d85af80e0c22b01cb4ae4884934.png)  
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?

---

<div class="post-metadata">

**Author:** ![Caseda](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/caseda/32/138695_2.png) [@Caseda](https://community.homey.app/u/Caseda)\
**Post date:** [July 1, 2019, 8:13am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/6 "2019-07-01T08:13:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [July 1, 2019, 8:21am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/7 "2019-07-01T08:21:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [July 1, 2019, 9:43am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/8 "2019-07-01T09:43:23Z")

</div>

@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](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/3/35471d1f8622a57a3dbbea635ec51fb189f35ac8.png)

---

<div class="post-metadata">

**Author:** ![Caseda](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/caseda/32/138695_2.png) [@Caseda](https://community.homey.app/u/Caseda)\
**Post date:** [July 1, 2019, 9:55am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/9 "2019-07-01T09:55:07Z")

</div>

Not store, you can read, like you needed to do 😜

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [July 1, 2019, 10:00am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/10 "2019-07-01T10:00:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [July 1, 2019, 10:02am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/11 "2019-07-01T10:02:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [July 1, 2019, 10:10am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/12 "2019-07-01T10:10:23Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/5/538090a17857964812a92b2fa47be0af1177faa6.jpeg)

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [July 1, 2019, 10:21am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/13 "2019-07-01T10:21:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [July 1, 2019, 10:26am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/14 "2019-07-01T10:26:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [July 1, 2019, 10:36am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/15 "2019-07-01T10:36:16Z")

</div>

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

> Invalid or unexpected token: Invalid or unexpected token

🤔

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [July 1, 2019, 10:40am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/16 "2019-07-01T10:40:05Z")

</div>

> [@How I can change a global variable or “tag” inside HomeyScript?](https://community.homey.app/t/how-i-can-change-a-global-variable-or-tag-inside-homeyscript/15008/4):
>
> var theHour =

did you define this?

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [July 1, 2019, 10:42am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/17 "2019-07-01T10:42:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [July 1, 2019, 10:46am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/18 "2019-07-01T10:46:36Z")

</div>

just tried to run it, no problem:  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/2X/0/0a3f06064510f44ea514b14ab93d38de57e520cf.png)

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [July 1, 2019, 10:59am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/19 "2019-07-01T10:59:14Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [July 1, 2019, 11:03am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/20 "2019-07-01T11:03:34Z")

</div>

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

[Next page](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819.md?page=2)
