Page 1 of 1

How to run a python3 script from a dzVents script ?

Posted: Thursday 28 November 2019 7:50
by MMSoft
Is it possible to run a python3 script from a dzVents script ?
And if so how is that exactly?

This does not work:
shellCommand = "python3 /home/pi/domoticz/scripts/python/Test.py"
sys.executable('python3 /home/pi/domoticz/scripts/python/Test.py')
os.system('python3 /home/pi/domoticz/scripts/python/Test.py')

Re: How to run a python3 script from a dzVents script ?

Posted: Thursday 28 November 2019 10:01
by felix63
Try using

Code: Select all

domoticz.osExecute("python3 /home/pi/domoticz/scripts/python/Test.py")
Function: Execute an os command.

Re: How to run a python3 script from a dzVents script ?

Posted: Thursday 28 November 2019 10:50
by MMSoft
Thanks for your reply.
Unfortunately, this gives the error message: attempt to call field 'osExecute' (a nil value)

Re: How to run a python3 script from a dzVents script ?

Posted: Thursday 28 November 2019 14:40
by waaren
MMSoft wrote: Thursday 28 November 2019 10:50 Thanks for your reply.
Unfortunately, this gives the error message: attempt to call field 'osExecute' (a nil value)
The command in dzVents is domoticz.utils.osExecute("python3 /home/pi/domoticz/scripts/python/Test.py") but you can also use
os.execute("python3 /home/pi/domoticz/scripts/python/Test.py")

Re: How to run a python3 script from a dzVents script ?  [Solved]

Posted: Friday 29 November 2019 10:40
by MMSoft
Thanks !!!

Re: How to run a python3 script from a dzVents script ?

Posted: Friday 29 November 2019 11:02
by waaren
MMSoft wrote: Friday 29 November 2019 10:40Thanks !!!
My personal preference for executing OS commands / scripts in dzVents is the following function.
It has a better error handling than osExecute and returns output (if any) and OS returnCode

Code: Select all

        local function osCommand(cmd)
            if dz == nil then dz = domoticz end -- make sure dz is declared as domoticz object if not already done earlier 
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

            local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
            local commandOutput = assert(fileHandle:read('*a'))
            local returnTable = {fileHandle:close()}

            if commandOutput:find '::ERROR::' then     -- something went wrong
               dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_ERROR)
            else -- all is fine!!
                dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
            end

            return commandOutput,returnTable[3] -- rc[3] contains returnCode
        end

Re: How to run a python3 script from a dzVents script ?

Posted: Tuesday 17 December 2019 18:05
by Codec303
Hi, where would I need to add this code in order to use it in my script? Thanks
waaren wrote: Friday 29 November 2019 11:02
MMSoft wrote: Friday 29 November 2019 10:40Thanks !!!
My personal preference for executing OS commands / scripts in dzVents is the following function.
It has a better error handling than osExecute and returns output (if any) and OS returnCode

Re: How to run a python3 script from a dzVents script ?

Posted: Tuesday 17 December 2019 19:12
by waaren
Codec303 wrote: Tuesday 17 December 2019 18:05 Hi, where would I need to add this code in order to use it in my script? Thanks
In the execute = section
Can be any place as long as it is above the call to this function.

See example below

Code: Select all

return 
{
    on = 
    {
        timer = { 'every minute' }
    },
    
    execute = function(dz)
        
        local function osCommand(cmd)
            if dz == nil then dz = domoticz end -- make sure dz is declared as domoticz object if not already done earlier 
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

            local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
            local commandOutput = assert(fileHandle:read('*a'))
            local returnTable = {fileHandle:close()}

            if commandOutput:find '::ERROR::' then     -- something went wrong
               dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_ERROR)
            else -- all is fine!!
                dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
            end

            return commandOutput,returnTable[3] -- rc[3] contains returnCode
        end
        
        local result, rc = osCommand('ls -l')
        
        dz.log('return Code: ' .. rc  ,dz.LOG_FORCE)
        dz.log('Result: \n' .. result,dz.LOG_FORCE)
        
    end
}