Homey Community Forum

Filtering of MQTT message

Hello,

I have installed a SIP2MQTT tool and now I get an MQTT message whenever someone calls my Asterisk PBX. The script also searches for the accompanying name in a vCard file.

The MQTT message looks like:

{"verb": "incoming", "caller":"Remco Hannink (0612345678)", "uri":"\"0612345678\" <sip:0612345678@192.168.1.256>"}

Now I want to filter just the name off the maller from this message and Send it to Google Home to make an announcement.
I am wonderen how I can achieve this with Homey flows.

Gr. Remco

You can use Homeyscript for that:

The mqttcaller script extracts the caller and puts it in a tag:

const payload = JSON.parse(args[0]);
setTagValue('caller', { type: 'string', title : 'caller' }, payload.caller);
return true;

(for some reason, the tag title isn’t show in the Action card, just its green placeholder)

@robertklep,

Thanks slot, I Will try it and let you know…

Gr. Remco

@robertklep,

Two problems, Problem one, I don’t see the returned TagValue from the script with the callerID in the list of tags (no empty placholder either).
And I was wondering if I could filter just the name from the entire callerID: “Remco Hannink (0612345678)”.

Gr. Remco

First problem is solved. I had to run the script once manual and then the caller tags shows in the list (with name BTW).

Second problem is still not solved.

If it’s always of that format, you can remove everything from the first parentheses:

setTagValue('caller', { type: 'string', title : 'caller' }, payload.caller.replace(/\s*\(.*/, ''));

@robert,

I like to send the name if it is known. I have the tag callername for that.
If caller is empty however I want to display the tag Number. I have already tried to Create the tag myself, regular expression for everything between parentheses is:

\(([^\)]+)\)

But how do I Create a string with that:

setTagValue('callername', { type: 'string', title : 'naam' }, payload.caller.replace(/\s*\(.*/, ''));
setTagValue('callernumber', { type: 'string', title : 'nummer' }, payload.caller ????????? 

Sorry for these “dumb” questions, was not Able to find it on google.

Gr. Remco

This should work:

payload.caller.replace(/.*\((.*?)\).*/, '$1')

@robertklep,

Thanks, its working now…

Gr. Remco

I have a similar use-case with a IKEA dimmer connected with Zigbee2MQTT on a Raspberry PI.
It outputs a payload like:
{“rate”:195,“linkquality”:36,“brightness”:252,“update_available”:true,“battery”:87,“action”:“rotate_stop”}

I can use the MQTT client and receive the payload with the topic within a flow:
zigbee2mqtt/IKEA SYMFONISK REMOTE (keuken)

How can i extract the brightness value, in this case “252”, and store it in a variable? Is there a way without Homeyscript, for instance make use of math.js in the app Better Logic to only extract a piece of a string. Or even better connect a way to receive a topic including a direct to link to a payload array in the MQTT client app.

Can you help me out?

It might be possible with Math.js and Better Logic. I doubt it’s possible with the MQTT Client app because parsing/processing JSON is too specific (JSON isn’t part of MQTT).

But I think Homeyscript is the easiest way, the script will be very similar to this script, just a different call to setTagValue:

setTagValue('brightness', { type: 'number', title : 'brightness' }, payload.brightness);

Hello Robert,

Thanks for your help. It worked with your homeyscript sollution.

Unfortunately my flow is automatically dissabled by Homey after some use of the volume remote because the flow is to much started if I dail in and out the round dailer knob on the remote. (use case is to change volume of a chromecast by a remote with Homey)

I will try out the Homey MQTT hub app to directly change the chromecast volume capability without a flow and see if this will work.

If anyone have a better sollution or some tips, you’re welcome. :slight_smile: