# Add a MQTT return to a var

**URL:** <https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635>\
**Category:** Questions & Help\
**Created:** [August 15, 2020, 11:06am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635 "2020-08-15T11:06:49Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 16, 2020, 12:10pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/21 "2020-08-16T12:10:18Z")

</div>

I think I found a solution by creating a small script. The only thing I miss in this script is how do I get the result in a var which I can use for a logic decision and should I create a true part because the result is underfunded.

if (args[0] == undefined) {  
console.log (‘Missing argument’);  
return false;  
}

let bl = await Homey.apps.getApp({ id: ‘net.i-dev.betterlogic’ } );  
if (!bl) {  
console.error(‘Better Logic not installed!’);  
return false;  
}

obj = JSON.parse(args[0]);

bl.apiPut(‘State’ + ‘:’ + obj.state)  
console.log('Updating variable: ’ + ‘State’ + ‘:’ + obj.state);

I get this as a return when I run the script for both topics.

Script returned:  
false  
Updating variable: State:1

* * *

Script returned:  
undefined  
Updating variable: State:0

* * *

Script returned:  
undefined

---

<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:** [August 16, 2020, 2:20pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/22 "2020-08-16T14:20:10Z")

</div>

The format of the line where you store the value to the BL variable, must be like:  
` let result=BLApp.apiPut("RadioDag/" + randomNumber);`

**"RadioDag"** is the Variable Name,  
**/** (backslash) is also very important, it must be added after the Variable name  
**randomNumber** is the value that is inserted into the BL Variable.

EDIT: simplified, it was unnecessary complicated.

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 16, 2020, 8:57pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/23 "2020-08-16T20:57:41Z")

</div>

I tried a lot of things but did not succeed to get the result in a var.

Looking to the script I am using, what is the name of the var I should make in Better Logic?  
Sorry but I do not have knowledge about scripting… Learning a bit… takes time. 😉

---

<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:** [August 16, 2020, 11:26pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/24 "2020-08-16T23:26:33Z")

</div>

You can use a name of your choice, as long as you use exactly the same name in the script routine, also the variable type must be the same (String or number or boolean)

As the last line of your script you can use; **return true;** so you can run this script in the AND column.

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 21, 2020, 7:27am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/25 "2020-08-21T07:27:43Z")

</div>

After a lot of trail & error finally it worked. 🙂👌🏻  
I modified the script as shown below:

if (args[0] == undefined) {  
console.log (‘Missing argument’);  
return false;  
}

let bl = await Homey.apps.getApp({ id: ‘net.i-dev.betterlogic’ } );  
if (!bl) {  
console.error(‘Better Logic not installed!’);  
return false;  
}

obj = JSON.parse(args[0]);

bl.apiPut(“ZoneStatus/” + obj.state);  
bl.apiPut(“ZoneDuration/” + obj.duration);

console.log('Updating variable: ’ + ‘State’ + ‘:’ + obj.state + ’ ’ + ‘Duration’ + ‘:’ + obj.duration);

There is one thing to solve. I think I have to create a 2nd script which reads the MQTT Topic. The 1st script was about : Message received via Topic.  
The received Topic is: opensprinkler/station/#  
‘#’ is a number between 0 & 7.

What will be a simple way to put this number in a var? That means the Topic wil be (example):  
opensprinkler/station/7  
var ZoneNumber = 7

---

<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:** [August 21, 2020, 7:53am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/26 "2020-08-21T07:53:05Z")

</div>

Doesn’t the MQTT Client app provide the topic in a token that you can pass as second argument (`args[1]`) to your script? Then you can extract the number from the topic name.

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 21, 2020, 8:07am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/27 "2020-08-21T08:07:11Z")

</div>

> [@RobvandeGoor](#):
>
> opensprinkler/station/7

This is wat I receive as Topic message: opensprinkler/station/7. I only need the last digit.  
This text does not look the same as the Topic message: {“state”:1}.

---

<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:** [August 21, 2020, 8:13am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/28 "2020-08-21T08:13:35Z")

</div>

> [@RobvandeGoor](#):
>
> opensprinkler/station/7. I only need the last digit.

`Number(topic.match(/\d+$/)[0])`

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 21, 2020, 8:30am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/29 "2020-08-21T08:30:05Z")

</div>

> [@robertklep](#):
>
> Number(topic.match(/\d+$/)[0])

Do you think 2 labels in 1 script will work? Or should I make 2 scripts? One per label?

 ![Script MQTT](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/b/2bfa046cfd761898f90d943b3f872988faa2dd3e.png)

---

<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:** [August 21, 2020, 11:26am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/30 "2020-08-21T11:26:18Z")

</div>

Yes, I think you can use two labels for one script. The first is accessible as `args[0]`, the second as `args[1]` (etc…).

I would put it all in one script 🙂

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 22, 2020, 10:41pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/31 "2020-08-22T22:41:13Z")

</div>

I have tested a lot but when the Topic information is like: opensprinkler/station/1, there is no way to get it into a var / script.

---

<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:** [August 23, 2020, 4:35am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/32 "2020-08-23T04:35:06Z")

</div>

Can you receive the topic name in your script? Obviously, instead of using `topic` in your script, use `args[1]` if you pass it as second argument:

```auto
const name = args[1].match(/\d+$/)[0]

```

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 23, 2020, 7:37am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/33 "2020-08-23T07:37:18Z")

</div>

I do get all information visible in the script.

When I add two arguments in the flow card, the script reads it as one argument.

[![](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/b/2bfa046cfd761898f90d943b3f872988faa2dd3e.png) ](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/b/2bfa046cfd761898f90d943b3f872988faa2dd3e.png)

Is there a way to split 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:** [August 23, 2020, 7:52am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/34 "2020-08-23T07:52:55Z")

</div>

Oh right, I thought that Homeyscript also had a card that would accept multiple arguments, but apparently it only has a card that accepts _one_ argument.

You can split them like this:

```auto
const [full, topic, message] = args[0].match(/(\S+)\s+(.*)/)

```

But you need to switch the tokens in the flow card around: `Naam Topic` first, `Bericht ontvangen via topic` second.

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 23, 2020, 9:37am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/35 "2020-08-23T09:37:21Z")

</div>

> [@robertklep](#):
>
> Number(topic.match(/\d+$/)[0])

I got the topic in a var. This problem has been solved. But I created a new one.  
the MQTT ‘message’ is the total of the message + topic. See used script below and result:

if (args[0] == undefined) {  
console.log (‘Missing argument’)  
return false;  
}

let bl = await Homey.apps.getApp({ id: ‘net.i-dev.betterlogic’ } );  
if (!bl) {  
console.error(‘Better Logic not installed!’)  
return false;  
}

const [message, topic] = args[0].match(/(\S+)\s+(.\*)/);

//obj = JSON.parse(args[0]);  
ZoneNumber = Number(topic.match(/\d+$/)[0]);

[//bl.apiPut](https://bl.apiPut)(‘State’ + ‘:’ + obj.state);  
[//bl.apiPut](https://bl.apiPut)(“ZoneStatus/” + obj.state);  
bl.apiPut(“ZoneNumber/” + ZoneNumber);  
[//bl.apiPut](https://bl.apiPut)(“ZoneDuration/” + ZoneNumber);

console.log(message);  
console.log('Zone Number: ’ + ZoneNumber);

Script returned:  
false  
opensprinkler/station/7 {“state”:0,“duration”:38}  
Zone Number: 7

Now I to find a solution to get the only the {“state”:0,“duration”:38} and pars is like: obj = JSON.parse(args[0]); or obj = JSON.parse(message);

---

<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:** [August 23, 2020, 9:38am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/36 "2020-08-23T09:38:16Z")

</div>

> [@RobvandeGoor](#):
>
> const [message, topic] = args[0].match(/(\S+)\s+(.\*)/);

Please look at the code I posted and notice the difference between what you’re posting here and what I posted 😛

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 23, 2020, 10:09am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/37 "2020-08-23T10:09:34Z")

</div>

> [@robertklep](#):
>
> `const [full, topic, message] = args[0].match(/(\S+)\s+(.*)/)`

Sorry you’r right. I forgot the ‘Full’ part. But finally it works as expected. 😅

if (args[0] == undefined) {  
console.log (‘Missing argument’)  
return false;  
}

let bl = await Homey.apps.getApp({ id: ‘net.i-dev.betterlogic’ } );  
if (!bl) {  
console.error(‘Better Logic not installed!’)  
return false;  
}

const [full, topic, message] = args[0].match(/(\S+)\s+(.\*)/);

ZoneNumber = Number(topic.match(/\d+$/)[0]);  
obj = JSON.parse(message);

[//bl.apiPut](https://bl.apiPut)(‘State’ + ‘:’ + obj.state);  
bl.apiPut(“ZoneStatus/” + obj.state);  
bl.apiPut(“ZoneDuration/” + obj.duration);  
bl.apiPut(“ZoneNumber/” + ZoneNumber);

console.log('Zone Number: ’ + ZoneNumber);  
console.log('Zone state: ’ + obj.state);  
console.log('Duration: ’ + obj.duration);

* * *

## Script returned: undefined Zone Number: 7 Zone state: 0 Duration: 27

---

<div class="post-metadata">

**Author:** ![Eelke](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/eelke/32/9188_2.png) [@Eelke](https://community.homey.app/u/Eelke)\
**Post date:** [August 23, 2020, 10:23am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/38 "2020-08-23T10:23:33Z")

</div>

Homey script is not necessary to get a value out od a json received from the MQTT client. With only the Better Logic app this is possible.

Use the ‘JSONPath BetterLogic’ card for this. See screenshots:

 ![Screenshot_20200823-121455_Homey](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/9/7/97bfefc4c382b0e505a2d9f4dff13672285dd72c.jpeg)

 ![Screenshot_20200823-122312_Homey](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/6/2663ba9f572608bf340170c4fc8372f5ad8a79d1.jpeg)

---

<div class="post-metadata">

**Author:** ![RobvandeGoor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robvandegoor/32/20328_2.png) [@RobvandeGoor](https://community.homey.app/u/RobvandeGoor)\
**Post date:** [August 23, 2020, 10:35am UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/39 "2020-08-23T10:35:55Z")

</div>

Ok, thanks for the information. I will test this. And what about the MQTT Topic? This is not a JSON.

---

<div class="post-metadata">

**Author:** ![Eelke](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/eelke/32/9188_2.png) [@Eelke](https://community.homey.app/u/Eelke)\
**Post date:** [August 23, 2020, 12:37pm UTC](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635/40 "2020-08-23T12:37:37Z")

</div>

I see I mentioned the BetterLogic app, but is is the ‘HTTP Request’ you have to use…

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

[Previous page](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635.md?page=1)

[Next page](https://community.homey.app/t/add-a-mqtt-return-to-a-var/33635.md?page=3)
