Letting LUA wait for result from Python script? Topic is solved

Post Reply
quack3d
Posts: 52
Joined: Sunday 26 March 2017 17:03
Target OS: -
Domoticz version:
Contact:

Letting LUA wait for result from Python script?

Post by quack3d » Monday 12 February 2018 15:15

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

User avatar
emme
Posts: 782
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Letting LUA wait for result from Python script?

Post by emme » Monday 12 February 2018 15:32

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
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

quack3d
Posts: 52
Joined: Sunday 26 March 2017 17:03
Target OS: -
Domoticz version:
Contact:

Re: Letting LUA wait for result from Python script?

Post by quack3d » Monday 12 February 2018 21:58

Can you show me how this is done?

User avatar
emme
Posts: 782
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Letting LUA wait for result from Python script?

Post by emme » Tuesday 13 February 2018 9:25

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
}
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

quack3d
Posts: 52
Joined: Sunday 26 March 2017 17:03
Target OS: -
Domoticz version:
Contact:

Re: Letting LUA wait for result from Python script?

Post by quack3d » Tuesday 13 February 2018 13:26

It almost works. I get push notification every second even though I've set the switch Off delay to 1.

quack3d
Posts: 52
Joined: Sunday 26 March 2017 17:03
Target OS: -
Domoticz version:
Contact:

Re: Letting LUA wait for result from Python script?

Post by quack3d » Tuesday 13 February 2018 21:43

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 

dannybloe
Posts: 1269
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Letting LUA wait for result from Python script?

Post by dannybloe » Thursday 15 February 2018 13:33

Or use dev.switchOff().silent() and perhaps with .checkFirst().
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests