Setting Logic Variable by HomeyScript

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:

//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}})
4 Likes