Lua and Kodi

Moderator: leecollings

rickwilleme
Posts: 53
Joined: Wednesday 16 December 2015 15:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.6792
Location: Landgraaf, The Netherlands
Contact:

Lua and Kodi

Post by rickwilleme »

Guys,

I'm new to Lua and I'm feeling like a total noob here.. :roll:

Once my doorbell is pressed, I would like to run a Lua script so my camera feed is being showed on my screen.
Secondly, as long as it's playing anything else but music (like a movie), it has to pause it.

I got this far:

Code: Select all

commandArray = {}
  if (devicechanged['DummyDeurbel']=='Group On') then
    print("Dummy Deurbel!")
    if (otherdevices['HTPC - Kodi'] ~= "Audio") then
        commandArray['HTPC - Kodi']='Pause'
        end
    commandArray['HTPC - Kodi']='Execute script.securitycam'
    end
return commandArray
The individual Pause and Execute statements work, but not while they're in the same script.
I think the array item for the HTPC - Kodi device gets overridden. Can anyone confirm?
So how can this be done from the same script?

Any help is appreciated!

Thanks,
Rick
Raspberry Pi ~ Fibaro Dimmer (Z-Wave) ~ Several KAKU switches and dimmers (433 MHz) ~ Amazon Echo ~ Somfy RTS ~ Sony TV ~ Kodi/XBMC ~ Essent E-Thermostaat (ICY) ~ Cresta thermostat ~ Wundermap Weather ~ Smoke detectors
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua and Kodi

Post by simonrg »

commandArray is purely an array, so you are setting the 'HTPC - Kodi' item to 'Pause', then this value is overwritten by 'Execute ....'.

Domoticz allows you to use a different mechanism by allowing nested arrays - have a look at the wiki page http://www.domoticz.com/wiki/Events

Rewritting you script as below should work:

Code: Select all

commandArray = {}
if (devicechanged['DummyDeurbel']=='Group On') then
    print("Dummy Deurbel!")
    i = 0
    if (otherdevices['HTPC - Kodi'] ~= "Audio") then
        i = i + 1
        commandArray[i]={ ['HTPC - Kodi'] = 'Pause' }
    end
    i = i + 1
    commandArray[i] = { ['HTPC - Kodi'] = 'Execute script.securitycam' }
end
return commandArray
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
MatthijsPH
Posts: 15
Joined: Friday 01 May 2015 19:27
Target OS: Linux
Domoticz version: 3.5305
Location: The Netherlands
Contact:

Re: Lua and Kodi

Post by MatthijsPH »

Can't you make script.securitycam pause Kodi?

Sent from my HTC One_E8 dual sim using Tapatalk
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua and Kodi

Post by dannybloe »

I'd use dzVents for this. If there are Kodi specific commands missing then I can add them.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rickwilleme
Posts: 53
Joined: Wednesday 16 December 2015 15:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.6792
Location: Landgraaf, The Netherlands
Contact:

Re: Lua and Kodi

Post by rickwilleme »

simonrg wrote:commandArray is purely an array, so you are setting the 'HTPC - Kodi' item to 'Pause', then this value is overwritten by 'Execute ....'.

Domoticz allows you to use a different mechanism by allowing nested arrays - have a look at the wiki page http://www.domoticz.com/wiki/Events

Rewritting you script as below should work:

Code: Select all

commandArray = {}
if (devicechanged['DummyDeurbel']=='Group On') then
    print("Dummy Deurbel!")
    i = 0
    if (otherdevices['HTPC - Kodi'] ~= "Audio") then
        i = i + 1
        commandArray[i]={ ['HTPC - Kodi'] = 'Pause' }
    end
    i = i + 1
    commandArray[i] = { ['HTPC - Kodi'] = 'Execute script.securitycam' }
end
return commandArray
Thanks simonrg for your help. Unfortunately, it doesn't seem to work as well.
Even if I simplify my code to the one below, only the last command gets executed. :|

Code: Select all

commandArray = {}
if (devicechanged['DummyDeurbel']=='Group On') then
    print("Dummy Deurbel!")
    commandArray[1] = { ['HTPC - Kodi'] = 'Execute script.securitycam' }
    commandArray[2] = { ['HTPC - Kodi'] = 'Pause' }
end
return commandArray
Raspberry Pi ~ Fibaro Dimmer (Z-Wave) ~ Several KAKU switches and dimmers (433 MHz) ~ Amazon Echo ~ Somfy RTS ~ Sony TV ~ Kodi/XBMC ~ Essent E-Thermostaat (ICY) ~ Cresta thermostat ~ Wundermap Weather ~ Smoke detectors
rickwilleme
Posts: 53
Joined: Wednesday 16 December 2015 15:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.6792
Location: Landgraaf, The Netherlands
Contact:

Re: Lua and Kodi

Post by rickwilleme »

MatthijsPH wrote:Can't you make script.securitycam pause Kodi?
Hi Matthijs,

Fair point. However, I did not write that script myself and don't want to mess around with it too much.
Rather keep all the logic and events in one place (Domoticz)
Raspberry Pi ~ Fibaro Dimmer (Z-Wave) ~ Several KAKU switches and dimmers (433 MHz) ~ Amazon Echo ~ Somfy RTS ~ Sony TV ~ Kodi/XBMC ~ Essent E-Thermostaat (ICY) ~ Cresta thermostat ~ Wundermap Weather ~ Smoke detectors
rickwilleme
Posts: 53
Joined: Wednesday 16 December 2015 15:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.6792
Location: Landgraaf, The Netherlands
Contact:

Re: Lua and Kodi

Post by rickwilleme »

dannybloe wrote:I'd use dzVents for this. If there are Kodi specific commands missing then I can add them.
Thanks Danny, looks very promising! Has this already been implemented in the latest Domoticz build or do I still need to install it manually? (Currently using 3.5228)
Raspberry Pi ~ Fibaro Dimmer (Z-Wave) ~ Several KAKU switches and dimmers (433 MHz) ~ Amazon Echo ~ Somfy RTS ~ Sony TV ~ Kodi/XBMC ~ Essent E-Thermostaat (ICY) ~ Cresta thermostat ~ Wundermap Weather ~ Smoke detectors
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua and Kodi

Post by dannybloe »

You still need to install it manually (just copy some files, quite easy. Instructions are in the manual).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rickwilleme
Posts: 53
Joined: Wednesday 16 December 2015 15:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.6792
Location: Landgraaf, The Netherlands
Contact:

Re: Lua and Kodi

Post by rickwilleme »

dannybloe wrote:You still need to install it manually (just copy some files, quite easy. Instructions are in the manual).
I performed the steps described on Github, but it doesn't look like the event gets triggered. (Nothing is listed in the logs after triggering the test script with a switch)
:cry:
Raspberry Pi ~ Fibaro Dimmer (Z-Wave) ~ Several KAKU switches and dimmers (433 MHz) ~ Amazon Echo ~ Somfy RTS ~ Sony TV ~ Kodi/XBMC ~ Essent E-Thermostaat (ICY) ~ Cresta thermostat ~ Wundermap Weather ~ Smoke detectors
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua and Kodi

Post by dannybloe »

rickwilleme wrote:
dannybloe wrote:You still need to install it manually (just copy some files, quite easy. Instructions are in the manual).
I performed the steps described on Github, but it doesn't look like the event gets triggered. (Nothing is listed in the logs after triggering the test script with a switch)
:cry:
  • Make sure you have the dzVents folder in the correct place with all its contents (../domoticz/scripts/lua/dzVents)
  • puy both scripts script_device.main.lua and script_time_main.lua in your Lua script folder (../domoticz/scripts/lua) and
  • you renamed the settings file to dzVents_settings.lua (and set the http parameters inside to correct ip address etc).
  • You created a subfolder 'scripts' inside your Lua folder (../domoticz/scripts/lua/scripts) and
  • in there create a script file according to the examples/generic.lua (edited to your needs).
  • Make sure the script is active: active=true
If you put this script in the scripts folder you should see stuff in the logs:

Code: Select all

return {
	active = true,
	on = {
		'<exact name of the switch of id of the switch>'
	},

	execute = function(domoticz, switch) 
		domoticz.log('This should show up in the logs when the switch is triggerd')
		domoticz.log(tostring(switch.state))
	end
}
If the switch isn't triggered, make sure the name or the id is correct in your 'on' part of the script. Always check the domoticz log. dzVents does a lot of reporting there.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rickwilleme
Posts: 53
Joined: Wednesday 16 December 2015 15:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.6792
Location: Landgraaf, The Netherlands
Contact:

Re: Lua and Kodi

Post by rickwilleme »

Figured out that my folder structure was incorrect. (Even after checcking.....)
Is this why they say that you shouldn't combine coding with alcohol? :lol:
Raspberry Pi ~ Fibaro Dimmer (Z-Wave) ~ Several KAKU switches and dimmers (433 MHz) ~ Amazon Echo ~ Somfy RTS ~ Sony TV ~ Kodi/XBMC ~ Essent E-Thermostaat (ICY) ~ Cresta thermostat ~ Wundermap Weather ~ Smoke detectors
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua and Kodi

Post by dannybloe »

:lol:
Let me know if there still problems or missing kodi support.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rickwilleme
Posts: 53
Joined: Wednesday 16 December 2015 15:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.6792
Location: Landgraaf, The Netherlands
Contact:

Re: Lua and Kodi

Post by rickwilleme »

dannybloe wrote::lol:
Let me know if there still problems or missing kodi support.
What command should I use for executing the Kodi script?
Tried two ways but neither of them seem to work. Pausing video does work, so I know the Lua script is being triggered well.

Code: Select all

return {
	active = true,
	on = {
		'DummyDeurbel'
	},

	execute = function(domoticz, mySwitch)
		if (mySwitch.state == 'Group On') then
			local kodi = domoticz.devices['HTPC - Kodi']
			-- Pause if not playing music
			if (kodi.state ~= "Audio") then
        		kodi.state = 'Pause'
        		end
        	
        	-- Execute script SecurityCam
    		kodi.state = 'Execute script.securitycam'
    		kodi.execute = 'script.securitycam'
		end
	end
}
Raspberry Pi ~ Fibaro Dimmer (Z-Wave) ~ Several KAKU switches and dimmers (433 MHz) ~ Amazon Echo ~ Somfy RTS ~ Sony TV ~ Kodi/XBMC ~ Essent E-Thermostaat (ICY) ~ Cresta thermostat ~ Wundermap Weather ~ Smoke detectors
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua and Kodi

Post by dannybloe »

rickwilleme wrote:
dannybloe wrote::lol:
Let me know if there still problems or missing kodi support.
What command should I use for executing the Kodi script?
Tried two ways but neither of them seem to work. Pausing video does work, so I know the Lua script is being triggered well.

Code: Select all

return {
	active = true,
	on = {
		'DummyDeurbel'
	},

	execute = function(domoticz, mySwitch)
		if (mySwitch.state == 'Group On') then
			local kodi = domoticz.devices['HTPC - Kodi']
			-- Pause if not playing music
			if (kodi.state ~= "Audio") then
        		kodi.state = 'Pause'
        		end
        	
        	-- Execute script SecurityCam
    		kodi.state = 'Execute script.securitycam'
    		kodi.execute = 'script.securitycam'
		end
	end
}
:D
No, I guess this won't work indeed. So, let's start at the beginning. I assume that you do get into the first if-block right? Second, modifying devices with dzVents is not by setting the state. You have to use any of the device methods as listed here. So in your case you could try kodi.setState('Pause').
Third, I just learned there are specific kodi commands (from the wiki):

commandArray['Lounge']='Off'
commandArray['Lounge']='Pause'
commandArray['Lounge']='Set Volume 30'
commandArray['Lounge']='Play Playlist Indierock'
commandArray['Lounge']='Play Playlist ActionMovies 15'
commandArray['Lounge']='Play Favorites'
commandArray['Lounge']='Play Favorites 3'
commandArray['Lounge']='Execute script.securitycam'

Currently there is no kodi support in dzVents specific for this so you have to use the generic methods:

domoticz.sendCommand('HTPC - Kodi', 'Execute script.securitycam')

so your script will be something like this:

Code: Select all

return {
	active = true,
	on = {
		'DummyDeurbel'
	},

	execute = function(domoticz, deurbel)
		if (deurbel.state == 'Group On') then
			local kodi = domoticz.devices['HTPC - Kodi']
			-- Pause if not playing music
			if (kodi.state ~= "Audio") then
				kodi.setState('Pause')
			end

			domoticz.sendCommand('HTPC - Kodi', 'Execute script.securitycam')

		end
	end
}
I will add the kodi commands and let you know.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rickwilleme
Posts: 53
Joined: Wednesday 16 December 2015 15:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.6792
Location: Landgraaf, The Netherlands
Contact:

Re: Lua and Kodi

Post by rickwilleme »

Seems like it's the old scenario all over again.
Individual commands work great, but as soon as a second command should be executed, only the last one gets send to Kodi

Code: Select all

return {
	active = true,
	on = {
		'DummyDeurbel'
	},

	execute = function(domoticz, mySwitch)
		if (mySwitch.state == 'Group On') then
			local kodi = domoticz.devices['HTPC - Kodi']
        		domoticz.sendCommand('HTPC - Kodi', 'Paused')
    			domoticz.sendCommand('HTPC - Kodi', 'Execute script.securitycam')
		end
	end
}
This only triggers the securitycam script.. :cry:
Raspberry Pi ~ Fibaro Dimmer (Z-Wave) ~ Several KAKU switches and dimmers (433 MHz) ~ Amazon Echo ~ Somfy RTS ~ Sony TV ~ Kodi/XBMC ~ Essent E-Thermostaat (ICY) ~ Cresta thermostat ~ Wundermap Weather ~ Smoke detectors
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua and Kodi

Post by dannybloe »

Ah, I get it. I assume that the command is not properly handled by the plugin then. I tried to see if I can see anything in the Domoticz code but couldn't see it. I guess you should create a ticket in github for it.

In the meantime.. can you do this from the Domoticz GUI? If so then there is probably a json command that you send instead (inspect the Network tab in your browser console).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua and Kodi

Post by dannybloe »

I assume you did use 'Pause' instead of 'Paused' right?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua and Kodi

Post by dannybloe »

I just created the Kodi commands in dzVents. (dev branch). Maybe you could give it a try to see if it works.
  • kodiExecuteAddOn(addonId): Function. Will send an Execute Addon command sending no parameters. Addon IDs are embedded in the addon configuration and are not to be confused with the Addon Name. For example: http://forums.homeseer.com/showthread.php?p=1213403.
  • kodiPause(): Function. Will send a Pause command, only effective if the device is streaming.
  • kodiPlay(): Function. Will send a Play command, only effective if the device was streaming and has been paused.
  • kodiPlayFavorites([position]): Function. Will play an item from the Kodi's Favorites list optionally starting at position. Favorite positions start from 0 which is the default.
  • kodiPlayPlaylist(name, [position]): Function. Will play a music or video Smart Playlist with name optionally starting at position. Playlist positions start from 0 which is the default.
  • kodiSetVolume(level): Function. Set the volume for a Kodi device, 0 <= level <= 100.
  • kodiStop(): Function. Will send a Stop command, only effective if the device is streaming.
  • kodiSwitchOff(): Function. Will turn the device off if this is supported in settings on the device.
Cheers,
Danny
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rickwilleme
Posts: 53
Joined: Wednesday 16 December 2015 15:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.6792
Location: Landgraaf, The Netherlands
Contact:

Re: Lua and Kodi

Post by rickwilleme »

Thanks Danny for the quick support on this :mrgreen:

Unfortunately I still have the same issue.

This only pauses:

Code: Select all

return {
	active = true,
	on = {
		'DummyDeurbel'
	},

	execute = function(domoticz, mySwitch)
		if (mySwitch.state == 'Group On') then
			local kodi = domoticz.devices['HTPC - Kodi']
    			kodi.kodiExecuteAddOn('script.securitycam')
    			kodi.kodiPause()
		end
	end
}
This only shows the security cam overlay:

Code: Select all

return {
	active = true,
	on = {
		'DummyDeurbel'
	},

	execute = function(domoticz, mySwitch)
		if (mySwitch.state == 'Group On') then
			local kodi = domoticz.devices['HTPC - Kodi']
			kodi.kodiPause()
    			kodi.kodiExecuteAddOn('script.securitycam')
		end
	end
}
For the record; this is the log:

Code: Select all

2016-06-09 22:40:44.422 LUA: Handling events for: "DummyDeurbel", value: "Group On"
2016-06-09 22:40:44.423 LUA: =====================================================
2016-06-09 22:40:44.423 LUA: >>> Handler: kodi
2016-06-09 22:40:44.423 LUA: >>> Device: "DummyDeurbel" Index: 330
2016-06-09 22:40:44.423 LUA: .....................................................
2016-06-09 22:40:44.424 LUA: .....................................................
2016-06-09 22:40:44.424 LUA: <<< Done
2016-06-09 22:40:44.424 LUA: -----------------------------------------------------
2016-06-09 22:40:44.425 LUA: [1] = HTPC - Kodi: Execute script.securitycam
2016-06-09 22:40:44.425 LUA: [2] = HTPC - Kodi: Pause
2016-06-09 22:40:44.425 LUA: =====================================================
2016-06-09 22:40:44.434 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_main.lua
Raspberry Pi ~ Fibaro Dimmer (Z-Wave) ~ Several KAKU switches and dimmers (433 MHz) ~ Amazon Echo ~ Somfy RTS ~ Sony TV ~ Kodi/XBMC ~ Essent E-Thermostaat (ICY) ~ Cresta thermostat ~ Wundermap Weather ~ Smoke detectors
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Lua and Kodi

Post by dannybloe »

I'd make a ticket in github tracker for Domoticz.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest