dzVents How to translate standard LUA functions into a dzVents script

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
jake
Posts: 751
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

dzVents How to translate standard LUA functions into a dzVents script

Post by jake »

I started using dzVents because of the easy possibility to add delays to actions. I am in need of that since controlling my Onkyo receiver asks for some patience (especially after the switch-on command). Although a simple switch-on and off dzVents script works fine, I think I don't get it how to translate the original LUA functions into a dzVents script.

This is a snip from a standard LUA script file that I use for the longest time within Domoticz:

Code: Select all

-- function to get information from receiver 
 function onkyo_status(command) 
  local result = {} 
  local output = io.popen ('onkyo '..command) 
  for line in output:lines() do 
  table.insert(result, line) 
 end 
 output:close() 
 return result 
end 

-- function to change settings receiver 
 function onkyo_change(command) 
 os.execute('/usr/local/bin/onkyo --host '..onkyohostip..' --port '..onkyohostport..' '..command) 
 end 
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents How to translate standard LUA functions into a dzVents script

Post by dannybloe »

Well, dzVents is just plain old Lua with an added API to control your Dz instance so nothing prevents you from just calling this function. Simply place it somewhere in your script file:

Code: Select all


-- function to get information from receiver 
function onkyo_status(command) 
	local result = {} 
	local output = io.popen ('onkyo '..command) 
	for line in output:lines() do 
		table.insert(result, line) 
	end 
	output:close() 
	return result 
end 

-- function to change settings receiver 
function onkyo_change(command) 
	os.execute('/usr/local/bin/onkyo --host '..onkyohostip..' --port '..onkyohostport..' '..command) 
end

return {
    active = true,
    on = {
       ...
    },
    execute = function(domoticz, device)
    	local result = onkyo_status('mycommand')
    
    
    end
}

Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jake
Posts: 751
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: dzVents How to translate standard LUA functions into a dzVents script

Post by jake »

dannybloe wrote:Well, dzVents is just plain old Lua with an added API to control your Dz instance so nothing prevents you from just calling this function. Simply place it somewhere in your script file:
Thanks, it indeed works as you explain.

To reuse functions and local variables as many times in 1 script file (to only have to declare them once and don't have everything multiple times), can I do something like this in 1 script file?

Code: Select all

     return {
        active = true,
        on = {
            'test'
        },
        execute = function(domoticz, switch1)
            if (switch1.state == 'On') then
                domoticz.notify('Hey!', 'I am on!',
                domoticz.PRIORITY_NORMAL)
            else
                domoticz.notify('Hey!', 'I am off!',
                domoticz.PRIORITY_NORMAL)
            end
        end
    }
	
     return {
        active = true,
        on = {
            'test2'
        },
        execute = function(domoticz, switch2)
            if (switch2.state == 'On') then
                domoticz.notify('Hey!', 'I am on!',
                domoticz.PRIORITY_NORMAL)
            else
                domoticz.notify('Hey!', 'I am off!',
                domoticz.PRIORITY_NORMAL)
            end
        end
    }
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents How to translate standard LUA functions into a dzVents script

Post by dannybloe »

No, you cannot do that. If you want to reuse stuff you can make a module yourself and require that in all your scripts. If you do a google search for 'lua modules' you get loads of exampels. It really easy and straightforward. You can also look at how dzVents loads the modules. Just search the code for 'require' (and you may have to set some path variables).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jake
Posts: 751
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: RE: Re: dzVents How to translate standard LUA functions into a dzVents script

Post by jake »

dannybloe wrote:No, you cannot do that. If you want to reuse stuff you can make a module yourself and require that in all your scripts. If you do a google search for 'lua modules' you get loads of exampels. It really easy and straightforward. You can also look at how dzVents loads the modules. Just search the code for 'require' (and you may have to set some path variables).
Thanks for pointing me into the right direction. It works great so far!

I only had to move my library away from from domoticz\scripts\lua\scripts to domoticz\scripts\lua instead. In the dzVents script folder, dzVents expect a proper script file instead of a library module. To my surprise LUA/dzVents was able to trace down my library file in the \lua folder without a specific path to it.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents How to translate standard LUA functions into a dzVents script

Post by dannybloe »

That is because there are a couple of registered paths where LUA searches for modules. You can extend that list with more paths. That is what dzVents does in one of it's scripts. Somewhere at the beginning.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest