# How to read and Set the logic variables with HomeyScript?

**URL:** <https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520>\
**Category:** Apps\
**Created:** [June 17, 2019, 2:04pm UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520 "2019-06-17T14:04:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcelT](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcelt/32/10042_2.png) [@MarcelT](https://community.homey.app/u/MarcelT)\
**Post date:** [June 17, 2019, 2:04pm UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520/1 "2019-06-17T14:04:56Z")

</div>

Hi,

I am writing a script to set my screens on a certain percentage. Therefore I need to set/read the new 2.0 logic variables from Homey Script.

Does anybody know how to do this?

Kind regards,

Marcel

---

<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 17, 2019, 3:51pm UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520/2 "2019-06-17T15:51:24Z")

</div>

From [this post](https://community.homey.app/t/homeyscript-app/854/25) onwards, there’s some discussion on how to set Logic variables.

---

<div class="post-metadata">

**Author:** ![MarcelT](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcelt/32/10042_2.png) [@MarcelT](https://community.homey.app/u/MarcelT)\
**Post date:** [June 17, 2019, 4:15pm UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520/3 "2019-06-17T16:15:15Z")

</div>

Thanks!

---

<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:27pm UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520/4 "2022-09-26T14:27:16Z")

</div>

Found this from google search and this is how I got variables to update:

```auto
const variables = await Homey.logic.getVariables()

console.log('variables', variables)
const id = "2df501f3-f1d7-4f40-bbba-6a8e29b51fee" // my variable id 
const dimLevel = await Homey.logic.getVariable({id: id})

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:** ![tfoldi](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tfoldi/32/78325_2.png) [@tfoldi](https://community.homey.app/u/tfoldi)\
**Post date:** [June 9, 2023, 9:18am UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520/5 "2023-06-09T09:18:45Z")

</div>

If you want to look up a variable by name instead of id, you can use lodash like:

```auto
const vars = await Homey.logic.getVariables(); 
const myVar = _.find(vars, (o) => o.name === "Variable name");

log(`Changed switch is ${myVar.value}`)

await Homey.logic.updateVariable({id: myVar.id, variable: {value: "foo"}}) 

```

---

<div class="post-metadata">

**Author:** ![dsdevries](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dsdevries/32/80220_2.png) [@dsdevries](https://community.homey.app/u/dsdevries)\
**Post date:** [June 7, 2024, 2:00pm UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520/7 "2024-06-07T14:00:22Z")

</div>

you really don’t need lodash to convert an object into an array so you can find a variable by a field property. you can just write:

```auto
const myVar = Object.values(await Homey.logic.getVariables()).find(({name}) => name === "Variable name");

```

---

<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 7, 2024, 2:18pm UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520/8 "2024-06-07T14:18:08Z")

</div>

But `lodash.find()` is faster 🤭

---

<div class="post-metadata">

**Author:** ![dsdevries](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dsdevries/32/80220_2.png) [@dsdevries](https://community.homey.app/u/dsdevries)\
**Post date:** [June 7, 2024, 2:35pm UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520/9 "2024-06-07T14:35:29Z")

</div>

I really don’t think so:

> **[Is Lodash That Good? Performance Comparison of JavaScript and Lodash |...](https://hackernoon.com/is-lodash-that-good-performance-comparison-of-javascript-and-lodash)**
>
> Lodash is a very popular JavaScript library and I have come across it in almost every project I have, but was it justified? is she that good? let's find out in

The native array and object methods are almost always faster than Lodash, plus they require less memory.

---

<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 7, 2024, 2:40pm UTC](https://community.homey.app/t/how-to-read-and-set-the-logic-variables-with-homeyscript/14520/10 "2024-06-07T14:40:20Z")

</div>

> [@dsdevries](#):
>
> I really don’t think so:

 

`benchmark` says otherwise:

```auto
✔ lodash.find 5,137,273.76 ops/sec ±2.87% (96 runs) fastest
✔ Array.find 3,212,320.51 ops/sec ±2.05% (96 runs) -37.47%

➔ Fastest is lodash.find

```

Duh never mind, `getVariables()` returns an object, I tested incorrectly 😅
