Newbie Question - Can I trigger Python Scripts from Domoticz

Python and python framework

Moderator: leecollings

The_it_bloke
Posts: 8
Joined: Sunday 08 June 2014 9:04
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: New Forest National Park, UK
Contact:

Newbie Question - Can I trigger Python Scripts from Domoticz

Post 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
xKingx
Posts: 15
Joined: Tuesday 03 June 2014 11:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Holland
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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
Raspberry Pi running Domoticz image
The_it_bloke
Posts: 8
Joined: Sunday 08 June 2014 9:04
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: New Forest National Park, UK
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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.
:D
The_it_bloke
Posts: 8
Joined: Sunday 08 June 2014 9:04
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: New Forest National Park, UK
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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
The_it_bloke
Posts: 8
Joined: Sunday 08 June 2014 9:04
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: New Forest National Park, UK
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post by The_it_bloke »

Unfortunately getting nowhere fast :cry:

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
xKingx
Posts: 15
Joined: Tuesday 03 June 2014 11:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Holland
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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!
Raspberry Pi running Domoticz image
The_it_bloke
Posts: 8
Joined: Sunday 08 June 2014 9:04
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: New Forest National Park, UK
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

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

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 :D
xKingx
Posts: 15
Joined: Tuesday 03 June 2014 11:23
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Holland
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post by xKingx »

You're welcome. Good to hear!
Raspberry Pi running Domoticz image
pafs
Posts: 4
Joined: Thursday 10 July 2014 15:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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?
kylegordon
Posts: 26
Joined: Thursday 31 October 2013 23:58
Target OS: Linux
Domoticz version: Trunk
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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
1 x HP DL380 & KVM
1 x RPI
1 x RFXtrx433 V78
11 x LWRF Switches
1 x LIFX
2 x Echo Dots
6 x Byron PIRs
2 x Nexa PIRs
2 x Kodi
2 x ESP8266 MQTT
1 x GPIO/MQTT PIR
1 x GPIO(PWM)/MQTT LEDs
1 x GPIO(SPI)/MQTT LPD6803 LEDs
Lots of Node-Red glue
pafs
Posts: 4
Joined: Thursday 10 July 2014 15:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

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

Code: Select all

script:///home/pi/domoticz/scripts/round_modus.py -u [email protected] -p mypassword --away
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
I am not active on this forum anymore.
gerardvs
Posts: 81
Joined: Sunday 04 January 2015 0:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest-1
Location: /dev/null
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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
domo_py_switch.PNG (83.36 KiB) Viewed 16720 times

Alternatively you can start a shell script and call the python scriipt from there.

--Gerard
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

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

Code: Select all

#!/bin/bash 
/usr/bin/python /home/pi/domoticz/scripts/round_modus.py "-u [email protected] -p w8woord --away"
I gave both the .py and .sh scripts executable rights (chmod +x filename)

So i don't know anymore now...........
I am not active on this forum anymore.
jannl
Posts: 666
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post by ThinkPad »

I now have this bash script which works when executed on commandline:
thermostaat_laag.sh

Code: Select all

#!/bin/bash 
/usr/bin/python /home/pi/domoticz/scripts/round_modus.py -u [email protected] -p w8woord --away
For the switch action i have:

Code: Select all

script:///home/pi/domoticz/scripts/thermostaat_laag.sh
And it now finally works :D
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
I am not active on this forum anymore.
gerardvs
Posts: 81
Joined: Sunday 04 January 2015 0:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest-1
Location: /dev/null
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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
gerardvs
Posts: 81
Joined: Sunday 04 January 2015 0:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest-1
Location: /dev/null
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post by gerardvs »

Just to be sure I have your entire commandline succesfully tested.

The script line:

Code: Select all

script://home/pi/domoticz/scripts/py_test.py -u [email protected] -p mypassword --away
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:

Code: Select all

2015-02-12_21:11:48 Fan is: [/home/pi/domoticz/scripts/py_test.py][-u][[email protected]][-p][mypassword][--away]
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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 :)
I am not active on this forum anymore.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Newbie Question - Can I trigger Python Scripts from Domo

Post 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
I am not active on this forum anymore.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest