Openhab 2 & Kodi

I think the stop channel only works as button (in german as a “Taster”) that only sends a stop command to Kodi. openHAB has currently no button widget, so a switch is normally used for this purpose. But the switch does not really represent the current state.
But I think in this case it should be possible that the stop channel can give a feedback whether Kodi is playing something or not.

You can create an issue in the openHAB repo.

BTW: the latest version is 2.1.0-SNAPSHOT. You can donwload the latest openHAB version from here https://openhab.ci.cloudbees.com/job/openHAB-Distribution/ or only the latest Kodi add-on (maybe unstable because it is build based on pull requests) from:
https://openhab.jfrog.io/openhab/libs-pullrequest-local/org/openhab/binding/org.openhab.binding.kodi/2.1.0-SNAPSHOT/

Hey there,

I read the thread and want to show you my version of controling the lights when watching a movie with Kodi. Hence there is no really “Stop” state my rules based on a Player item for the control channel and a String item for the mediatype channel, Ignoring the stop channel completely. I want to dim the lights while watching movies, brighten them in breaks and at the end. But the light should not change while listening to music or looking pictures. Here you are:

rule "Kodi Player Command changed to PLAY"
when
    Item kodi2LivingRoomPlayerCommand changed to PLAY
then
    if( (kodi2LivingRoomPlayerType.state.toString == "channel") ||
        (kodi2LivingRoomPlayerType.state.toString == "episode") ||
        (kodi2LivingRoomPlayerType.state.toString == "movie") ||
        (kodi2LivingRoomPlayerType.state.toString == "video") ) {
            callScript("lights.darken")
    }
end
rule "Kodi Player Command changed to PAUSE"
when
    Item kodi2LivingRoomPlayerCommand changed to PAUSE
then
    callScript("lights.brighten")
end
rule "Kodi Player Mediatype changed"
when
    Item kodi2LivingRoomPlayerType changed
then
    var boolean darken = false

    if( (kodi2LivingRoomPlayerType.state.toString == "channel") ||
        (kodi2LivingRoomPlayerType.state.toString == "episode") ||
        (kodi2LivingRoomPlayerType.state.toString == "movie") ||
        (kodi2LivingRoomPlayerType.state.toString == "video") ) {
        darken = true
    }

    if( (kodi2LivingRoomPlayerCommand.state.toString == "PLAY") &&
        (darken == true) ) {
        callScript("lights.darken")
    } else {
        callScript("lights.brighten")
end

Why do I use these three rules?
The first and second rule together may be a solution for the requirement. But when you want to darken the light only for video or movie files you had to check the media type. In a lot of situations the media type is not correct when the first rule is executed. This can be traced back to the sequence in which the binding refreshes channels.
The third script is a solution as well. But when you want to brighten the lights in a break you need all three rule together. Another positive side effect of this combination is that the brightness of the light changes when you are listening to music and start a movie in the background.

Currently I am thinking about implementing a trigger-channel in the Kodi binding for handling this scenario a little easier.

2 Likes

Thanks for sharing your solutuion. Will try it in my OH2 setup.

Hi again

Tried your rule and just read your description again. If I’m correct, you don’t distinguish between PAUSE and STOP.
So I’m still missing the functionality of the old binding in OH1. In my scenario, I have to distinguish:
PLAY (movie or episode): 2 lamps switch off
PAUSE: one of the lamps dims to 50%
STOP: both lamps switch to 100% again.

So I really do need the stop channel again I’m afraid.

1 Like

I’m pretty sure this pull request will solve my problems:

But I asked to make sure.

Yes that is correct. I don’t do that. In case of STOP the media type is set to “”, in case of PAUSE it remains the same value as before. Maybe that helps.

//EDIT: I agree, the referenced pull request solves your problem.

Already tried to use the media type to distinguish between the two states, but that didn’t work cleanly either. I think the media type wasn’t always set to “” when the state already changed so you would have to work with sleeps and that always gets a bit messy.
Thanks for confirming, I think I’ll just wait for the pull request to be available then my problems should be solved :grin:

Hi, i’m into OH just 2-3 weeks but have been able to fully integrate my 3 Kodi system.

Everything works and I can control volume, switching lights based on play-mode and displaying OH messages on TV when a OH trigger been raised.

The only problem I have is when Kodi/XBMC is crashing or is getting powered-off… this is causing an error in OH when trying to send commands to the Kodi Instance.

How can I avoid such error and detect that a Kodi instance has died or become unavailable?

Whenever Kodi is starting or getting closed/crashing OH is receiving either of the below messages.

Message when Kodi starts:

 [me.event.ThingStatusInfoChangedEvent] - 'kodi:kodi:a972fc7c-ce36-9cfe-7f1a-81c63cfe850f' changed from OFFLINE to ONLINE

Message when Kodi is closed or even crashed:

 [me.event.ThingStatusInfoChangedEvent] - 'kodi:kodi:a972fc7c-ce36-9cfe-7f1a-81c63cfe850f' changed from ONLINE to OFFLINE

I tried to catch the event with according rule but it is not detecting

rule "Handle KODI1"
when
    Channel 'kodi:kodi:a972fc7c-ce36-9cfe-7f1a-81c63cfe850f' triggered
then
    logInfo("Kodi 1 alarm", "===============>>>>>>>> KODI is on/off <<<===================") 
end

Unfortunately the rule is not detecting the change in the Kodi instance! … is there any way to identify the Kodi is actively running ? …

Thank you for your help!

Hi Thomas,

I guess this is not a Channel, it should be a Thing. Something like this:

[...]
when
    Thing '...' changed from ONLINE to OFFLINE
then
[...]

But there is currently an issue in the ESH repo for getting the status of it:

https://github.com/eclipse/smarthome/issues/1654

So it is not possible as far as i know. I like to have such a feature, too.

Hi Christoph,

yes, I tired to catch the event with 'Thing" and ONLINE/OFFLINE status as well … I hope this feature will be realised soon.

Is there any other way to avoid error messages when sending commands to a Kodi instance which does not exist (either not yet started or maybe crashed)

I’m using

String sKod1_Msg "Kodi Message [%s] " {gGroup1,) {channel=“Kodi:Kodi:xxxxx.shownotification”)

sendCommand(sKodi1_Msg, “Movement at Back-Door !!”)

but the sendCommand or any other item which is linked to the Kodi instance is creating an error when the instance is not available!

Thanks for suggestions

Maybe yes. It depends on your setup. If the device running Kodi is offline too, you can try to use the Network binding. If the device is online and only the Kodi instance is down, I think it is not possible.

Could you send me an abstract of your log file, containing such an error? Maybe we can try to handle it and avoid those messages.

thanks for the help
Just a bit of background.

  • I’m running several Kodi system (Win & OpenElec)
  • I have Xiaomi sensors in my home (Motion, Temp, Lightswitches, Camera,etc)

Whenever I have a certain set of events or alarms triggered I want them to be shown on every active Kodi screen.

My Items are:

String Kodi_Message "Kodi Message" <television>
String sKodi1_Msg "Kodi1 [%s]" <television> (gBedroom,gIndoor,gKodi_Bedroom) {channel="kodi:kodi:a972fc7c-ce36-9cfe-7f1a-81c63cfe850f:shownotification"}
String sKodi2_Msg "Kodi2 [%s]" <television> (gLivingroom,gIndoor,gKodi_Livingroom) {channel="kodi:kodi:1e1af152-91c0-2425-e1f6-32f022a972c8:shownotification"}

My rules are:

rule "new kodi message"
when
     Item Kodi_Message received command 
then
logInfo("=========>> Mesage ========>> ", Kodi_Active.state.toString)
   if (Kodi_Active.state.toString=="1" ) sendCommand(sKodi1_Msg,Kodi_Message.state.toString) // send to Kodi-1
   if (Kodi_Active.state.toString=="2" ) sendCommand(sKodi2_Msg,Kodi_Message.state.toString) // send to Kodi-2
   if (Kodi_Active.state.toString=="3" ) { sendCommand(sKodi1_Msg,Kodi_Message.state.toString) // send to both
   sendCommand(sKodi2_Msg,Kodi_Message.state.toString) }
end

This is the error message when the Kodi-2 instance is not active (works without error when instance is up):

13:07:58.132 [INFO ] [marthome.event.ItemStateChangedEvent] - Kodi_Message changed from Dog Movement detected !! to Entrance Movement detected !!
13:07:58.132 [INFO ] [cript.=========>> Mesage ========>> ] - 3
13:07:58.132 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'sKodi1_Msg' received command Entrance Movement detected !!
13:07:58.147 [ERROR] [i.internal.protocol.KodiClientSocket] - Error during callMethod
java.lang.Exception: socket not initialized
        at org.openhab.binding.kodi.internal.protocol.KodiClientSocket.sendMessage(KodiClientSocket.java:197)[183:org.openhab.binding.kodi:2.0.0]
        at org.openhab.binding.kodi.internal.protocol.KodiClientSocket.callMethod(KodiClientSocket.java:222)[183:org.openhab.binding.kodi:2.0.0]
        at org.openhab.binding.kodi.internal.protocol.KodiConnection.showNotification(KodiConnection.java:490)[183:org.openhab.binding.kodi:2.0.0]
        at org.openhab.binding.kodi.handler.KodiHandler.handleCommand(KodiHandler.java:143)[183:org.openhab.binding.kodi:2.0.0]
        at org.eclipse.smarthome.core.thing.internal.ThingManager$4.call(ThingManager.java:372)[105:org.eclipse.smarthome.core.thing:0.9.0.b4]
        at org.eclipse.smarthome.core.thing.internal.ThingManager$4.call(ThingManager.java:1)[105:org.eclipse.smarthome.core.thing:0.9.0.b4]
        at org.eclipse.smarthome.core.common.SafeMethodCaller$CallableWrapper.call(SafeMethodCaller.java:179)[98:org.eclipse.smarthome.core:0.9.0.b4]
        at java.util.concurrent.FutureTask.run(Unknown Source)[:1.8.0_131]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)[:1.8.0_131]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)[:1.8.0_131]
        at java.lang.Thread.run(Unknown Source)[:1.8.0_131]
13:07:58.147 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'sKodi2_Msg' received command Entrance Movement detected !!
13:08:00.576 [INFO ] [marthome.event.ItemStateChangedEvent] - mDog changed from OFF to ON
13:08:00.576 [INFO ] [marthome.event.ItemStateChangedEvent] - mDog_last changed from 2017-05-16T12:32:27.353+1000 to 2017-05-16T13:08:00.576+1000
13:08:00.576 [INFO ] [g.eclipse.smarthome.model.script.Dog] - Movement detected !!
13:08:00.592 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'Kodi_Message' received command Dog Movement detected !!
13:08:00.592 [INFO ] [marthome.event.ItemStateChangedEvent] - Kodi_Message changed from Entrance Movement detected !! to Dog Movement detected !!
13:08:00.592 [INFO ] [cript.=========>> Mesage ========>> ] - 3
13:08:00.592 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'sKodi1_Msg' received command Dog Movement detected !!
13:08:00.592 [ERROR] [i.internal.protocol.KodiClientSocket] - Error during callMethod
java.lang.Exception: socket not initialized
        at org.openhab.binding.kodi.internal.protocol.KodiClientSocket.sendMessage(KodiClientSocket.java:197)[183:org.openhab.binding.kodi:2.0.0]
        at org.openhab.binding.kodi.internal.protocol.KodiClientSocket.callMethod(KodiClientSocket.java:222)[183:org.openhab.binding.kodi:2.0.0]
        at org.openhab.binding.kodi.internal.protocol.KodiConnection.showNotification(KodiConnection.java:490)[183:org.openhab.binding.kodi:2.0.0]
        at org.openhab.binding.kodi.handler.KodiHandler.handleCommand(KodiHandler.java:143)[183:org.openhab.binding.kodi:2.0.0]
        at org.eclipse.smarthome.core.thing.internal.ThingManager$4.call(ThingManager.java:372)[105:org.eclipse.smarthome.core.thing:0.9.0.b4]
        at org.eclipse.smarthome.core.thing.internal.ThingManager$4.call(ThingManager.java:1)[105:org.eclipse.smarthome.core.thing:0.9.0.b4]
        at org.eclipse.smarthome.core.common.SafeMethodCaller$CallableWrapper.call(SafeMethodCaller.java:179)[98:org.eclipse.smarthome.core:0.9.0.b4]
        at java.util.concurrent.FutureTask.run(Unknown Source)[:1.8.0_131]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)[:1.8.0_131]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)[:1.8.0_131]
        at java.lang.Thread.run(Unknown Source)[:1.8.0_131]
13:08:00.608 [INFO ] [marthome.event.ItemStateChangedEvent] - sKodi1_Msg changed from Entrance Movement detected !! to Dog Movement detected !!
13:08:00.608 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'sKodi2_Msg' received command Dog Movement detected !!
13:08:00.608 [INFO ] [marthome.event.ItemStateChangedEvent] - sKodi2_Msg changed from Entrance Movement detected !! to Dog Movement detected !!

How do you determine the state of the Kodi_Active item? This seems to be the interesting part. :slight_smile:

The error message seems familiar to me. I saw it in my logs, too. In the same situations you mentioned - after a Kodi crash and sometimes after a reboot. IMHO the connection of the socket could be checked, before sending a notification to the Kodi instance. Maybe we should open an issue in the openHAB 2-addons repo to avoid those message

Hi Christoph,

I played around with the network binding and it finally works!

  1. Configure the Kodi-Instance Thing to port:9090 (default is 0 but is not helpful)
  2. Change Refresh interval to 1500ms
  3. setup rules with “changed to ON” and “changed to OFF” for your Kodi-instance

with this method I can identify if the Kodi-Instance is running on Windows or not and do set my Kodi_Active accordingly!

thank you for your help

Hi Thomas,

sounds good. I am happy you find a solution. You’re welcome, ask again if anything matters.

I had another idea this night. You can access the ONLINE or OFFLINE Status of a thing via the REST API. Combined with the HTTP binding and a JavaScript Transformation it is possible to setup a Switch item for each Kodi instance and change it to ON/OFF if the Kodi status changes. I am going to test that way and post it here.

Hi everybody,

The monitoring of the Kodi-Instance (Online Status) as mentioned above works OK.

I have now tried to extract further Kodi properties as described here

and here

Is there any way to access these properties with the Openhab 2 binding?

Hi Thomas,

currently it is not possible with the Kodi binding. Since the Kodi uses a JSON-RPC API you can access them in the same way as described in my last post (HTTP binding combined with a JavaScript Transformation). I imagine that there is a tutorial or post in this forum describing this method with more details -
but I cannot find it at the moment.

Which properties do you miss at much? The only thing I am missing is the Screensaver.State.

Hey Thomas,

the feature is now available in the current snapshot:

https://community.openhab.org/t/using-hue-online-offline-events-in-rules/30178

sounds exactly what I was looking for … where is the best place to get the right snapshot ?

You can find more information on that in the Install Documentation.