# Set Logic variable by NAME?

**URL:** <https://community.homey.app/t/set-logic-variable-by-name/117888>\
**Category:** Questions & Help\
**Tags:** homeyscript, homey-pro\
**Created:** [October 11, 2024, 11:23am UTC](https://community.homey.app/t/set-logic-variable-by-name/117888 "2024-10-11T11:23:55Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [October 11, 2024, 4:44pm UTC](https://community.homey.app/t/set-logic-variable-by-name/117888/4 "2024-10-11T16:44:50Z")

</div>

@Pat_M If you mean per homeyscript, this should do it:

> [@Setting Logic Variable by HomeyScript](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/43):
>
> Here I have created two functions which you can add to your scripts.

```auto
//Name of Logic Variable
var name = "TVProgramma";

//Read Logic Var
var value = await ReadLogicVar(name);
console.log("The parameter '"+name+"' has a value '"+value+"'");

//Write Logic Var
var new_value = "NOS Journaal";
await WriteLogicVar(name,new_value);

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

  var varArr = await Homey.logic.getVariable({id:varID});

  return varArr.value;
}

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

  if (typeof(value) == varArr.type){
    await Homey.logic.updateVariable({id:varID,variable:{value:value}})
    //console.log("Value has been updated")
    return true;
  }
  else{
    console.log("New value has the wrong type! Expected type is '"+varArr.type+"', not '"+typeof(value)+"'. Value has NOT been updated!")
    return false;
  }
}

```

---

_[View the full topic](https://community.homey.app/t/set-logic-variable-by-name/117888)._
