Problem with lua and os.execute Topic is solved

Moderator: leecollings

Post Reply
moengiant
Posts: 28
Joined: Monday 25 January 2016 6:33
Target OS: Windows
Domoticz version:
Contact:

Problem with lua and os.execute

Post by moengiant »

Not sure what the problem is so I'm reaching out to the forum in hope of a possible solution. Basically If I use an os.execute statement in a time lua script it runs with no issues however if I use the same statement in an on or off trigger script i get no joy.
What I am trying to do is execute a python script from a lua script. Seems straight forward and cut and dry but I still have not figured out why it will not run.

Here is the lua script - triggered by the off action on an on/off switch - and how its executed in the device - Off Action: script://scripts\lus\stopcast.lua

Code: Select all

print("Stoping Chromecast ")

-- this is the line not executing
os.execute('start pythonw "C:\\Program Files (x86)\\Domoticz\\scripts\\python\\stop_casting.pyw"')

Here is the python code for - stop_casting.pyw. Its a script to force a Chromecast device to disconnect -Spotify constantly hanging

Code: Select all

import pychromecast
import urllib
import json
sun = urllib.request.urlopen("http://192.168.1.2:8080/json.htm?type=devices&rid=86")
data = sun.read().decode('utf-8')
parts = json.loads(data)
nord = parts['result'][0]['Data']
print(nord)
chromecasts = pychromecast.get_chromecasts()
cast = next(cc for cc in chromecasts if cc.device.friendly_name == "Den TV")
cast.wait()
status = cast.status.status_text
if (status == "Netflix" or status == "YouTube") and nord == "night":
    data = urllib.request.urlopen("http://192.168.1.2:8080/json.htm?type=command&param=switchlight&idx=86&switchcmd=Off")
    data = urllib.request.urlopen("http://192.168.1.2:8080/json.htm?type=command&param=switchscene&idx=3&switchcmd=On")
log = urllib.request.urlopen("http://192.168.1.2:8080/json.htm?type=command&param=addlogmessage&message=Disconnected+Den+TV+Chromecast")
cast.quit_app()
quit()


Now if I rename the lue script to script_time_stopcast.lua - everything works as expected - every minute if casting to the device - its disconnected.

Things I have tried with no luck:
Run the python from the off action - nothing
Run a python script using a subprocess - thinking the script was taking to long. - nothing
Run a lua script using an io.popen statement:

Code: Select all

cmd ='cmd /C python "C:\\Program Files (x86)\\Domoticz\\scripts\\python\\stopCast.pyw"
local f = assert(io.popen(cmd, 'r'))'
What I'd like to fix if possible the ability to use os.execute for non device or time scripts as its very handy - when it works as expected.

Running Domoticz: 2020-2, Windows 10 and python 3.8.2

Any help, insight or shared experience - confirmation of this being a problem - would be greatly appreciated.
I'd rather have a bottle in front of me than a frontal lobotomy
moengiant
Posts: 28
Joined: Monday 25 January 2016 6:33
Target OS: Windows
Domoticz version:
Contact:

Re: Problem with lua and os.execute

Post by moengiant »

So it seems that this is a permissions issue - I signed out and signed in as a different user then signed out and signed back in as me and now all of a sudden all is working. Curious if it was a Window's update that needed to be completely installed. The good news is its fixed bad news is don't know what broke it in the first place.
I'd rather have a bottle in front of me than a frontal lobotomy
moengiant
Posts: 28
Joined: Monday 25 January 2016 6:33
Target OS: Windows
Domoticz version:
Contact:

Re: Problem with lua and os.execute

Post by moengiant »

OK so I figured out what the problem was with my python scripts and more importunately I fixed the issue and now python and Domoticz play nice. More or less a permission issue. Domoticz was running as a service using a local system account as such python was not able to execute (python needs administrator permissions). I fixed the issue by removing the Domoticz service in nssm (non sucking service manager). Did this by starting cmd as admin -> then CD'ed to Domoticz directory -> and then typed "nssm remove domoticz." Warning as this will remove Domoticz starting up as a service. Now
to get Domoticz to launch on startup I went into startup and added a Domoticz shortcut using shell:startup. Then I right clicked on the shortcut and selected properties -> then Advanced and checked "Run as administrator." As well added arguments to the target of the shortcut to read "C:\Program Files (x86)\Domoticz\domoticz.exe" -www XXXX -sslwww XXX - (XXXX = port number for web and XXX port for ssl). Now my Domoticz runs as administrator and my python scripts are now working again when triggered by an on or off action. Hope this helps anyone running Domoticz on WIndows and having issues with python not running.
I'd rather have a bottle in front of me than a frontal lobotomy
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest