# 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:** 2

<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, 11:56am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/21 "2019-07-01T11:56:34Z")

</div>

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

---

<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, 1:08pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/22 "2019-07-01T13:08:36Z")

</div>

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

> **[GitHub - athombv/com.athom.homeyscript: HomeyScript is a JavaScript-based...](https://github.com/athombv/com.athom.homeyscript)**
>
> HomeyScript is a JavaScript-based scripting language for Homey that empowers users to unleash their creativity. - GitHub - athombv/com.athom.homeyscript: HomeyScript is a JavaScript-based scripting...

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?

---

<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, 1:46pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/23 "2019-07-01T13:46:17Z")

</div>

> **[HomeyScript - share your scripts - main discussion topic - Page 2](https://forum.athom.com/discussion/3666/homeyscript-share-your-scripts-main-discussion-topic/p2)**
>
> HomeyScript can possibly change the need to have Apps installed for all kinds of functionality. Getting the scripts to work is not the hardest part.

---

<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, 2:01pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/24 "2019-07-01T14:01:48Z")

</div>

I’ve read the complete topic, but I can’t find any example that reads a Better Logic variable? This is my first Homeyscript (not my first programming challenge). I’m afraid I need a bit more guidance (example code) to understand how I can retrieve this information. Sadly the HomeyScript is so badly documented.

---

<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, 2:10pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/25 "2019-07-01T14:10:47Z")

</div>

the code:  
`let allTokens = await Homey.flow.getTokens();`  
Also get the same error:

> * * *
> 
> Script Error:  
> Homey.flow.getTokens is not a function  
> Maybe @Dijker can answer, probably this was working in Homey V1 firmware?

---

<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:45pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/27 "2019-07-01T19:45:39Z")

</div>

> [@JPe4619](#):
>
> the code:  
> `let allTokens = await Homey.flow.getTokens();`  
> Also get the same error:

After digging into the Homey developer API, Ive finally discovered the right syntax to get the tokens (which contain the Better Logic variables)!  
It should be:  
`let allTokens = await Homey.flowToken.getFlowTokens();`  
Apparently Athom changed the syntax. So if anyone stumbles on the same problem, this is the code to use.

---

<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 2, 2019, 12:10pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/28 "2019-07-02T12:10:36Z")

</div>

That’s great, thanks! 😃 I also found the HomeyScript variables with this:

> “title”: string"Klok12H"  
> “type”: string"number"  
> “value”: int12  
> “uriObj”: {5 items  
> “type”: string"app"  
> “id”: string"com.athom.homeyscript"  
> “name”: string"HomeyScript"

---

<div class="post-metadata">

**Author:** ![Techniman](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/techniman/32/8654_2.png) [@Techniman](https://community.homey.app/u/Techniman)\
**Post date:** [November 14, 2019, 10:53am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/29 "2019-11-14T10:53:41Z")

</div>

Was looking for the same and found the below works  
just in case anyone else is struggling 🙂

> // my script  
> let BLApp = await Homey.apps.getApp({id:“net.i-dev.betterlogic” });  
> //set Better Logic Variable  
> [//BLApp.apiPut](https://BLApp.apiPut)(“VariableName/value”);
> 
> //get Better Logic variable  
> let tmp = await BLApp.apiGet(“DayOfYear”) ;
> 
> console.log(tmp) ;  
> console.log(tmp.name) ;  
> console.log(tmp.type) ;  
> console.log(tmp.value) ;

Will show :

> { name: ‘DayOfYear’, type: ‘string’, value: ‘296’ }  
> DayOfYear  
> string  
> 296

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 6, 2020, 4:56pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/30 "2020-08-06T16:56:49Z")

</div>

Is this still current syntax? I can’t seem to update BL variables nor read them.

---

<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:** [August 6, 2020, 4:59pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/31 "2020-08-06T16:59:00Z")

</div>

show your code please.

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 6, 2020, 5:01pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/32 "2020-08-06T17:01:42Z")

</div>

```
let BLApp = await Homey.apps.getApp({ id: 'net.i-dev.betterlogic' });
await BLApp.apiPut(`fluxCT/1`);
const read = await BLApp.apiGet('fluxCT')
console.log('read', read)

```

results in

```
Script Error:
An unkown error has occured [unknown_error]
```

---

<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:** [August 6, 2020, 5:20pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/33 "2020-08-06T17:20:45Z")

</div>

> [@iotnerd](#):
>
> ```auto
> let BLApp = await Homey.apps.getApp({ id: 'net.i-dev.betterlogic' });
> await BLApp.apiPut(`fluxCT/1`);
> const read = await BLApp.apiGet('fluxCT')
> console.log('read', read)
> 
> ```

try this, (in this example it is a String variable)  
` await BLApp.apiPut('fluxCT/'+ '1');`

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 6, 2020, 5:27pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/34 "2020-08-06T17:27:41Z")

</div>

```
await BLApp.apiPut('fluxCT/2');

```

results in _unknown error_

```
await BLApp.apiPut('fluxCT');

```

results in _Not found: PUT /api/app/net.i-dev.betterlogic/fluxCT_

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 6, 2020, 5:31pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/35 "2020-08-06T17:31:03Z")

</div>

OK, my bad. Variable name created in BL app didn’t match name in HomeyScript. 🙄

---

<div class="post-metadata">

**Author:** ![Slof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/slof/32/1318_2.png) [@Slof](https://community.homey.app/u/Slof)\
**Post date:** [May 3, 2022, 3:11pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/36 "2022-05-03T15:11:14Z")

</div>

Is this allready possible to read or write a Logic from Homey Script?

I have some flow which I want to move to one script to make it easyer for me, but then I need the values

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [May 3, 2022, 3:40pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/37 "2022-05-03T15:40:45Z")

</div>

Yes, since v7.4.x-RC firmware. You can actually write to logics vars. Reading vars was possible already if I’m correct.

 ![Screenshot_20220503-174001_Chrome](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/c/e/ce0a8b88438bce72a3f00e9f66fc6255bfada968.jpeg)

Here’s some how-to

> [@DIY circadian rhythm based lighting](https://community.homey.app/t/diy-circadian-rhythm-based-lighting/33348/37):
>
> It should be something like this with Logics variables. (First create the variables, and then go [find the variable ID’s](https://community.homey.app/t/diy-circadian-rhythm-based-lighting/33348/36) ) // This is not needed anylonger, so comment out or remove //let BLApp = await Homey.apps.getApp({ id: 'net.i-dev.betterlogic' }); //BLApp.apiPut(`${VARIABLE_PREFIX}temperature/${temperature}`); await Homey.logic.updateVariable({id: '1a164078-63fc-470c-ac76-9da7f113445e', variable: { value: temperature } }) // The example ID should be the ID from var '${VARIABLE\_PREFIX}\_tem…

---

<div class="post-metadata">

**Author:** ![MagTer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/magter/32/107154_2.png) [@MagTer](https://community.homey.app/u/MagTer)\
**Post date:** [May 29, 2022, 7:32am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/38 "2022-05-29T07:32:44Z")

</div>

I just tried using Homey.logic.updateVariable and it updates the variable as expected except for that it fails silently if you try to provide a value of the wrong type, like ‘22’ instead of 22 to a number type variable.

Where run in to issues is when I am trying to create a function where the variable ID to be updated is dynamic.

This is what works:  
Homey.logic.updateVariable({id: ‘218558ac-ef03-4f11-b9ed-653df90fd1e1’, variable: { value: 22 } });

This is what does not work:  
Homey.logic.updateVariable({id: outputvariable.id, variable: { value: 22 } });

I am a beginner at JavaScript but I guess this should be possible somehow? 🙂

---

<div class="post-metadata">

**Author:** ![MagTer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/magter/32/107154_2.png) [@MagTer](https://community.homey.app/u/MagTer)\
**Post date:** [May 29, 2022, 9:15am UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/39 "2022-05-29T09:15:48Z")

</div>

Figured it out eventually. I needed to convert the id to a string by adding .toString()

---

<div class="post-metadata">

**Author:** ![Tommi\_Taskinen](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tommi_taskinen/32/61493_2.png) [@Tommi\_Taskinen](https://community.homey.app/u/Tommi_Taskinen)\
**Post date:** [September 26, 2022, 2:35pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/40 "2022-09-26T14:35:50Z")

</div>

this is how I set variables:

```auto
const variables = await Homey.logic.getVariables()
console.log('variables', variables)
const dimLevel = await Homey.logic.getVariable({id: "2df501f3-f1d7-4f40-bbba-6a8e29b51fee"})
const newLevel = dimLevel.value > 0.2 ? dimLevel.value - 0.2 : 0
await Homey.logic.updateVariable({id: dimLevel.id, variable: {value: newLevel}})

```

---

<div class="post-metadata">

**Author:** ![dooniem](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dooniem/32/58384_2.png) [@dooniem](https://community.homey.app/u/dooniem)\
**Post date:** [October 6, 2022, 6:13pm UTC](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/41 "2022-10-06T18:13:02Z")

</div>

I’m not good at javascript, but I bashed and bashed at the keyboard until I could use the name of the variable for the whole operation, not manually find the id first. So it’s not beautiful, but works:

```auto
//Set or get variable in Homey by name of variable

const varName = 'myVariable' //The name of your variable

let Logic = await Homey.logic.getVariables();
let idArr = Object.entries(Logic);
let filtered = idArr.filter(([key, value]) => value.name==varName);
let [[ ,{ id: varID}]] = filtered;

//Now varID contains the ID you're after
//Read/get value of variable
var varArr = await Homey.logic.getVariable({id: varID})
var varValue = varArr.value

//Set variable to newValue:
var newValue = 'some value'
await Homey.logic.updateVariable({id: varID, variable: {value: newValue}})

```

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

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