Here I have created two functions which you can add to your scripts.
//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;
}
}
ReadLogicVar and WriteLogicVar functions seems very useful to me as has been using JSONs as arguments earlier, but as many data needed also in trends decided to recode current approach with these funtions
Can someone advice how to make these functions “global”, so all of my codes/Scripts would be able to use them w/o repeating same in each one?
Got bit mixed results:
This is working fine (i.e. value changed as planned:
_.myWriteLogicVar(“Test_Var”,“abcd”)
where (as conbination from above messages):
//myWriteLogicVar
_.myWriteLogicVar = async function (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;
}
}
But having issue with reading-test:
let value = _.myReadLogicVar(“Test_Var”)
console.log(“value :”, value)
and (with similar mods):
//myReadLogicVar
_.myReadLogicVar = async function (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;
}
but getting only ('-marks added to show full message)):
Just is case some other newbees (like me) around, made one also for sensors (as need those values as well in several scripts), combined above advice and Homey example-script:
Sure. Note that you don’t need the type checking, because Homey will throw an ‘invalid_value_type’ error. Though you can catch it if you want to adjust your scripts behavior in that case.
_.myWriteLogicVar = async function ( name, value ) {
const thevar = await _.myReadLogicVar( name );
return Homey.logic.updateVariable( {
id: thevar.id,
variable: { value },
} );
};
Hi. Im trying to make a script. I want to get the time when a flow starts, for example, the sun rises → run script → the the script updates a variable named Tid,
If name always the same, then tag would do? I use them to pass info between scripts (e.g. if all OK / pass other data). Also avail. for flows (logic cards) when ones created…
On script-examples (at least in my HP2023) was easy to catch:
This script will create a few Tags for use in Flow.
It will remove them after 5 seconds.
// Create Tags
log(‘Creating My String…’);
await tag(‘My String’, ‘abcdef’);
log(‘Creating My Number…’);
await tag(‘My Number’, 123);
log(‘Creating My Boolean…’);
await tag(‘My Boolean’, true);
// Wait 15s
for (let i = 0; i < 5; i++) {
log(‘.’);
await wait(1000); // in milliseconds
}
// Delete Tags
log(‘Deleting My String…’);
await tag(‘My String’, null);
log(‘Deleting My Number…’);
await tag(‘My Number’, null);
log(‘Deleting My Boolean…’);
await tag(‘My Boolean’, null);