Kodi / XBMC Library Rescan via Button

If anyone wants to know how to trigger a Library rescan on a (remote) Kodi / XBMC machine, this is how I’ve done it:

Sending the curl / json command directly through a rule or from a binding was too difficult because of the escaping of the characters. So I created a script:

kodi_rescan.sh

#!/bin/bash
# Kodi Video Library Scan

curl --data-binary '{ "jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "mybash"}' -H 'content-type: application/json;' http://[user]:[pass]@[ip]:[port]/jsonrpc

Then I created an item and bound it to the sitemap as a simple push-button:
In .items

Switch i_Kodi_Rescan "Video Library" <office>

In .sitemap:

Switch item=i_Kodi_Rescan mappings=[ON="Rescan"]

Last thing is to create a rule to run the shell script when the button is pushed:
In .rules

rule "Kodi Video Library Scan"
when
	Item i_Kodi_Rescan received command
then
	val result = executeCommandLine("\/home\/user\/kodi_rescan.sh", 5000)	
	logInfo("Kodi Library Scan", result)
end

That’s it.

6 Likes

That sounds really good. I’ll have to add it to the ever growing list of things to do.

Thanks Josh

1 Like

Hi @janw , i bookmarked your post :slight_smile:
Can you help me: i want to catch button press from Kodi and send it to OH.
How i can do that?

Hi Martiniman,
Do you mean ‘customized’ button presses ? I searched also a lot for that, but haven’t found it yet. I found a add-on which uses mqtt to feedback status changes from Kodi to your mqtt broker, but it does not accept customized (with customized I mean ‘free’ shortcut keys that are not assigned to any Kodi command) button presses from Kodi.
See link: https://github.com/owagner/kodi2mqtt. If you just want feedback on certain Kodi commands, this could work for you.

Yes, i mean ‘customized’ button presses.
I want use my Harmony remote to control lights and other devices when i Kodi is running.
I can send any keypress to windows/kodi, and i need to Kodi somehow send that keypress to OH.

Same here, I use a Flirc IR receiver in a RPi3, and want to use unused buttons on remote controls to send signals to openHAB.

I think may be use keylogger and when Kodi is running, read and send to mqtt key presses from keylogger logs?

when running this froma console, it shows all the json commands from my kodi remote but doesnt trigger the video update or ever exit?

Hi Everyone Hoping for help

I have created the item created the script saved it in the right place this time and edited the rule

Rule

rule "Kodi Video Library Scan"
when
	Item Kodi_Rescan received command
then
	val result = executeCommandLine("/home/openhabian/kodi_rescan.sh", 5000)  

	logInfo("Kodi Library Scan", result)
end

item

Switch Kodi_Rescan "Video Library" <office>

script

#!/bin/bash
# Kodi Video Library Scan

curl --data-binary '{ "jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "mybash"}' -H 'content-type: application/json;' http://***user***:**pass*****@192.168.0.**:8080/jsonrpc

this is not working im getting a error 13 permission denied

2018-07-29 23:44:06.866 [WARN ] [lipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "/home/openhabian/kodi_rescan.sh" (in directory "."): error=13, Permission denied)

Hoping for some pointers or is there a different way to do this ?

@Sharpy At a guess, it looks like either the user that tries to run the script isn’t allowed to access it in the way it needs to. Check the rights on the file, is it executable for the user running it (owner/group, I would assume)?

Normally the script is executed by the user openhab. So this user needs to have permission to execute the script, if it needs sudo, you also need to update your sudoers config.
See the documentation of the exec binding in openhab Docs

This is a way to do it directly by rules (no scripts):

Item:

Switch Kodi_Rescan "Run Library scan"

Rule:

rule "Run kodi library scan"
when
	Item Kodi_Rescan received command ON
then
	var String content = '{"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "myOpenHAB"}'
	var String url = 'http://username:password@192.168.xx.zz:8081/jsonrpc?request='
	sendHttpPostRequest(url,'application/json',content)
	createTimer(now.plusSeconds(1)) [| Kodi_Rescan.postUpdate(OFF) ]
 end

If you define your switch with the autoupdate parameter then you don’t need the timer in your rule to switch the Kodi_Rescan switch back to the off position. The visual confirmation due to the switch being in the on position for one second may be a good reason to keep the current implementation though.

Switch Kodi_Rescan "Run Library scan" { autoupdate="false" }