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

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

Moderator: leecollings

Post Reply
MMSoft
Posts: 14
Joined: Sunday 22 September 2019 8:20
Target OS: -
Domoticz version:
Contact:

How to run a python3 script from a dzVents script ?

Post 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')
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

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

Post by felix63 »

Try using

Code: Select all

domoticz.osExecute("python3 /home/pi/domoticz/scripts/python/Test.py")
Function: Execute an os command.
MMSoft
Posts: 14
Joined: Sunday 22 September 2019 8:20
Target OS: -
Domoticz version:
Contact:

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

Post by MMSoft »

Thanks for your reply.
Unfortunately, this gives the error message: attempt to call field 'osExecute' (a nil value)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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")
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MMSoft
Posts: 14
Joined: Sunday 22 September 2019 8:20
Target OS: -
Domoticz version:
Contact:

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

Post by MMSoft »

Thanks !!!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Codec303
Posts: 1
Joined: Sunday 15 December 2019 18:11
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

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

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest