# 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:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pat\_M](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pat_m/32/56295_2.png) [@Pat\_M](https://community.homey.app/u/Pat_M)\
**Post date:** [October 11, 2024, 11:23am UTC](https://community.homey.app/t/set-logic-variable-by-name/117888/1 "2024-10-11T11:23:55Z")

</div>

Hi,

Is there a way to update/set a Logic variable by NAME and not by id please?

Many thanks!

---

<div class="post-metadata">

**Author:** ![Rmb](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rmb/32/79135_2.png) [@Rmb](https://community.homey.app/u/Rmb)\
**Post date:** [October 11, 2024, 2:31pm UTC](https://community.homey.app/t/set-logic-variable-by-name/117888/2 "2024-10-11T14:31:46Z")

</div>

I wouldn’t even know how to set/update a variable per ID, so please show your code/flow to we can have a better understanding of your problem.

---

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

</div>

Not directly, you’ll need to get the ID by searching the name between all variables.  
(Get all variables, search name, see what ID belongs to it, update variable however you want).

The update function itself only works with IDs.

---

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

```

---

<div class="post-metadata">

**Author:** ![Pat\_M](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pat_m/32/56295_2.png) [@Pat\_M](https://community.homey.app/u/Pat_M)\
**Post date:** [October 12, 2024, 2:10pm UTC](https://community.homey.app/t/set-logic-variable-by-name/117888/5 "2024-10-12T14:10:34Z")

</div>

Hi again, thanks for all input.

Here is my current script:

```auto
let testDay = new Date();
let dateNum = testDay.getDate();
let monthTxtShort = testDay.toLocaleString('fefault', { month: 'short'})

let TodaysDate = Object.values(await Homey.logic.getVariables()).find(({name}) => name === "TodaysDate");
let TodaysDateId = TodaysDate.id
let MonthTextShort = Object.values(await Homey.logic.getVariables()).find(({name}) => name === "MonthTextShort");
let MonthTextShortId = MonthTextShort.id

Homey.logic.updateVariable({id: TodaysDateId, variable: {'value' : dateNum}} )
Homey.logic.updateVariable({id: MonthTextShortId, variable: {'value' : monthTxtShort}} )

```

I just find it awkard to first have to get the ID of the variable. Is it because (which seems fair) you can have multiple variables with the same name and ID is the only key?

Thanks!

---

<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 12, 2024, 2:36pm UTC](https://community.homey.app/t/set-logic-variable-by-name/117888/6 "2024-10-12T14:36:43Z")

</div>

> [@Pat\_M](#):
>
> you can have multiple variables with the same name and ID is the only key

Yes. This is also true for flows and devices. The name does not have to be unique, the ID always is.

---

<div class="post-metadata">

**Author:** ![Pat\_M](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/pat_m/32/56295_2.png) [@Pat\_M](https://community.homey.app/u/Pat_M)\
**Post date:** [October 12, 2024, 2:41pm UTC](https://community.homey.app/t/set-logic-variable-by-name/117888/7 "2024-10-12T14:41:30Z")

</div>

Thank you Mr. Kawa, once again good advice! 😀
