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')
How to run a python3 script from a dzVents script ? [Solved]
Moderator: leecollings
- 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 ?
Try usingFunction: Execute an os command.
Code: Select all
domoticz.osExecute("python3 /home/pi/domoticz/scripts/python/Test.py")
Re: How to run a python3 script from a dzVents script ?
Thanks for your reply.
Unfortunately, this gives the error message: attempt to call field 'osExecute' (a nil value)
Unfortunately, this gives the error message: attempt to call field 'osExecute' (a nil value)
- 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 ?
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
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
- 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 ?
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
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
-
- 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 ?
Hi, where would I need to add this code in order to use it in my script? Thanks
- 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 ?
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
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Who is online
Users browsing this forum: No registered users and 1 guest