Setting Logic Variable by HomeyScript

Oh YES, that seems to work.
How do I retrieve a variable? is there some kind of BLApp.apiGet function?

I have a hard time retrieving the Beter Logic variable. In my search I stumbled on this example:

the code:
let allTokens = await Homey.flow.getTokens();
Should retrieve Beter Logic variable data… but when I try te code I get the following error in the console:

Script Error: Homey.flow.getTokens is not a function
Anyone who knows how to retrieve these better logic variables inside HomeyScript?

I’ve read the complete topic, but I can’t find any example that reads a Better Logic variable? This is my first Homeyscript (not my first programming challenge). I’m afraid I need a bit more guidance (example code) to understand how I can retrieve this information. Sadly the HomeyScript is so badly documented.

the code:
let allTokens = await Homey.flow.getTokens();
Also get the same error:


Script Error:
Homey.flow.getTokens is not a function
Maybe @Dijker can answer, probably this was working in Homey V1 firmware?

After digging into the Homey developer API, Ive finally discovered the right syntax to get the tokens (which contain the Better Logic variables)!
It should be:
let allTokens = await Homey.flowToken.getFlowTokens();
Apparently Athom changed the syntax. So if anyone stumbles on the same problem, this is the code to use.

3 Likes

That’s great, thanks! :smiley: I also found the HomeyScript variables with this:

“title”: string"Klok12H"
“type”: string"number"
“value”: int12
“uriObj”: {5 items
“type”: string"app"
“id”: string"com.athom.homeyscript"
“name”: string"HomeyScript"

Was looking for the same and found the below works
just in case anyone else is struggling :slight_smile:

// my script
let BLApp = await Homey.apps.getApp({id:“net.i-dev.betterlogic” });
//set Better Logic Variable
//BLApp.apiPut(“VariableName/value”);

//get Better Logic variable
let tmp = await BLApp.apiGet(“DayOfYear”) ;

console.log(tmp) ;
console.log(tmp.name) ;
console.log(tmp.type) ;
console.log(tmp.value) ;

Will show :

{ name: ‘DayOfYear’, type: ‘string’, value: ‘296’ }
DayOfYear
string
296

2 Likes

Is this still current syntax? I can’t seem to update BL variables nor read them.

show your code please.

let BLApp = await Homey.apps.getApp({ id: 'net.i-dev.betterlogic' });
await BLApp.apiPut(`fluxCT/1`);
const read = await BLApp.apiGet('fluxCT')
console.log('read', read)

results in

Script Error:
An unkown error has occured [unknown_error]

try this, (in this example it is a String variable)
await BLApp.apiPut('fluxCT/'+ '1');

await BLApp.apiPut('fluxCT/2');

results in unknown error

await BLApp.apiPut('fluxCT');

results in Not found: PUT /api/app/net.i-dev.betterlogic/fluxCT

OK, my bad. Variable name created in BL app didn’t match name in HomeyScript. :roll_eyes:

2 Likes

Is this allready possible to read or write a Logic from Homey Script?

I have some flow which I want to move to one script to make it easyer for me, but then I need the values

Yes, since v7.4.x-RC firmware. You can actually write to logics vars. Reading vars was possible already if I’m correct.

Here’s some how-to

I just tried using Homey.logic.updateVariable and it updates the variable as expected except for that it fails silently if you try to provide a value of the wrong type, like ‘22’ instead of 22 to a number type variable.

Where run in to issues is when I am trying to create a function where the variable ID to be updated is dynamic.

This is what works:
Homey.logic.updateVariable({id: ‘218558ac-ef03-4f11-b9ed-653df90fd1e1’, variable: { value: 22 } });

This is what does not work:
Homey.logic.updateVariable({id: outputvariable.id, variable: { value: 22 } });

I am a beginner at JavaScript but I guess this should be possible somehow? :slight_smile:

Figured it out eventually. I needed to convert the id to a string by adding .toString()

1 Like

this is how I set variables:

const variables = await Homey.logic.getVariables()
console.log('variables', variables)
const dimLevel = await Homey.logic.getVariable({id: "2df501f3-f1d7-4f40-bbba-6a8e29b51fee"})
const newLevel = dimLevel.value > 0.2 ? dimLevel.value - 0.2 : 0
await Homey.logic.updateVariable({id: dimLevel.id, variable: {value: newLevel}})
1 Like

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