How to configure a MediaPlayer Item with MQTT (Play/Next/Previous)

Hi everyone,

I’m trying to integrate a BLE remote into openHAB via MQTT. The remote expects commands on the topic:

BLERemote/cmd

with payloads like:

  • PLAY

  • PAUSE

  • NEXT

  • PREVIOUS

I would like to expose this in openHAB as a MediaPlayer Item so that the UI shows the proper media controls (play, pause, next, previous).

Here’s what I tried so far:

Thing

Thing mqtt:topic:bleremote "BLE Remote" {
    Channels:
        Type string : control "Media Control" [
            commandTopic="BLERemote/cmd",
            commandTransformation="JS:bleremote.js"
        ]
}

Transformation (transform/bleremote.js)

(function(command) {
  switch(command) {
    case "PLAY": return "PLAY";
    case "PAUSE": return "PAUSE";
    case "NEXT": return "NEXT";
    case "PREVIOUS": return "PREVIOUS";
    default: return "";
  }
})(input)

Item

Player BLERemoteControl "BLE Remote" { channel="mqtt:topic:bleremote:control" }


The problem:

  • If I try to define the channel as Type player, I get Channel mqtt:topic:bleremote:control not supported!

  • Using Type string works, but I’m not sure if this is the correct or recommended approach for binding a Player Item to MQTT.

:red_question_mark: My questions:

  1. Is there a “best practice” way to map an MQTT topic to a Player Item?

  2. Should I continue using a string channel with a Player Item, or is there a better way?

  3. Has anyone built a similar MQTT → MediaPlayer setup and can share their config?

  4. No command is send out finally, where is my failure of definition?

Thanks a lot in advance!

Yes, string is perfectly fine

The commandTransformation="JS:bleremote.js" is incorrect though.

Here’s what everything should look like:

Thing mqtt:topic:bleremote "BLE Remote" (mqtt:broker:mosquitto) {
    Channels:
        Type string : control "Media Control" [
            commandTopic="BLERemote/cmd"
        ]
}
Player BLERemoteControl "BLE Remote" { channel="mqtt:topic:bleremote:control" [ profile="transform:JS", commandFromItemScript="|input" ] }

If you have the Thing definition inside your bridge then you don’t need to add the bridge reference as I did.

Note: The use of the profile should not have been necessary, but for some reason it didn’t work without it.

Need to investigate why this is so and fix it.

@jimtng hello Jim is this solved?

I haven’t looked into it, and I don’t know whether someone else had. I’ve got a few other things on my to-do list at the moment. If someone would jump in and take on this task, feel free!