Page 1 of 1

Letting LUA wait for result from Python script?

Posted: Monday 12 February 2018 14:15
by quack3d
I made this script in LUA that runs a python script that updates the value of a dummy info device in Domoticz. Then the LUA script sends out a Pushbullet notification with the value of the dummy device.

But what's a good way of making the LUA script wait for the result of the python script robotdock.pyw? The way it is now I have to run the script twice to get the real value of the dummy info device since it doesn't update fast enough for the first run.

Code: Select all

local switch_1 = 'Robot - check dock'
local text_sensor = 'Is robot in dock??'
local title = 'Robot dock status'
local robot_path = 'C:\\Program Files (x86)\\Domoticz\\scripts\\python\\robotdock.pyw'
local push_path = 'C:\\Program Files (x86)\\Domoticz\\scripts\\batch\\pushbullet.bat'

 commandArray = {}
  if (otherdevices[switch_1] == 'On')
  then
    commandArray[switch_1] = 'Off'
    print('"'..robot_path..'"')
    os.execute('"'..robot_path..'"')
    local message = otherdevices[text_sensor]
    local tit_msg = title..'"' .. ' "' ..message
    local combined = '""'..push_path..'" "'..tit_msg..'""'
    print(combined)
    os.execute(combined)
  end
return commandArray

Re: Letting LUA wait for result from Python script?

Posted: Monday 12 February 2018 14:32
by emme
the problem you have is that the otherdevice table contains info at the moment the script is fired...
even if the pythonscript works fine, you have to file the script twice

a possible good way to handle could be using dzVents with 2 different triggers:
the first (that launch the python script) triggered by whatever triggers now the script
the second, generated by the update of the text device that sends you the notification

ciao
M

Re: Letting LUA wait for result from Python script?

Posted: Monday 12 February 2018 20:58
by quack3d
Can you show me how this is done?

Re: Letting LUA wait for result from Python script?

Posted: Tuesday 13 February 2018 8:25
by emme
if I well undoestood what it is supposed to do.... this could be a way:

Code: Select all

local switch_1 = 'Robot - check dock'
local text_sensor = 'Is robot in dock??'
local title = 'Robot dock status'
local robot_path = 'C:\\Program Files (x86)\\Domoticz\\scripts\\python\\robotdock.pyw'
local push_path = 'C:\\Program Files (x86)\\Domoticz\\scripts\\batch\\pushbullet.bat'


return {
	on = {
		devices = { switch_1, text_sensor },
	},

	execute = function(dz, dev)
        if dev.name == switch_1 then        -- triggered by Switch, running the robot!!
            os.execute('"'..robot_path..'"')
            dev.switchOff()
            dz.log('Running the robot!!')
        elseif dev.name == text_sensor then -- Robot has finish and the text sensor has been updated
            message = dz.devices(text_sensor).text
            tit_msg = title..'"' .. ' "' ..message
            combined = '""'..push_path..'" "'..tit_msg..'""'
            dz.log(combined)
            os.execute(combined)
        end
    end
}

Re: Letting LUA wait for result from Python script?

Posted: Tuesday 13 February 2018 12:26
by quack3d
It almost works. I get push notification every second even though I've set the switch Off delay to 1.

Re: Letting LUA wait for result from Python script?

Posted: Tuesday 13 February 2018 20:43
by quack3d
I think I understand why. When setting the dev.switchOff() in the script it automatically triggers a new change and running the script over and over. If I uncomment that line and keep Off delay to 0, it works. However, I would still like the device to be set to Off without triggering a new run of the script. Any ideas?

EDIT: The following change fixed it. Thanks a bunch.

Code: Select all

if dev.name == switch and dev.state == 'On' then 

Re: Letting LUA wait for result from Python script?

Posted: Thursday 15 February 2018 12:33
by dannybloe
Or use dev.switchOff().silent() and perhaps with .checkFirst().