Python Works at Command Line Not in Domoticz

Python and python framework

Moderator: leecollings

Post Reply
Lorccan
Posts: 32
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Python Works at Command Line Not in Domoticz

Post 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?
User avatar
jorgh
Posts: 124
Joined: Friday 27 June 2014 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8224
Location: Netherlands
Contact:

Re: Python Works at Command Line Not in Domoticz

Post 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
SweetPants

Re: Python Works at Command Line Not in Domoticz

Post 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.
moroen
Posts: 408
Joined: Sunday 15 January 2017 11:06
Target OS: Linux
Domoticz version: beta
Location: Norway
Contact:

Re: Python Works at Command Line Not in Domoticz

Post 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
Lorccan
Posts: 32
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Works at Command Line Not in Domoticz

Post 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:

Code: Select all

sudo chmod +s myscript
This is to ensure that when the script is run by Domoticz it is elevated to root privileges and overcomes any permissions issues.
Lorccan
Posts: 32
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Works at Command Line Not in Domoticz

Post 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.
moroen
Posts: 408
Joined: Sunday 15 January 2017 11:06
Target OS: Linux
Domoticz version: beta
Location: Norway
Contact:

Re: Python Works at Command Line Not in Domoticz

Post by moroen »

@Lorccan,

would you mind posting your script?

M
Lorccan
Posts: 32
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Works at Command Line Not in Domoticz

Post 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
Lorccan
Posts: 32
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Works at Command Line Not in Domoticz

Post 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`
moroen
Posts: 408
Joined: Sunday 15 January 2017 11:06
Target OS: Linux
Domoticz version: beta
Location: Norway
Contact:

Re: Python Works at Command Line Not in Domoticz

Post 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
Lorccan
Posts: 32
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Python Works at Command Line Not in Domoticz

Post 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.
wube
Posts: 10
Joined: Wednesday 18 October 2017 12:56
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Python Works at Command Line Not in Domoticz

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest