# Better logic with homeyscript

**URL:** <https://community.homey.app/t/better-logic-with-homeyscript/13420>\
**Category:** Questions & Help\
**Created:** [May 20, 2019, 7:03pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420 "2019-05-20T19:03:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![adem\_p](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adem_p/32/9485_2.png) [@adem\_p](https://community.homey.app/u/adem_p)\
**Post date:** [May 20, 2019, 7:03pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/1 "2019-05-20T19:03:03Z")

</div>

hi  
can someone post a homeyscript with getting/ setting better logic variables please.

Try to set a value via homescript, no error, but variabe is not set:  
`setTagValue('test',{type: 'number',title:'title'}, 10);`

Next set the variable via app-\>betterlogic  
Try to update variable in a flow. Error: Parameter is broken.

Any idea or example how to set / get variables with better logic?  
Do I have always to set inital Variable?

---

<div class="post-metadata">

**Author:** ![Ludwig\_Hallgren](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ludwig_hallgren/32/10694_2.png) [@Ludwig\_Hallgren](https://community.homey.app/u/Ludwig_Hallgren)\
**Post date:** [May 20, 2019, 9:08pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/2 "2019-05-20T21:08:03Z")

</div>

I’m not so into better logic yet since built in logic works great for me so far. Homeys own API works nice for crud operations in logic variables.

> [@\[APP\]\[Pro\] HomeyScript](https://community.homey.app/t/homeyscript-app/854/28):
>
> let devices = await Homey.devices.getDevices(); \_.forEach(devices, device =\> { console.log(device.name, ’ ',device.id); });

---

<div class="post-metadata">

**Author:** ![adem\_p](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adem_p/32/9485_2.png) [@adem\_p](https://community.homey.app/u/adem_p)\
**Post date:** [May 20, 2019, 9:16pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/3 "2019-05-20T21:16:34Z")

</div>

hey @Ludwig_Hallgren i would use homey logic also, I just need to be able to set variables from inside homescript not via a flow.  
is this somehow possible?

for me as a reminder: [https://forum.athom.com/discussion/4541/ok-homey-whats-the-current-day-number](https://forum.athom.com/discussion/4541/ok-homey-whats-the-current-day-number)  
to test again

---

<div class="post-metadata">

**Author:** ![Ludwig\_Hallgren](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ludwig_hallgren/32/10694_2.png) [@Ludwig\_Hallgren](https://community.homey.app/u/Ludwig_Hallgren)\
**Post date:** [May 21, 2019, 3:46am UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/4 "2019-05-21T03:46:59Z")

</div>

@adem_p yes that is very easily done. Look at the thread I linked at the last 4-5 comments.

---

<div class="post-metadata">

**Author:** ![Dijker](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dijker](https://community.homey.app/u/Dijker)\
**Post date:** [May 21, 2019, 5:04am UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/5 "2019-05-21T05:04:27Z")

</div>

> [@adem\_p](#):
>
> can someone post a homeyscript with getting/ setting better logic variables please.

For what I know it is not possible to change values of tags/variables of other apps.  
That could/would give inconsistent results as not all apps are prepared for that.  
Since the last update it is possible for set the Homey’s global variables.  
Beside that HomeyScript has its own variables.

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [May 21, 2019, 4:14pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/6 "2019-05-21T16:14:10Z")

</div>

> [@adem\_p](#):
>
> can someone post a homeyscript with getting/ setting better logic variables please

```
let sys = await Homey.system.getInfo();
var theTime = new Date(sys.date);
var theHour = theTime.getHours();
if (theHour > 12) { theHour = theHour - 12};
let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" });
BLApp.apiPut('Klok12h/' + theHour);

```

---

<div class="post-metadata">

**Author:** ![adem\_p](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/adem_p/32/9485_2.png) [@adem\_p](https://community.homey.app/u/adem_p)\
**Post date:** [May 21, 2019, 9:07pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/7 "2019-05-21T21:07:52Z")

</div>

I tried that also, but had some issues with homeyscript.  
a) some of the scripts disappear/deleted. after adding a new test script.  
b) parameter wrong message disappeared after restarting homey.

but nevermind thank you @JPe4619 and @Dijker for your example and clearance

---

<div class="post-metadata">

**Author:** ![mazpo](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/mazpo/32/3680_2.png) [@mazpo](https://community.homey.app/u/mazpo)\
**Post date:** [June 17, 2020, 12:18pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/8 "2020-06-17T12:18:01Z")

</div>

hi, thanx for your idea/example.

i am very close to finish. but the last step does not work. i read a better logic variable, do slicing an want to write it back, but the variable does not change.

do you have an idea, here is my code:

// my script

let BLApp = await Homey.apps.getApp({id:‘net.i-dev.betterlogic’ });

//set Better Logic Variable

[//BLApp.apiPut](https://BLApp.apiPut)(“VariableName/value”);

//get Better Logic variable

let tmp = await BLApp.apiGet(‘CameraLink’);

var link = tmp.value;

var laenge = link.length;

var res = link.slice(0,laenge-17);

//var link = await BLApp.apiGet(‘CameraLink’);

[//console.log](https://console.log)(res) ;

//var res = link.slice(0,link.Length()-1);

[//console.log](https://console.log)(res) ;

//set Better Logic Variable

[//BLApp.apiPut](https://BLApp.apiPut)(“CameraLink/”+res);

BLApp.apiPut(`CameraLink/${res}`);

//`Hi, ${user.name}!`

[//BLApp.apiPut](https://BLApp.apiPut)(‘CameraLink/NEU’);

[//console.log](https://console.log)(tmp) ;

[//console.log](https://console.log)(link.name) ;

[//console.log](https://console.log)(link.type) ;

[//console.log](https://console.log)(link.value) ;

[//console.log](https://console.log)(`CameraLink/${res}`)

return(true);

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [June 17, 2020, 2:24pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/9 "2020-06-17T14:24:55Z")

</div>

I’ve had some struggling also setting / reading better logic variables. Athom did some syntax changes. Read this topic for solutions:

> [@Setting Logic Variable by HomeyScript](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/27):
>
> 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.

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [June 17, 2020, 4:40pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/10 "2020-06-17T16:40:30Z")

</div>

> [@mazpo](#):
>
> [//BLApp.apiPut](https://BLApp.apiPut)(“CameraLink/”+res);
> 
> BLApp.apiPut( `CameraLink/${res}` );

You should use the first of this two lines

Sorry I dont have BetterLogic on my Homey, so can not test it, if it is not working, please indicate, what is working and what is not.

Please be aware of the “quote” signs, they sometimes are misformed .

---

<div class="post-metadata">

**Author:** ![mazpo](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/mazpo/32/3680_2.png) [@mazpo](https://community.homey.app/u/mazpo)\
**Post date:** [June 17, 2020, 7:19pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/11 "2020-06-17T19:19:34Z")

</div>

hi jpe4619

thanks for your reply. the var res gets the correct string without the last 16 character (&export=download), homeyscript prints it running console.log(res) ;

but still the betterlogic variable does not get updated by  
BLApp.apiPut(“CameraLink/”+res);

here is my flow to test, i get the same link with the &export=download twice.  
when i dont use the variable res, e. g  
BLApp.apiPut(“CameraLink/varvalueexmaple”);  
then the BL var is updated.

[https://homey.app/f/7SSI3b](https://homey.app/f/7SSI3b)

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [June 17, 2020, 8:30pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/12 "2020-06-17T20:30:28Z")

</div>

Found another peace of code, maybe you can follow this example?

let BLApp = await Homey.apps.getApp({id:“net.i-dev.betterlogic” });  
let result = await BLApp.apiPut("/" + BLVarName + “/” + Math.round(dayOfYear) );  
return true;

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [June 18, 2020, 5:54am UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/13 "2020-06-18T05:54:02Z")

</div>

FWIW, you can format pieces of code using the `</>` button (or using ` or ``` in the text). Makes it much easier to read.

---

<div class="post-metadata">

**Author:** ![mazpo](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/mazpo/32/3680_2.png) [@mazpo](https://community.homey.app/u/mazpo)\
**Post date:** [June 18, 2020, 6:10am UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/14 "2020-06-18T06:10:02Z")

</div>

> [@JPe4619](#):
>
> let result = await BLApp.apiPut(“/” + BLVarName + “/” + Math.round(dayOfYear) );

thanks for your idea and your awesome support.

unfortunately the script still does not change the better logic variable. maybe i missed something in my code below?

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/3/e/3e60002f9df9a9e7699c14ebe66af62674d9c89d.png)

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [June 18, 2020, 8:51am UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/15 "2020-06-18T08:51:59Z")

</div>

> [@mazpo](#):
>
> k/varvalueexmaple

Have installed BL and tested with both lines:

> BLApp.apiPut(“CameraLink/”+res);  
> let result=BLApp.apiPut(“CameraLink/”+res);

Both are working well in next flow"

[https://homey.app/f/xOhD6b](https://homey.app/f/xOhD6b)

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/3/8/38cea8d144bec451bff38b9f593547d9e006feac.png)

---

<div class="post-metadata">

**Author:** ![mazpo](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/mazpo/32/3680_2.png) [@mazpo](https://community.homey.app/u/mazpo)\
**Post date:** [June 18, 2020, 10:20am UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/16 "2020-06-18T10:20:09Z")

</div>

hi again,

could you please post your complete code? your last screenshot shows the same variable value twice, does this mean it works?

i tested your 2 lines for putting the new value to BL variable, but still no success.

with my code i would expect that the link will be reduced each script run by 16 characters. but it does not work because setting the new values does not change the variable in Better Logic.

Never give up:D

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [June 18, 2020, 12:46pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/17 "2020-06-18T12:46:59Z")

</div>

> [@mazpo](#):
>
> does this mean it works?

Yes, it works perfectly, as mentioned above, tested with both lines (one at a time)  
\> `// my script`  
` > let BLApp = await Homey.apps.getApp({id:"net.i-dev.betterlogic" });`  
` > let tmp = await BLApp.apiGet("CameraLink");`  
` > var link = tmp.value;`  
` > var laenge = link.length;`  
` > var res = link.slice(0,laenge-17);`  
`> //BLApp.apiPut("CameraLink/"+res);`  
`> let result=BLApp.apiPut("CameraLink/"+res);`  
`> return(true);`

Your BL-variable can be changed from IFTTT side and from your HomeyScript, are you sure these two don’t interfere? Maybe you should use two different variables.

> [@mazpo](#):
>
> Never give up:D

Thats the right spirit 👍

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [June 18, 2020, 12:55pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/18 "2020-06-18T12:55:26Z")

</div>

> [@robertklep](#):
>
> you can format pieces of code

Thanks Robert, as (almost) always you are right,  
but now an offtopic question to the man who knows it all:  
do you have an explanation for the “misforming” of the quote marks (" ") when they are entered in this forum? and if so, is there a way to avoid this?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [June 18, 2020, 12:59pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/19 "2020-06-18T12:59:29Z")

</div>

> [@JPe4619](#):
>
> do you have an explanation for the “misforming” of the quote marks (" ") when they are entered in this forum? and if so, is there a way to avoid this?

The forum editor converts normal quotes to “smart” quotes. It doesn’t look like you can avoid this (unless `"you're using the preformatted text option"`). Very annoying 😡

---

<div class="post-metadata">

**Author:** ![mazpo](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/mazpo/32/3680_2.png) [@mazpo](https://community.homey.app/u/mazpo)\
**Post date:** [June 18, 2020, 1:49pm UTC](https://community.homey.app/t/better-logic-with-homeyscript/13420/20 "2020-06-18T13:49:27Z")

</div>

thanks again, I am getting closer and closer.

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/4/f/4fc0a12b1b9cc288fda885b52c3b7ac6d6885713.png)

the following script with code line 4 active works correct, but when i disable codeline 4 and use the flow THEN Card with SET BL Variable CameraLink = tag1 (this is a lokal tag and it holds the link from IFTTT) the script does not perform the slicing.

very strange

[Next page](https://community.homey.app/t/better-logic-with-homeyscript/13420.md?page=2)
