[SOLVED] Need help with REGEX transformations in the sitemap

I need help with the REGEX syntax when it is used in a sitemap. But first some background:

I’m using OpenHabian OH2.4 that is controlling a Volumio internet radio via MQTT. A useful status messages from Volumio gives extensive information on what is playing, which is sent to OH2 in a string item named VolumioStatInfo.

It’s a large string that has several fields. I’d like to extract the title and artist fields from the string in the sitemap rather than with rules. The REGEX transformation add-on binding has been installed.

Here is an example of the contents of the string item:
{"status":"play","position":3,"title":"Wham! - Careless Whisper","artist":"MIX 96","album":null,"albumart":"/albumart?cacheid=265&web=MIX%2096//extralarge","uri":"http://17963.live.streamtheworld.com:80/KYMXFM_SC?DIST=TuneIn&tdtok=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6ImZTeXA4In0.eyJpc3MiOiJ0aXNydiIsInN1YiI6IjIxMDY0IiwiaWF0IjoxNTQyMzQ0NzA2LCJ0ZC1yZWciOmZhbHNlfQ.rQ-4bJC9zzv1oBaIsxnq_HeoDd5A5ZbFqgd-0TT5tnU&TGT=TuneIn","trackType":"webradio","seek":4520588,"duration":0,"samplerate":"","bitdepth":"","channels":2,"random":true,"repeat":true,"repeatSingle":false,"consume":true,"volume":18,"mute":false,"disableVolumeControl":false,"stream":true,"updatedb":false,"volatile":false,"service":"webradio"}

I’d like to use REGEX to extract the title field.In the item string example above it is
"title":"Wham! - Careless Whisper"

The problem:

I tested my REGEX pattern using an online developer tool. It passes the syntax test and provide the correct result, per the screen shot below:

In my sitemap I have this Text item:
Text item=VolumioStatInfo label="Title [REGEX(\"title\": \"(.*?)\"(.*)):%s]"

But it produces an error in the log and the sitemap shows NULL for the value.
Here’s the log error:
2018-11-16 15:43:46.368 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - transformation of type REGEX did not return a valid result

I’ve run out of ideas and need some help with correcting the regex sitemap code.

  • Thomas

Your string is a JSON string
There is a transformation service called JSONPATH that you need to install in the PaperUI
See: jsonpath.com

Text item=VolumioStatInfo label="Title [JSONPATH($.title):%s]"
1 Like

That was insanely easy. Thank you!

Now that it is working I now see that some of the music titles are a bit too long. Can you advise how I would add the substring (or equiv) JASON function to the “Title [JSONPATH($.title):%s]” to trim its length?

  • Thomas

I don’t think there is a substring function in JSONPATH
You will need another item and a rule to achieve what you want

is it possible to “cascade” (terminology?) 2 transformation services?
meaning: do the jsonpath thingy and process the result in a regex to trim the result of the jsonpath in 1 shot?

No, I really want this functionality too. I think the work you are looking for is “nest”.
It is not possible to nest transformation services at present. It may be possible to do so in the future.

2 Likes

What you can do instead is use a JAVASCRIPT transform.

You will need to write a bit of javascript code that do the transformation

** Install the JAVASCRIPT transformation**

Create a file called title.js in the transform folder with the following contents:

(function(jsonString) {
var data = JSON.parse(jsonString);
var title = data.title;
var maxLength = 25; // Set-up here what max length you want your string to be
if (title.length > maxLength) {
    title = title.substring(0, maxLength +1);
}
return title;
})(input)

And your sitemap element becomes:

Text item=VolumioStatInfo label="Title [JS(title.js):%s]"
1 Like

@vzorglub, thanks for creating the JAVASCRIPT example. Might take a day or two before I can get to it, but I will certainly try it out.

  • Thomas

@vzorglub, you did it again! The substring Java transform works great. Thanks again for your brilliant suggestions.

  • Thomas

Please mark the thread as solved.
Thanks