Openhab 2 & Kodi

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.

The Thing status is detected but it is not working all the time :frowning:

In the stable OH version I was able yo follow the Online/Offline status in the Thing section and it worked very responsive … doing the same now in the latest Snapshot I have delays in the Online/Offline status in the Thing section.

Its a step in the right direction but not working reliable :wink:

Hey Thomas,

did you update to OH2.1 already? Any changes?

Hi Christoph,

I’m running the latest snapshot 2.2 on a RPI3 and the KODI binding got even more unreliable and is now OFFlLINE most of the time. I’m using a Windoes Kodi 17.3 and a LibraElec 17.3 and both systems show the same behaviour.

I was in doubt of my network but I have tried WIFI and Ethernet connectivity on both boxes.

Additionally I have now installed MQTT in parallel on both boxes. I’m getting the correct status update via MQTT and as well I’m able to send notifications to to the KODI screen. This works really fine and very responsive and again both systems respond in the same behaviour.

For example:

  • I’m starting a movie on kodi
  • MQTT is providing the new title to all system subscribed to the message (including OH)
  • After receiving the MQTT message I send back a notification to the KODI Screen “MQTT:Movie $$ has started”
  • KODI binding is taking a while 1-2minutes (and often fails) to update the title in the Paper UI
  • after receiving the KODI-Title change update I send a notification back to the KODI Screen “KODI: Move $$ has started”

The MQTT service is running fine even when the KODI service is showing OFFLINE for the KODI Thing

Any ideas on how to investigate on that issue? Is anybody else having the same situation ?

Additional update:

I’m getting a similar error for both of my KODI instances.

  1. Kodi is set to pause
  2. Kodi is resumed from PAUSE to PLAY

I’m getting the below error

2017-07-06 16:32:06.420 [ItemStateChangedEvent     ] - pKodi2_Control changed from PAUSE to PLAY
==> /var/log/openhab2/openhab.log <==
2017-07-06 16:32:06.423 [ERROR] [i.internal.protocol.KodiClientSocket] - Error handling event {"jsonrpc":"2.0","method":"Player.OnPlay","params":{"data":{"item":{"episode":4,"season":5,"showtitle":"Star Trek: The Next Generation","title":"Silicon Avatar","type":"episode"},"player":{"playerid":-1,"speed":1}},"sender":"xbmc"}} player state change message: null
java.lang.NullPointerException
	at org.openhab.binding.kodi.internal.protocol.KodiConnection.requestPlayerUpdate(KodiConnection.java:234)[189:org.openhab.binding.kodi:2.2.0.201707042200]
	at org.openhab.binding.kodi.internal.protocol.KodiConnection.processPlayerStateChanged(KodiConnection.java:377)[189:org.openhab.binding.kodi:2.2.0.201707042200]
	at org.openhab.binding.kodi.internal.protocol.KodiConnection.handleEvent(KodiConnection.java:354)[189:org.openhab.binding.kodi:2.2.0.201707042200]
	at org.openhab.binding.kodi.internal.protocol.KodiClientSocket$KodiWebSocketListener$2.run(KodiClientSocket.java:158)[189:org.openhab.binding.kodi:2.2.0.201707042200]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)[:1.8.0_121]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_121]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)[:1.8.0_121]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)[:1.8.0_121]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[:1.8.0_121]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)[:1.8.0_121]
	at java.lang.Thread.run(Thread.java:745)[:1.8.0_121]

Hey Thomas,

thanks for the update. I am a little bit helpless with your concern. I have some problems in my setup in combination of Kodi and openHAB. But nothing similar to yours. No performance problems. The Kodi binding is based on a Websocket implementation. This Websocket sometimes gets closed. But I don’t know why. E.g. if I reboot Kodi sometimes the connection gets lost, sometimes works without problems afterwards. I often have the situation, that Kodi goes to Suspend or Hibernate mode and doesn’t accept the commands from openHAB whereas every command using a third-party app (I am using Yatse) is executed without exception.

Yes, I’m a bit lost as well with these issues. I had the same problems as you have with suspend or hibernate and disabled these functionalities.

In meanwhile I found out that whenever I had any of the above issues the Kodi-Thing went OFFLINE either before or just after the issue. I will try to install some persistence logging on the Kodi-Thing and on Commands send and received from Kodi, maybe this will give me some insight.

And I can confirm on 3rd party-apps. Whenever I had an issue with Kodi(e.g Thing went offline)

  • I used Yatse to control the Kodi…it worked all the time
  • I used the MQTT messages to send and receive information … works all the time ([KODI MQTT])(https://github.com/owagner/kodi2mqtt)
  • I used the Kodi-Web interface … it worked as well all the time

My issues are often apearing in the below scenarios:

  • Kodi-Thing goes offline … commands to/from Kodi fail to execute
  • when Kodi is running and OH is restarting (e.g: Stope/Start RPI OH instance)sometimes the Kodi-Thing is not registered as ONLINE and remains OFFLINE
  • when modifying rules (no OH restart) the Kodi-Thing is going OFFLINE
  • when Kodi is crashing or the according system is powered-off … the Kodi-Thing goes OFFLINE and is not registering when Kodi is coming back online (maybe it is still in a timeout-loop?)

As said, I will try to get some more logging details to analyse. At moment, I’m suspecting that some Kodi command timeout-durations are just too long and preventing to reconnect the Kodi-Thing when trying to come back ONLINE.

Besides these issues I love the Kodi binding and the options it is opening up. I have now created virtual channel switches and able to start selected Movies and TV channels via Alexa and other triggers :grinning:

Reviving this thread…

Since upgrading from 2.4 to 2.5M4, I get this warning every 10s when something is playing:

[WARN ] [di.internal.utils.ByteArrayFileCache] - Could not write file '7b78bebb84c9d626127f50056b473d7d.248%253a32400%252fphoto%252f%253a%252ftranscode%253fwidth%253d1920%2526height%253d1920%2526minSize%253d1%2526upscale%253d0%2526url%253d%252flibrary%252fmetadata%252f1596%252fart%252f1570971676%2526X-Plex-Token%253dXScJLJbUdcybNXFyHLuv' to cache

java.nio.file.FileSystemException: /var/lib/openhab2/cache/org.openhab.binding.kodi/7b78bebb84c9d626127f50056b473d7d.248%253a32400%252fphoto%252f%253a%252ftranscode%253fwidth%253d1920%2526height%253d1920%2526minSize%253d1%2526upscale%253d0%2526url%253d%252flibrary%252fmetadata%252f1596%252fart%252f1570971676%2526X-Plex-Token%253dXScJLJbUdcybNXFyHLuv: File name too long

	at sun.nio.fs.UnixException.translateToIOException(UnixException.java:91) ~[?:?]

	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) ~[?:?]

	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) ~[?:?]

	at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214) ~[?:?]

	at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434) ~[?:?]

	at java.nio.file.Files.newOutputStream(Files.java:216) ~[?:?]

	at java.nio.file.Files.write(Files.java:3292) ~[?:?]

	at org.openhab.binding.kodi.internal.utils.ByteArrayFileCache.writeFile(ByteArrayFileCache.java:140) ~[?:?]

	at org.openhab.binding.kodi.internal.utils.ByteArrayFileCache.put(ByteArrayFileCache.java:98) ~[?:?]

	at org.openhab.binding.kodi.internal.protocol.KodiConnection.downloadImageFromCache(KodiConnection.java:893) ~[?:?]

	at org.openhab.binding.kodi.internal.protocol.KodiConnection.getImageForElement(KodiConnection.java:856) ~[?:?]

	at org.openhab.binding.kodi.internal.protocol.KodiConnection.requestPlayerItemUpdate(KodiConnection.java:654) ~[?:?]

	at org.openhab.binding.kodi.internal.protocol.KodiConnection.requestPlayerUpdate(KodiConnection.java:518) ~[?:?]

	at org.openhab.binding.kodi.internal.protocol.KodiConnection.updatePlayerStatus(KodiConnection.java:509) ~[?:?]

	at org.openhab.binding.kodi.internal.handler.KodiHandler.lambda$1(KodiHandler.java:627) ~[?:?]

	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:?]

	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [?:?]

	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [?:?]

	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [?:?]

	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:?]

	at java.util.concurrent.ThreadPoolExecutor$Worker.

As you probably see I’m using a Plex server together with Kodi.
Not sure why or where OH tries to write an image… but how can I prevent this?

Alex

Hi Alex,

Can you provide a full TRACE log? You might want to send it to me via PM to keep private data hidden. Thanks.

Kodi binding uses an internal cache to omit downloading thumbnails or fanarts all the time. In general it tries to extract the filename extension and calculates a hash for the basename to prevent such long filenames. This algorithm seem to fail in your case.

1 Like