Add a MQTT return to a var

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

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.

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. :wink:

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.

After a lot of trail & error finally it worked. :slight_smile::ok_hand:t2:
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

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.

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}.

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

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 :slight_smile:

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.

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:

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

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.

Is there a way to split this?

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:

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.

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(‘State’ + ‘:’ + obj.state);
//bl.apiPut(“ZoneStatus/” + obj.state);
bl.apiPut(“ZoneNumber/” + ZoneNumber);
//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);

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

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

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(‘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

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:

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

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

JSONPath