python script to lua

Moderator: leecollings

edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

python script to lua

Post by edwin1234 »

can someone help me to convert this pyhon script to a lua script because the script works in the command line but not in domoticz on action.
treid everything


this is the python script:

#!/usr/bin/python3

#import libraries
import os
import time

# Start the servo motors
os.system('sudo /home/pi/PiBits/ServoBlaster/user/servod')

# turn servo 0 to 10% position
os.system("echo 7=10% > /dev/servoblaster")
time.sleep(2.5)

# turn servo 0 to 60% position
os.system("echo 7=50% > /dev/servoblaster")
time.sleep(3)

os.system("echo 7=10% > /dev/servoblaster")
time.sleep(2)

os.system("echo 7=100% > /dev/servoblaster")
time.sleep(1)

os.system("echo 7=10% > /dev/servoblaster")
time.sleep(2)


# Stop the servo motors
os.system('sudo killall servod')
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: python script to lua

Post by jvdz »

I am doing something similar but use a bash script in the On action, which then shells another Bash script.
Main reason for this approach was to avoid hanging the event system while the action was running.
You could use a similar approach and shell a python script by just updating the below code.
The last & ensures the script returns immediately leaving the second script running in the background.

Code: Select all

#!/bin/bash
#--Check if script is already running and only start it when it isn't running yet.
chk=`sudo ps x | grep "Blinds_Set.sh 0" | grep -cv grep`
if  [ "$chk" = "0" ] ; then
	sh /home/pi/domoticz/scripts/Blinds_Set.sh  >> /var/tmp/blinds.log 2>&1 &
#--else
#--	echo "$(date +%x) $(date +%X) Blinds_Set.sh is already running" >> /var/tmp/blinds.log
fi
Jos
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

i tried to run the python script trough a bash script but it didnt work,
in comend line it did
thats why i want it in a lua script. but i dont know how to do it :oops:
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: python script to lua

Post by jvdz »

How does the commandline you want to start look exactly?
Do you use the fully qualified path for the python file?

Jos
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

I used for on script:///home/domoticz/scripts/python/servoscript.py

And also tried script:///home/domoticz/scripts/servoscript.sh

And
Script://scripts/python/servoscript.py
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: python script to lua

Post by jvdz »

No ... I mean what is the commandline you used in the Bash file that isn't working and the commandline that is working?
Also: what is the content of script: script:///home/domoticz/scripts/servoscript.sh ?

Jos
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

Content is:
#!/bin/bash

python3 /home/pi/domoticz/scripts/python/servoscript.py




exit 0

From the command line i do: python servoscript.py this works
And : ./servoscript.sh also works from the commandline
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: python script to lua

Post by jvdz »

Did you set the x attribute for them to allow them to be executed?
Don't forget to add the & to the end of the command to avoid hanging the event system for the duration of the script.

Jos
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

I did chmod +x
And tried sudo chmod +x
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: python script to lua

Post by jvdz »

So what happens when you have this in /home/domoticz/scripts/servoscript.sh:

Code: Select all

#!/bin/bash
python3 /home/pi/domoticz/scripts/python/servoscript.py >> /var/tmp/test.log 2>&1 &
exit 0
.. and you check the logfile for any feeedback? : /var/tmp/test.log
Change the path for the logfile to anything your system supports.
And use the On Action: script:///home/domoticz/scripts/servoscript.sh

Jos
febalci
Posts: 331
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: python script to lua

Post by febalci »

Did you tried:
script:///usr/bin/python3 /home/domoticz/scripts/python/servoscript.py (assuming python3 is under /usr/bin)

Also the same goes for bash:
#!/bin/bash
/usr/bin/python3 /home/pi/domoticz/scripts/python/servoscript.py

And you better put your bash file under:
/home/domoticz/scripts/bash/

Additionally maybe there could be something wrong with:
os.system('sudo /home/pi/PiBits/ServoBlaster/user/servod')

It might require user input like password??
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

Thanks for your help, i will try it tomorow,
I am at a party now 😃
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

hi jvz
script:///home/domoticz/scripts/servoscript.sh
Gives this in the domoticz log:
Error: Error executing script command (/usr/bin/python3). returned: -1

and in /var/tmp/test.log i get nothing

i just dont get it if i ssh into the pi and execute the script it works perfectly
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

hi jvdz

i did what you asked, i get no results in the log file with the on action.
if i ssh in to the pi and execute servoscript.sh there
i get this in var/tmp/test.log:

Board model: 1
GPIO configuration: P1 (40 pins)
Using hardware: PWM
Using DMA channel: 14
Idle timeout: Disabled
Number of servos: 8
Servo cycle time: 20000us
Pulse increment step size: 10us
Minimum width value: 50 (500us)
Maximum width value: 250 (2500us)
Output levels: Normal

Using P1 pins: 7,11,12,13,15,16,18,22

Servo mapping:
0 on P1-7 GPIO-4
1 on P1-11 GPIO-17
sorry if i dont understand you but iam new to linux and scripting
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: python script to lua

Post by jvdz »

edwin1234 wrote: Sunday 10 December 2017 7:14 Error: Error executing script command (/usr/bin/python3). returned: -1
Are you sure you did put this in there: "script:///home/domoticz/scripts/servoscript.sh"
..and not "script:///usr/bin/python3 /home/domoticz/scripts/python/servoscript.py " like @febalci suggested?

Jos
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: python script to lua

Post by jvdz »

Another thing to check is whether the permissions are set correctly for both files!

Jos
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

no with youre sugestion i get this:
10:18:35.495 Error: Error executing script command (/home/pi/domoticz/scripts/servoscript.sh). returned: -1
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

must i do chmod +x for both files or
sudo chmod +x for both files?
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: python script to lua

Post by jvdz »

The "sudo chmod +x " will only set the execute flag.
To allow everybody access you could do : "sudo chmod 777" for both files.

Jos
edwin1234
Posts: 330
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: python script to lua

Post by edwin1234 »

i did sudo chmod 777 on both files
but still the same error in the domoticz log
Error: Error executing script command (/home/pi/domoticz/scripts/servoscript.sh). returned: -1
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest