OH3 - Sonos - parse title and artist

If I play radio stations on my Sonos the artist and title information is not shown. The information however is available in the current track (embedded in a string):

Radio Swiss Jazz - Daniel Schläppi & Marc Copland - Song For My Father to Radio

I’ve create a small rule to parse this string and to populate album, artist and title:

import org.openhab.core.model.script.ScriptServiceUtil

val UpdateArtistAndTitle = [ String itemPrefix |
    val trackItem = ScriptServiceUtil.getItemRegistry?.getItem(itemPrefix + "_CurrentTrack")
    val artistItem = ScriptServiceUtil.getItemRegistry?.getItem(itemPrefix + "_MediaArtist")
    val titleItem = ScriptServiceUtil.getItemRegistry?.getItem(itemPrefix + "_MediaTitle")
    val albumItem = ScriptServiceUtil.getItemRegistry?.getItem(itemPrefix + "_CurrentAlbum")

    val track = trackItem.state.toString()

    if (!track.isEmpty()) {
        
        if (track.indexOf("-") > 0) {               
            val parsedTrakname = track.split("-")
            
            val artist = parsedTrakname.get(1).trim().replaceAll("\\s+$", "")
            var title = parsedTrakname.get(2).trim().replaceAll("\\s+$", "")        
            
            val parsedTitle = title.split("[(]")
            title = parsedTitle.get(0)
            albumItem.postUpdate(titleItem.state)            
            artistItem.postUpdate(artist)
            titleItem.postUpdate(title)            
        } 
    }
]

// SONOS Radio stations encode the artist and the title
// in the track. This rule is to parse the information
// and to populate artist and title with the parsed
// info.
rule "Parse track information change/update"
when
    Item SonosSYMFONISKBuro_CurrentTrack changed or
    Item SonosSYMFONISKKuche_CurrentTrack changed or
    Item SonosPortFernsehzimmer_CurrentTrack changed or
    Item SonosSYMFONISKTechnikraum_CurrentTrack changed or
    Item SonosSYMFONISKKeller_CurrentTrack changed or
    Item SonosSYMFONISKBad_CurrentTrack changed or
    Item SonosSYMFONISKGastezimmer_CurrentTrack changed or
    Item SonosSYMFONISKSchlafzimmer_CurrentTrack changed
then
    UpdateArtistAndTitle.apply(triggeringItemName.split("_").get(0))
end

Maybe that is useful to someone :slight_smile: