Page 1 of 1

Turn switch on when pausing kodi?

Posted: Friday 16 December 2016 10:52
by tjabas
Hi!

Im a new user of Domoticz and i think that its a great automation tool.
My question is, is it possible to turn on and of switches when i play and pause kodi?
If so, how should i set it up!
I use kodi in another device, its a minix media player that runs kodi under Android.

Thanks in advance.

Re: Turn switch on when pausing kodi?

Posted: Friday 16 December 2016 12:10
by cherowley
Here's a script I use to carry on watching a program (tv show of film) in bed :)

When I run the "bed time" scene it triggers this script (and turns lots of lights off).
This script checks to see if the lounge kodi is paused and therefore assumes i want to carry on watching it in bed.
If not paused then it doesn't run - i'm going to bed to sleep lol

commandArray = {};
FullResult=""

function send(msg)
runcommand = "echo "..string.char(34).."POST "..msg.. " HTTP/1.1"..string.char(13)..string.char(10)..string.char(13)..string.char(10)..string.char(34).."| telnet "..IP.." "..Port.." "
print (runcommand)
os.execute(runcommand)
end

function urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end

function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')

FullResult=s
return s
end

function sendToKodi(IP, msg)
msg2="jsonrpc?request="..urlencode(msg)
runcommand = "curl http://"..(IP)..":"..(8080).."/"..(msg2)..""
print (runcommand)
os.capture(runcommand,false)
end

function sleep(n)
os.execute("sleep " .. tonumber(n))
end


if devicechanged['LoungeToBedroomKodi'] and devicechanged['LoungeToBedroomKodi']=='On' then

print("Kodi Lounge To Bedroom Script Started..")

PausedType=""
PausedID=0

--if lounge xbmc paused:
if otherdevices['Lounge (Kodi)'] == 'Paused' then

--query currently playing
sendToKodi('192.168.2.105','{"jsonrpc":"2.0","method":"Player.GetItem","params":{"properties":[],"playerid":1},"id":"VideoGetItem"}')
print(" Full Result: "..FullResult)

--store id and type playing
if string.len(FullResult) > 0 then
PausedType=string.sub(FullResult, string.find(FullResult,"type")+7, -5)
idstart=string.find(FullResult,"item")+12
idend=string.find(FullResult,",",idstart)-1
PausedID=string.sub(FullResult,idstart,idend)
print(PausedType.." "..PausedID)
end

if PausedType == 'movie' or PausedType == 'episode' then

--stop lounge kodi so it updates resume point
sendToKodi('192.168.2.105','{"jsonrpc":"2.0","id":1,"method":"Player.Stop","params":{"playerid":1}}')

--turn bedroom tv On
commandArray['Bedroom TV']='Set Level 10'

--wait a small delay to allow resume point to be updated in kodi sql database
sleep(2)

--start movie/tv show playing with resume
if PausedType == 'movie' then
sendToKodi('192.168.2.140','{"jsonrpc":"2.0","id":"1","method":"Player.Open","params":{"item":{"movieid":'..PausedID..'},"options":{"resume":true}}}')
end
if PausedType == 'episode' then
sendToKodi('192.168.2.140','{"jsonrpc": "2.0","id": "1","method":"Player.Open","params":{"item":{"episodeid":'..PausedID..'},"options":{"resume":true}}}')
end

--wait a small delay to allow kodi to get ready to pause
sleep(1)

--pause it, ready to watch!
sendToKodi('192.168.2.140','{"jsonrpc":"2.0","id":1,"method":"Player.PlayPause","params":{"playerid":1}}')
end

end

end

return commandArray

Re: Turn switch on when pausing kodi?

Posted: Friday 16 December 2016 12:15
by Egregius
Please use a code block for code...

I don't want Domoticz to constantly poll my Kodi player to see if it's playing or not.
I put Kodi in payse/play with a minimote.
When I pause it, light of the kithcen ('wasbak') turns on.
When I unpause it and the light is still on I update a device that triggert switch off of multiple lighst (hall, garage etc).

Code: Select all

<?php //part of pass2php
function miniliving2s()
{
    global $s,$i;
    $ctx=stream_context_create(array('http'=>array('timeout'=>3)));
    file_get_contents('http://192.168.2.7:1597/jsonrpc?request={"jsonrpc":"2.0","id":1,"method":"Player.PlayPause","params":{"playerid":1}}',false,$ctx);
    if($s['wasbak']=='On') file_get_contents('http://127.0.0.1:8084/json.htm?type=command&param=udevice&idx=198&nvalue=0&svalue=On');
    else file_get_contents('http://127.0.0.1:8084/json.htm?type=command&param=switchlight&idx=61&switchcmd=On');
}

Re: Turn switch on when pausing kodi?

Posted: Friday 16 December 2016 14:03
by tjabas
Thank you for your answers!

really nice to see that it would work.

im sorry to say that i dont understand the code that you posted, i started with Domoticz 1 week ago, so im not that good att programming yet,
Should i just copy paste the code/text?
Is it in Lua that i should paste the code/text?

im really sorry for the stupid questions :oops:

Re: Turn switch on when pausing kodi?

Posted: Friday 16 December 2016 20:21
by tjabas
i must ad that im using a logitech harmony one to control my kodi player, so i dont use the remote from Domoticz, can Domoticz somehow see the events made from kodi?

if its possible i guess that the events being made from kodi could control Domoticz to tunr on and off light when playing a movie.

Re: Turn switch on when pausing kodi?

Posted: Friday 16 December 2016 22:00
by Dnpwwo
Have a look at http://www.domoticz.com/wiki/Kodi#Event ... ifications to see if that does what you want.

Re: Turn switch on when pausing kodi?

Posted: Friday 16 December 2016 22:46
by tjabas
Dnpwwo wrote:Have a look at http://www.domoticz.com/wiki/Kodi#Event ... ifications to see if that does what you want.
thank you for answering.

i have managed to set up kodi to domoticz, so when i now play a movie i see the move title, and if i play or pause the move, it is all shown in domoticz,

i have set up some simple blocklys for some of the light at home, if a switch is triggered it will automaticly shut down after 60 mins.

but i guess that is my limit, i really dont know how to set the incomming events from kodi to switch of some of the lamps in my house.

i have looked at every block in blockly, but i cant figure out how to set it up.

i have also read that link that you provided above, but it didnt make me wiser.


I would really apericate some futher guidence, maby wich blocky i should use?


thanks in advance.

Re: Turn switch on when pausing kodi?

Posted: Saturday 17 December 2016 11:00
by Dnpwwo
I don't use Blockly but you can do it via a Lua script in the same Event page:

Code: Select all

commandArray = {}

-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
    if (deviceName=='Lounge Kodi') then
        print ("Kodi event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
        if (deviceValue == "On") or (deviceValue == "Off") then
            if (timeofday['Nighttime'] == true) and (otherdevices['Lounge Dimmer'] == 'Off') then otherdevices['Lounge Dimmer'] == 'Set Level 25' end
        elseif (deviceValue == "Paused") then
            if (timeofday['Nighttime'] == true) and (otherdevices['Lounge Dimmer'] == 'Off') then otherdevices['Lounge Dimmer'] == 'Set Level 25' end
        elseif (deviceValue == "Video") then
                if (otherdevices['Lounge Dimmer'] ~= 'Off') then commandArray['Lounge Dimmer'] = "Off" end
                if (otherdevices['Front Door Dimmer'] ~= 'Off') then commandArray['Front Door Dimmer'] = "Off AFTER 3" end
                if (otherdevices['Hall Dimmer'] ~= 'Off') then commandArray['Hall Dimmer'] = "Off AFTER 5" end
        end
    end
end
return commandArray

Re: Turn switch on when pausing kodi?

Posted: Saturday 17 December 2016 11:41
by tjabas
Dnpwwo wrote:I don't use Blockly but you can do it via a Lua script in the same Event page:

Code: Select all

commandArray = {}

-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
    if (deviceName=='Lounge Kodi') then
        print ("Kodi event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
        if (deviceValue == "On") or (deviceValue == "Off") then
            if (timeofday['Nighttime'] == true) and (otherdevices['Lounge Dimmer'] == 'Off') then otherdevices['Lounge Dimmer'] == 'Set Level 25' end
        elseif (deviceValue == "Paused") then
            if (timeofday['Nighttime'] == true) and (otherdevices['Lounge Dimmer'] == 'Off') then otherdevices['Lounge Dimmer'] == 'Set Level 25' end
        elseif (deviceValue == "Video") then
                if (otherdevices['Lounge Dimmer'] ~= 'Off') then commandArray['Lounge Dimmer'] = "Off" end
                if (otherdevices['Front Door Dimmer'] ~= 'Off') then commandArray['Front Door Dimmer'] = "Off AFTER 3" end
                if (otherdevices['Hall Dimmer'] ~= 'Off') then commandArray['Hall Dimmer'] = "Off AFTER 5" end
        end
    end
end
return commandArray
Hi!

so i just copy/paste, and replace the red marked devices with my own ones, and it should probably work?

thanks in advance

Re: Turn switch on when pausing kodi?

Posted: Saturday 17 December 2016 13:10
by tjabas
Hi!

i actually think i got it working, i tried different things in blockly, and i set it up like the picture shows.
This seems to work, when i pauses some of the lights turn on, but it seems like its only the logic blocky, paused that works.
none of the playing and stopped works, i really dont know why.

Does any one else know why none of these logical, like Playing, Stopped or stopp works?

I would like to get the lights to turn of after i stopp the movie.

[img]
kodip.PNG
kodip.PNG (12.28 KiB) Viewed 9808 times
[/img]


thanks.

Re: Turn switch on when pausing kodi?

Posted: Saturday 17 December 2016 13:35
by Egregius
Try with 'different' than Paused. Like != or /=

Re: Turn switch on when pausing kodi?

Posted: Saturday 17 December 2016 13:44
by blackdog65
I remember there was a plug-in available for Kodi that was originally meant for X10. I used it a year or 2 back when I was using Kodi. The plug-in sent json requests when a specific action was performed i.e. Play set lights off, pause set lights 50% and stop set lights back full... or however you wanted it set. Of course this can be used to switch scenes in blockly with "if dark" etc.

I've just returned to using Kodi, so I'll investigate further and report back.

Re: Turn switch on when pausing kodi?

Posted: Sunday 18 December 2016 2:03
by tjabas
thanks for your answers.

i tried everything in the blue pause block from the picture above, but nothing cant trigger the light to turn on when i stop the movie, pause works great for just pause, but nothing for stopping the movie.

any other suggerstions?

Re: Turn switch on when pausing kodi?

Posted: Sunday 18 December 2016 2:04
by tjabas
Egregius wrote:Try with 'different' than Paused. Like != or /=
i didnt really uderstand that "different", how could i ad != and/= ?

Re: Turn switch on when pausing kodi?

Posted: Sunday 18 December 2016 6:19
by Egregius
In the dropdown where you select the equal sign there's also 'not equal' indicated by a equal sign with a slash thru it:

Image

Notice: just try to help, I never use blockly...

Re: Turn switch on when pausing kodi?

Posted: Sunday 18 December 2016 11:45
by tjabas
Egregius wrote:In the dropdown where you select the equal sign there's also 'not equal' indicated by a equal sign with a slash thru it:

Image

Notice: just try to help, I never use blockly...
I apreciate your help, i will give this a try later.

Thank you.

Re: Turn switch on when pausing kodi?

Posted: Monday 16 January 2017 1:07
by Calzor Suzay
I use an addon running in kodi called callbacks, been using it on v16 & now v17 as I didn't even know the above methods existed.
http://kodi.wiki/view/Add-on:Kodi_Callbacks
http://forum.kodi.tv/showthread.php?tid=256170

Works quite well to just call a Json http request from within kodi.