Page 1 of 1
Python Works at Command Line Not in Domoticz
Posted: Wednesday 26 April 2017 1:53
by Lorccan
I have python script that works fine at the command line.
However I call it in Domoticz (either ON|OFF on a dummy switch, or calling from a shell script or from a Lua script) it will not work and returns an error 256.
I suspect that it's because the script is loading a non-standard module that can't be found. I have tried appending the module path to sys.path (in the .py), but this makes no difference.
Apart from telling me the solution

can anyone help with these questions please:
1. Does Domoticz run only a certain version of Python?
2. Does Domoticz need to use a special location for script modules to load successfully?
Re: Python Works at Command Line Not in Domoticz
Posted: Wednesday 26 April 2017 12:43
by jorgh
@Lorccan,
Did you correctly specify the script in the on and off action fields?
It should be something like:
script:///home/pi/domoticz/scripts/script.py param1 param2 param3
Notice the three /// if you specify the full path. I've seen others reporting it should be two, but this probably depends on wheter you specify a full or relave path.
Kind regards,
Jorg
Re: Python Works at Command Line Not in Domoticz
Posted: Wednesday 26 April 2017 12:50
by SweetPants
Lorccan wrote:1. Does Domoticz run only a certain version of Python?
To my understanding the Domoticz internal Python uses Python2, the new plugin manager Python3
I have read somewhere that this is going to be changed to Python3 only.
Re: Python Works at Command Line Not in Domoticz
Posted: Wednesday 26 April 2017 19:10
by moroen
@ Lorccan,
Are you running Domoticz on *nix or windows?
Scripts run from on/off-actions should not be dependent on the internal python interpreter. If running on some kind of linux, have you tried the following hashbang?
#!/usr/bin/env python3
or
#!/usr/bin/env python
It should give your script the same runtime environment as from the command-line.
M
Re: Python Works at Command Line Not in Domoticz
Posted: Thursday 27 April 2017 15:18
by Lorccan
Sorry for slow response - I didn't get notification of replies.
I am running on an RPi under Raspbian - so, yes *nix - and I did already have the hashbangs in place.
I have managed to get it working now. I needed to do a couple of things:
Explicitly point to an external module:
Code: Select all
import sys
sys.path.append('/home/pi/pyenergenie-master/src')
and, most importantly:
This is to ensure that when the script is run by Domoticz it is elevated to root privileges and overcomes any permissions issues.
Re: Python Works at Command Line Not in Domoticz
Posted: Thursday 27 April 2017 21:09
by Lorccan
I spoke too soon:
A short while after I posted my 'solution' - when I was quite happily calling the python scripts directly from the dummy switch on|off action - I started to see the error:
Code: Select all
Error: Error executing script command (/home/pi/domoticz/scripts/python/ceiling_on.py). returned: 256
I have tried once more wrapping the start of this script in a shell script, but this makes no difference:
Code: Select all
Error: Error executing script command (/home/pi/domoticz/scripts/python/start_ceiling_on.sh). returned: 256
The only thing that I can get to work is to make the on|off action touch a file in a directory that's being monitored by inotifywait, this then kicks-off the python script - but it's obviously slower than the direct link that I had working earlier on today.
NB: I have not changed anything in the environment - no OS or Domoticz updates - that could have caused the behaviour to change.
Re: Python Works at Command Line Not in Domoticz
Posted: Thursday 27 April 2017 22:35
by moroen
@Lorccan,
would you mind posting your script?
M
Re: Python Works at Command Line Not in Domoticz
Posted: Friday 28 April 2017 10:15
by Lorccan
This is it. As you can see it loads the energenie module (which is available at
https://github.com/whaleygeek/pyenergenie) and this drives a dedicated transceiver hat on the RPi. (ENER314-RT)
Code: Select all
#!/usr/bin/env python3
import time
import sys
sys.path.append('/home/pi/pyenergenie-master/src')
import energenie
APP_DELAY = 1
energenie.init()
me_global = sys.modules[__name__]
energenie.registry.load_into(me_global)
ceiling.turn_on()
time.sleep(APP_DELAY)
# END
Re: Python Works at Command Line Not in Domoticz
Posted: Friday 28 April 2017 15:31
by Lorccan
jorgh wrote:@Lorccan,
Did you correctly specify the script in the on and off action fields?
It should be something like:
script:///home/pi/domoticz/scripts/script.py param1 param2 param3
Notice the three /// if you specify the full path. I've seen others reporting it should be two, but this probably depends on wheter you specify a full or relave path.
Kind regards,
Jorg
Sorry Jorg, I overlooked your post. Yes, I have the three slashes and a fully qualified path. The only difference is that I have the .py's in a subfolder of scripts named 'python', so: `script:///home/pi/domoticz/scripts/python/ceiling_on.py`
Re: Python Works at Command Line Not in Domoticz
Posted: Friday 28 April 2017 17:39
by moroen
@Lorccan,
I'll take a closer look at your script, but in the meantime:
If the script lives in the scripts/python-subdirectory under domoticz, try calling it with relative path, for example:
script://python/tradfri.py 0 on
The script should be owned by the same user as domoticz runs as, and have the execute-bit set!
M
Re: Python Works at Command Line Not in Domoticz
Posted: Friday 28 April 2017 20:52
by Lorccan
I believe I have finally solved the problem - not so that the script runs as it should, but it will do for now:-
The script reads a file named registry.kv which supplies a dictionary of device names and addresses. I tried calling the .py from Lua and diverting stdout to a file. There I found that the name 'ceiling' was not recognised - because the reading of the registry is not working. (I did not write the python/c modules, so I am waiting for an answer from the owner about where the file needs to be located.)
I was able to fix it by forgetting the registry for now and hard-coding an address to turn on. (As I only have the one device of this type it's not a hardship!)
I'll post back with updates if I manage to get any further, but thanks for your suggestions.
Re: Python Works at Command Line Not in Domoticz
Posted: Wednesday 18 March 2020 15:38
by wube
Hi Everyone, any news on that. I'm struggling with exactly the same problem. I would like to use TP-Link HS100 from domoticz and the same thing happens, script runs fine from SSH but when added to dummy, no go. Thanks for any advice.