XBMC security cam addon

Hey Guys,

Found a great XBMC addon that is used to setup a security camera as a picture in picture called security camera overlay (guide here for anyone interested).

I want to use motion detection via Blue Iris to trigger the security camera addon which should be straight forward but I am struggling to find a method in Openhab. I can trigger the addon using the JSON post below:

http://10.0.0.34:8084/jsonrpc?request={"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":{"addonid":"script.securitycam"},"id":"1"}}

But I don’t know how to configure this into Openhab. Couldn’t find a property in the XBMC binding wiki either.

I have the XBMC addon and the camera trigger sorted just need some help on how to trigger the Addons.ExecuteAddon on XBMC.

thanks

I have this setup on my system and had to implement via an extra python script which did the talking to Kodi.

ITEMS

Switch      VT_Living_Kodi_ViewEntry        "View Entry Camera"                     <camera>                                    { autoupdate="false" }

RULES

rule "View entry camera on living room XBMC"
when
    Item VT_Living_Kodi_ViewEntry received command ON
then
    var String command = "/opt/openhab/etc/scripts/kodicam.py 'living_xbmc:8080'"
    executeCommandLine(command, 2000)
end

rule "Entry gate opened"
when
    Item OU_Entry_Gate changed to OPEN
then
    VT_Living_Kodi_ViewEntry.sendCommand(ON)
end

PYTHON SCRIPT

#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__    = 'Ben Jones <ben.jones12()gmail.com>'
__copyright__ = 'Copyright 2014 Ben Jones'
__license__   = """Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)"""

import sys
import urllib
import urllib2
import base64
try:
    import json
except ImportError:
    import simplejson as json

KODI_ADDON_ID = "script.securitycam"

def show(kodihost):
    jsonparams = {
        "jsonrpc" : "2.0",
        "method"  : "Addons.ExecuteAddon",
        "id"      : 1,
        "params"  : { "addonid" : KODI_ADDON_ID }
    }
    jsoncommand = json.dumps(jsonparams)

    url = 'http://%s/jsonrpc' % (kodihost)
    try:
        req = urllib2.Request(url, jsoncommand)
        req.add_header("Content-type", "application/json")
        response = urllib2.urlopen(req)
    except Exception, e:
        print "Error executing Kodi addon on %s: %s" % (kodihost, str(e))

if __name__ == "__main__":
    show(sys.argv[1])

1 Like

Hey Ben,

great job, thanks for sharing.

small problem… I am running OH on Windows, I think still doable?
so, I am guessing there’s no way to keep the code within OH?

You should still be able to run python scripts on Windows.

I guess it would be nice to build this into the XBMC/Kodi binding but it is not really a core Kodi featue (i.e. is an addon) so it would need to be some generic - Activate Addon action I guess. But then some addons would require parameters etc so it would quickly become a bit tricky.

For me it is fine the way it is.

I use this
rule "Front door to XBMC"
when
Item Front_door_SW received update
then if ( Front_door_SW.state == OPEN) {

		logInfo(" Front_door_SW "," Front_door Open")

var String json = "%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22Addons.ExecuteAddon%22%2C%22params%22%3A%7B%22addonid%22%3A%22script.securitycam%22%7D%2C%22id%22%3A%221%22%7D%7D%22%29"
sendHttpGetRequest(“http://192.168.2.51:8080/jsonrpc?request=” + json) // vista laptop
sendHttpGetRequest(“http://192.168.2.35:8080/jsonrpc?request=” + json) // main xbmc
}
if ( Front_door_SW.state == CLOSED) {

logInfo(" Front_door_SW “,” Front_door Closed")

	  }
        end
1 Like

Champion Greg! That works a treat- Very easy solution

this example no longer works
getting connection refused
anyone with latest kodi getting this to work ?

can you please post a complete example as i don’t think the url is correct and i do not see any authentication ?

Here is my latest script;

#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__    = 'Ben Jones <ben.jones12()gmail.com>'
__copyright__ = 'Copyright 2014 Ben Jones'
__license__   = """Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)"""

import sys
import urllib
import urllib2
import base64
try:
    import json
except ImportError:
    import simplejson as json

KODI_ADDON_ID = "script.securitycam"

def show(kodihost):
    # add these as command line variables if needed
    kodiusername = None
    kodipassword = None

    jsonparams = {
        "jsonrpc" : "2.0",
        "method"  : "Addons.ExecuteAddon",
        "id"      : 1,
        "params"  : { "addonid" : KODI_ADDON_ID }
    }
    jsoncommand = json.dumps(jsonparams)

    url = 'http://%s/jsonrpc' % (kodihost)
    try:
        req = urllib2.Request(url, jsoncommand)
        req.add_header("Content-type", "application/json")
        if kodipassword is not None:
            base64string = base64.encodestring ('%s:%s' % (kodiusername, kodipassword))[:-1]
            authheader = "Basic %s" % base64string
            req.add_header("Authorization", authheader)
        response = urllib2.urlopen(req)
    except Exception, e:
        print "Error executing Kodi addon on %s: %s" % (kodihost, str(e))

if __name__ == "__main__":
    show(sys.argv[1])

NOTE: I don’t use authentication so you will need to adjust the script to take the username/password in alongside the Kodi host.

thank you for uploading your latest script but i still have allot of questions reading this.
where does your script gets the kodi information it needs ? does this come from the openhab.cfg ?
when you say adjust the script you mean i have to input the username and password at the lines

kodiusername = None
kodipassword = None

correct ?
can you please explain what following in your script means.

url = ‘http://%s/jsonrpc’ % (kodihost)

kindest regards

I have virtual items which I call when I want to display a camera in Kodi (I have multiple Kodi clients so I can show the camera on each one by passing the kodihost).

You will need to also pass your username and password in your rule, or just hard code them in your kodicam.py script.

url = 'http://%s/jsonrpc' % (kodihost) is a simple parameter substitution, which results in; http://living-kodi:8080/jsonrpc

Here is one of my openHAB rules which triggers the script;

rule "View entry camera on living room Kodi"
when
    Item VT_Living_Kodi_ViewEntry received command ON
then
    var String command = "/opt/openhab/etc/scripts/kodicam.py 'living-kodi:8080'"
    executeCommandLine(command, 2000)
end

so if i read this correct you call them on hostname and not by ip adress ?
do you have the xbmc binding installed aswell as the action or only the xmbc binding ?
how can i hard code the username and password ? like i suggested in previous post ? inside the py script ?
i’m running 1.8.3 on a synology station.
before i used the exec binding to use the orignial script i had but the wget command is no longer working for kodi 16.1
thats why i’m searching for a solution

this was the original script

#!/bin/bash
OH_CFG="/volume1/@appstore/OpenHAB/configurations/openhab.cfg"
WGET="/usr/bin/wget"
PARAMS="-q -O /dev/null"
JSON_ON='jsonrpc?request={“jsonrpc”:“2.0”,“id”:1,“method”:“Addons.ExecuteAddon”,“params”:{“addonid”:“script.securitycam”,“params”:{“command”:“activate”}},“id”:1}'
usage()
{
echo "Usage: $0 kodi-host ON|OFF"
echo ""
exit 1
}

if [ “$2” == “” ]; then
usage
fi

if ! cat $OH_CFG | grep “xbmc:${1}” >/dev/null 2>/dev/null; then
echo "Kodi-host $1 not found on ${OH_CFG}!"
echo ""
exit 2
fi

if ! [ “$2” == “ON” ] && ! [ “$2” == “OFF” ]; then
echo "Valid commands are ON or OFF only!"
echo ""
exit 3
fi

KODI_HOST=cat $OH_CFG | grep "xbmc:${1}.host" | awk -F'=' ' { print $2 } '
KODI_PORT=cat $OH_CFG | grep "xbmc:${1}.rsPort" | awk -F'=' ' { print $2 } '
KODI_USER=cat $OH_CFG | grep "xbmc:${1}.username" | awk -F'=' ' { print $2 } '
KODI_PASS=cat $OH_CFG | grep "xbmc:${1}.password" | awk -F'=' ' { print $2 } '

CMD="${WGET} ${PARAMS} http://“
if ! [ “${KODI_USER}” == “” ]; then
CMD=”${CMD}${KODI_USER}“
if ! [ “${KODI_PASS}” == “” ]; then
CMD=”${CMD}:${KODI_PASS}“
fi
CMD=”${CMD}@“
fi
CMD=”${CMD}${KODI_HOST}:${KODI_PORT}/“
if [ “$2” == “ON” ]; then
CMD=”${CMD}${JSON_ON}“
else
CMD=”${CMD}${JSON_OFF}"
fi

$CMD

ok Solution found :wink: without script :slight_smile:

rule "View entry camera on living room Kodi"
when
Item XbmcKODI_Frontdoorcam received command ON
then
var String json = "%7B%22jsonrpc%22%3A%222.0%22%2C%22id%22%3A1%2C%22method%22%3A%22Addons.ExecuteAddon%22%2C%22params%22%3A%7B%22addonid%22%3A%22script.securitycam%22%2C%22params%22%3A%7B%22command%22%3A%22activate%22%7D%7D%2C%22id%22%3A1%7D%0D%0A"
sendHttpGetRequest(“http://login:password@ip:port/jsonrpc?request=” + json)
end