# 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:** 1\
**Showing post:** 41

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

```

---

_[View the full topic](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819)._
