Page 1 of 1

dzVents How to translate standard LUA functions into a dzVents script

Posted: Wednesday 01 March 2017 21:01
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 

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

Posted: Thursday 02 March 2017 8:39
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
}


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

Posted: Friday 03 March 2017 20:22
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
    }

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

Posted: Monday 06 March 2017 9:20
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).

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

Posted: Monday 06 March 2017 22:23
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.

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

Posted: Tuesday 07 March 2017 7:22
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.