Page 1 of 2
Newbie Question - Can I trigger Python Scripts from Domoticz
Posted: Friday 20 June 2014 16:56
by The_it_bloke
Sorry if this is an obvious answer but i have spent hours reading the FAQ, WIKI and many posts and not come up with an answer.
I want to trigger a Python script from an event such as a switch change or a sensor change/threshold etc.
All the posts seem to indicate people are running a python script externally from a scheduler or loop continuously in the background. I would like to trigger a short script from an event to interact with an SPI connected device or 3 so the script is only running as required and is short and fast.
Can this be done or do triggered scripts have to be Lua format?
Many thanks
Paul
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Friday 20 June 2014 20:41
by xKingx
Bit of a newbie to Domoticz too. However as you can just stick scripts in ~/domoticz/scripts (path on my Pi, could be different for you), you could do something like this :
example.py
Code: Select all
#!/usr/bin/python
mylogfile = '/home/pi/domoticz/scripts/example.log'
f = open(mylogfile, 'a')
f.write('Writing Example')
f.close()
In the switch set the action you want to execute the script.
script://home/pi/domoticz/scripts/example.py
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Friday 20 June 2014 23:16
by The_it_bloke
Thank you for your clear response, the bit I was missing was the file location reference in the switch tab (script://home/pi/domoticz/scripts/example.py), I couldn't find this information anywhere.
This is really helpful, I shall give this a try in the morning.
Many thanks again for taking the time to reply.

Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Saturday 28 June 2014 18:38
by The_it_bloke
Unfortunately i am still having issues getting python scripts to run

once again I have been at it for hours trying to get something simple to work
I have followed the instructions above, built a simple python script and setup a switch with a link to the script.
Try as i might i can't seem to be able to get the script to run (example above).
There is nothing I can see in the log to indicate it is even trying to run the script let alone an error message.
Is there anything anyone can suggest that i have missed?
I have written the script /home/pi/domoticz/scripts/example.py
It should create a file /home/pi/domoticz/scripts/example.log
The file starts with the line #!/usr/bin/python -
I checked and there is a file named python in this location, although it looks like a link to another location rather than an actual file?
If I log into the pi as 'pi' and run the script from IDLE in the GUI then the script works and creates the example.log file
If i try to run the script (or any other python script) from Domoticz nothing happens
Do i need to do anything to allow Domoticz to access Python? Is this likely to be a permissions thing?
As always, any suggestions would be appreciated.
Thanks
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Monday 30 June 2014 10:38
by The_it_bloke
Unfortunately getting nowhere fast
Before I report a bug, can someone please confirm that they are using the 'On Action' or 'Off Action' sections in the Switches Tab to run a Python script when a switch event occurs?
N.B. Place
script://home/pi/domoticz/scripts/example.py in either On Action or Off Action
The python script I am using is;
Code: Select all
#!/usr/bin/python
mylogfile = '/home/pi/domoticz/scripts/example.log'
f = open(mylogfile, 'a')
f.write('Writing Example')
f.close()
This script should simply create a file named example.log in the /home/pi/domoticz/scripts directory.
When I action the switch the Python script does not run or fails and I can't figure out why.
If anyone can confirm this is working on their system then at least i can isolate the issue.
Many thanks
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Tuesday 01 July 2014 9:33
by xKingx
I tried again and works fine for me.
What I did:
- I created a virtual on/off switch.
- Put the example.py in ~/domoticz/scripts
- Made the example.py executable
- Added the full path to the scripts to the "On Action" like this : script:///home/pi/domoticz/scripts/example.py
- Triggered the switch to see the log appear
Good luck!
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Tuesday 01 July 2014 17:10
by The_it_bloke
Thanks xKingx for taking the time to respond with such a clear and logical answer, I can finally see where I am going wrong!
Silly me, being a windows man I didn't know you had to make the python script files executable! You don't have to do this is when writing them in IDLE so haven't run into this yet,..... ahhhh I am so dumb
Anyway, thanks to your conscice answer I now have a clear instruction to follow.
Many thanks, I can stop pulling my hair out now

Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Tuesday 01 July 2014 22:15
by xKingx
You're welcome. Good to hear!
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Wednesday 23 July 2014 11:12
by pafs
xKingx wrote:I tried again and works fine for me.
What I did:
- I created a virtual on/off switch.
- Put the example.py in ~/domoticz/scripts
- Made the example.py executable
- Added the full path to the scripts to the "On Action" like this : script:///home/pi/domoticz/scripts/example.py
- Triggered the switch to see the log appear
Good luck!
Hi,
I'm also struggling with this. I can't get python scripts to run. The example.py is certainly executable (checked with the ls command) and everything is setup as described above. But the 'example.log' file doesn't appear.
One thing I noticed that may be my issue: In the '/usr/bin/' folder there is nothing that includes 'python', neither file or folder. I'm using the Raspberry Pi image. Is the python interpreter included in the image? Do we have to install it manually?
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Wednesday 23 July 2014 16:43
by kylegordon
If 'ls -l /usr/bin/python' shows no results, you can always install it with 'sudo apt-get install python'
I'm not sure if Raspberry Pi images include Python, but I'd be surprised if they don't. Minimal python support is a prerequisite of Debian these days
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Thursday 24 July 2014 9:56
by pafs
Thank you for your prompt reply.
'ls -l /usr/bin/python' showed no results.
'sudo apt-get install python' installed python successfully.
All my python scripts are now running fine.
Minor issue: The only python script that is still failing is the above example script. The 'example.log' file simply does not appear. Not biggie, of course, I'm just a little puzzled by it.
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Wednesday 11 February 2015 16:47
by ThinkPad
I would like to trigger a Python script from the 'On' action of a switch. I already managed to get the .log example of above working.
But how can i get a Python script that expects extra arguments working by activating a switch? I tried this:
but it doesn't work.
Maybe call the script from an other Python script? I found this:
Code: Select all
###
import os
print "I will call this other program called hello.py"
os.system("python hello.py")
###
Source:
https://mail.python.org/pipermail/tutor ... 17368.html
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Wednesday 11 February 2015 19:42
by gerardvs
Well, I just tested this (with only 1 parameter) but it works as expected.
This is the test script
Code: Select all
#!/usr/bin/python
import sys
import datetime
fmt = "%Y-%m-%d_%H:%M:%S"
time = datetime.datetime.now().strftime(fmt)
mylogfile = '/mnt/pilog/logs/py_test.log'
if len(sys.argv)>1:
cmd = str(sys.argv[1])
else:
cmd = '<unknown>'
logline = "{0} Fan is {1}\n".format(time,cmd)
f = open(mylogfile, 'a')
f.write(logline)
f.close()
This is de Domo config

- domo_py_switch.PNG (83.36 KiB) Viewed 16965 times
Alternatively you can start a shell script and call the python scriipt from there.
--Gerard
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Wednesday 11 February 2015 22:25
by ThinkPad
Hmmm it doesn't seem to be working, can't get it working :/
How much forward slashes do you need after the 'script:' ? In your screenshot you use 2, but in the example posted further above, there are used 3.
I tried using 2 as well as 3 forward slashes, but both don't work.
But when i execute the command at the commandline with 'python' in front of it (instead of 'script://') it works like it should....
Maybe it is not working because of special characters like the '@' in my e-mailadress? Can i escape these somehow? (single quotes or something) ?
Or tell me how to execute a python script from within a Bash script

Which Domoticz version are you using btw? I am using latest stable, v2.2025
Edit: Ok, also tried executing it from a Bash/shell script, but no luck.
Switch action: script://home/pi/domoticz/scripts/thermostaat_laag.sh
thermostaat_laag.sh:
I gave both the .py and .sh scripts executable rights (chmod +x filename)
So i don't know anymore now...........
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Thursday 12 February 2015 6:57
by jannl
Not quite sure about the python syntax, but linux wise the quotes around the parameters look strange. Dis you try with the quotes in the bash script? Does the bash script run from the commandline? Domoticz runs as user root, so the script will run as root as well.
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Thursday 12 February 2015 11:41
by ThinkPad
I now have this bash script which works when executed on commandline:
thermostaat_laag.sh
For the switch action i have:
Code: Select all
script:///home/pi/domoticz/scripts/thermostaat_laag.sh
And it now finally works

The scripts are used to control my Honeywell Round Connected Modulation to go into away/eco/normal mode. This is done by using the
evohome-client and some scripts from nickyb2:
round_modus.py
round_temp.py
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Thursday 12 February 2015 20:56
by gerardvs
ThinkPad wrote:Maybe it is not working because of special characters like the '@' in my e-mailadress?
No, this is not an issue. I just tried this without any problem, the email address is passed to the py script.
If you would post (or PM) the script I can test this in my environment if you like.
--Gerard
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Thursday 12 February 2015 21:29
by gerardvs
Just to be sure I have your entire commandline succesfully tested.
The script line:
The script:
Code: Select all
#!/usr/bin/python
import sys
import datetime
fmt = "%Y-%m-%d_%H:%M:%S"
time = datetime.datetime.now().strftime(fmt)
mylogfile = '/mnt/pilog/logs/py_test.log'
logline = "{0} Fan is: ".format(time)
for argje in sys.argv:
logline = logline + "[" + argje + "]"
logline = logline + "\n"
f = open(mylogfile, 'a')
f.write(logline)
f.close()
The output in the logfile:
Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Thursday 12 February 2015 22:01
by ThinkPad
Thanks for testing!
I already got it working by going the route by using an additional Bash script. Maybe i will try again later, but for now it works

Re: Newbie Question - Can I trigger Python Scripts from Domo
Posted: Friday 20 February 2015 13:17
by ThinkPad
Migrated Domoticz to my NAS, and directly calling the Python script from a switch is working now
On action (set thermostat to eco mode, setpoint minus 3 degrees):
Code: Select all
script://volume1/@appstore/domoticz/scripts/round_modus.py -u EMAIL -p PASSWORD --eco
Off action (set thermostat back to normal mode):
Code: Select all
script://volume1/@appstore/domoticz/scripts/round_modus.py -u EMAIL -p PASSWORD --normal