MQTT: player channel state & outgoing timestamp

There’s a few questions about how to get the value of Player channel states/commands as a string value, but it seems an MQTT Thing doesn’t publish these. The appear in debug/API as a plain text commands such as PREVIOUS, PLAY, PAUSE, NEXT etc. I have a player control set up next to a String value in a MQTT follow channel.

I’m also seeing a need to sync up the localised events in OpenHAB with the broker, as other MQTT clients will receive inaccurate message times if they are retained. They will be delivered like a flood and not reflect the actual time of the event.

The MQTT binding doesn’t mention Player channels, or a timestamp value: https://www.openhab.org/addons/bindings/mqtt.generic/

My Outgoing Value Format set in the MQTT Thing is simplistic:

{"attribute": "player", "value": "%s", "timestamp": "%t"}

Does anyone know how to achieve what’s above? The documentation seems scant.

What does %t mean in a string format?

You can build you json string in a rule then send that to the MQTT string channel.

Here is an example for a aircon.

What version of the mqtt binding are you using?

Using the latest binding. The variable %t was just an arbitrary value to indicate a timestamp or datetime value which would be helpful to have in the binding if it’s not there already.

We can’t do all that on every OpenHAB instance just for a text string like we get with the others. Isn’t there an actual event sent from the channel?

You could subscribe to the broker to always add timestamps to mqtt messages.

You can skip using things and just use a MQTT action to sent message

Then it would look something like


rule "Player State to MQTT"
when
  Item Tv received command
then

  val timestamp = new DateTimeType()
  val value = '{\"attribute\": \"player\", \"value\": \"' + receivedCommand + '\", \"timestamp\": \"' + timestamp + '\"}'  
  
  val actions = getActions("mqtt","mqtt:broker:myMQTTBroker") // myMQTTBroker is the name of my broker yours may be different
  actions.publishMQTT("tv/player",value)    
  logInfo("publishMQTT", value )
end