Start Harmony Hub Activity "automagically"

Dear Community!

I want to write a little rule to start a Harmony Activity if Kodi starts playing.
However, I want to do this dinamically, so when a music started, it should start “Listen to Music” Activity, etc…

Here is what I did so far.

rule "Start Playing"
	Item KodiMediacenter_Stop received update OFF
	if(CurrentActivityHub.state.toString == "PowerOff") {
		switch(Kodi_mediatype.state.toString) {
			case "song" :
				CurrentActivityHub.sendCommand("Listen to Music")
			case "episode" :
				CurrentActivityHub.sendCommand("Watch a Movie")
			case "movie" :
				CurrentActivityHub.sendCommand("Watch a Movie")
			case "radio" :
				CurrentActivityHub.sendCommand("Listen to Music")
			case "video" :
				CurrentActivityHub.sendCommand("Watch a Movie")
			case "unknown" : {
				logInfo("radio.rules", "Switch case: unknown")
				CurrentActivityHub.sendCommand("Listen to Music")
			default : {
				logInfo("radio.rules", "Switch case: Default")
				CurrentActivityHub.sendCommand("Watch a Movie")

However, it seems that the switch case only evaluates to default. I think I’m missing something there.
Also I need a little Thread::sleep at the beginning because Kodi needs a little time to start playing something and read and send the details of what’s playing to openHab. That’s why it start’s with sleep, but it messes up somehow.

Also, I need further checking at case “unknown” and default case, because sometimes Kodi couldn’t get mediatype information (like when I’m playing through Airplay).

So my second question is how do you check a String type in openHab if it is empty?
For example(?):

if(KodiMediacenter_Album == null)

Am I correct? Can someone help me?


To check for an empty string:

if(KodiMediacenter_Album == "")

If the variable is not initialized then do the == null check

I don’t know what is causing your problem, but I would suggest du output the state of Kodi_mediatype, so you know what you have.

logInfo("radio.rules", "Kodi_mediatype: " + Kodi_mediatype.state)

And I am not sure if you need to convert the state to string.

I would also consider to trigger on Item Kodi_mediatype change instead of Item KodiMediacenter_Stop received update OFF

If UI’s outputs “-” it means that String is uninitialized?

Thanks, I’ll try these!

However if I trigger for mediatype changed, will it trigger if the mediatype didn’t changed? I don’t know how this works in Kodi, maybe sometimes the mediatype stays at “null”?

There is a difference between Item changed ans Item updated.

While change requires the value to actually change, update can update the state to the same value it already has.
I don’t think the state will be “null”, if kodi is playing anything.
Have some faith. :wink:

Thanks, I’ll try it!

It seems that it works now… however I haven’t changed anything significantly.
Kodi mostly reports mediatype as “unknown” if the media is not from it’s library.