Capture Shell Command Output in Lua script

Moderator: leecollings

Post Reply
hendryman
Posts: 9
Joined: Sunday 22 October 2017 16:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Capture Shell Command Output in Lua script

Post by hendryman »

from a lua script, i want to execute this shell command:

Code: Select all

hueadm lights | grep -q 'on' && echo 'lights_on'
if any of my hue lights is "on", this command will echo "lights_on".

my question: how i verify if "lights_on" was echoed back in lua? eg can os.execute() be used in a way that it captures the executed shell command's output?
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Capture Shell Command Output in Lua script

Post by felix63 »

Would this work? Making a dummy switch and then curl the switch on from your shell script?

Code: Select all

hueadm lights | grep -q 'on' && curl 'http://DOMOTICZ-IP:8080/json.htm?type=command&param=switchlight&idx=SWITCH_ID&switchcmd=On
zicht
Posts: 251
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 2023.1+
Location: NL
Contact:

Re: Capture Shell Command Output in Lua script

Post by zicht »

you could query the log anyway you want by this :

http://xxx.xxx.xxx.xxx:xxxx/json.htm?type=command&param=getlog&lastlogtime=0&loglevel=268435455

This can be done by curl or shell whatever you want the respond will contain the switch name from log, since you invoke the command yourself you can assume the command is received. I use it to detect a reboot, and certain activities. With log lever you can select the details of log.

Hope it helps
Rpi & Win x64. Using : cam's,RFXCom, LaCrosse, RFY, HuE, google, standard Lua, Tasker, Waze traveltime, NLAlert&grip2+,curtains, vacuum, audioreceiver, smart-heating&cooling + many more (= automate all repetitive simple tasks)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Capture Shell Command Output in Lua script

Post by waaren »

hendryman wrote: Monday 15 April 2019 12:38 from a lua script, i want to execute this shell command:

Code: Select all

hueadm lights | grep -q 'on' && echo 'lights_on'
if any of my hue lights is "on", this command will echo "lights_on".

my question: how i verify if "lights_on" was echoed back in lua? eg can os.execute() be used in a way that it captures the executed shell command's output?
The attached function returns both the command output and the return code of the command.

Code: Select all

        local function osExecute(cmd)
            local fileHandle     = assert(io.popen(cmd, 'r'))
            local commandOutput  = assert(fileHandle:read('*a'))
            local returnTable    = {fileHandle:close()}
            return commandOutput,returnTable[3]            -- rc[3] contains returnCode
        end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
hendryman
Posts: 9
Joined: Sunday 22 October 2017 16:20
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Capture Shell Command Output in Lua script

Post by hendryman »

thank you. much appreciated! i'm afraid my lua skills are very limited. could you please give an example on how to call this function and parse its results (three different parameters, if i understand correctly) from the main script? in my case, specifically, i'd like to receive the shell command's "matched" string (or the fact that the shell command did not return a string).
deve87
Posts: 45
Joined: Saturday 08 April 2017 8:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Capture Shell Command Output in Lua script

Post by deve87 »

I actually have the same question as you. I'm not any good in scripting. And was hoping someone know how to help with your/my question.

I got a script with io.popen work, but after like 2min. Domoticz lags bad and got error in log that 'Script has been running for more than 10sec'

I want to get a telnet session in Domoticz. And like I said, got it working but the problem.

Code: Select all

commandArray = {}

local s = os.date('%S')
if (s % 60 == 0) then

local f = assert(io.popen("telnet 192.168.10.110 20108 | strings &" , 'r'))
local Rotel = assert(f:read('*a'))
assert:close()

print(Rotel)
commandArray['UpdateDevice'] = '1376'..'|0|'.. Rotel ..''

end

return commandArray
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Capture Shell Command Output in Lua script

Post by waaren »

deve87 wrote: Monday 22 April 2019 21:40 I actually have the same question as you. I'm not any good in scripting. And was hoping someone know how to help with your/my question.

I got a script with io.popen work, but after like 2min. Domoticz lags bad and got error in log that 'Script has been running for more than 10sec'

I want to get a telnet session in Domoticz. And like I said, got it working but the problem.

Code: Select all

commandArray = {}

local s = os.date('%S')
if (s % 60 == 0) then

local f = assert(io.popen("telnet 192.168.10.110 20108 | strings &" , 'r'))
local Rotel = assert(f:read('*a'))
assert:close()

print(Rotel)
commandArray['UpdateDevice'] = '1376'..'|0|'.. Rotel ..''

end

return commandArray
Not sure what you want to achieve but please understand that the eventsystem in domoticz is single threaded.
So all event triggered scripts will be waiting for this script to finish. The telnet command will likely not finish so that is the reason domoticz complains.
What might be an option here is to enter this commands in a bash script and execute that bash script in the background from Lua .
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Capture Shell Command Output in Lua script

Post by waaren »

hendryman wrote: Tuesday 16 April 2019 9:58 thank you. much appreciated! i'm afraid my lua skills are very limited. could you please give an example on how to call this function and parse its results (three different parameters, if i understand correctly) from the main script? in my case, specifically, i'd like to receive the shell command's "matched" string (or the fact that the shell command did not return a string).
Happy to help if you can share your Lua script where you want to use this function.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Capture Shell Command Output in Lua script

Post by waaren »

hendryman wrote: Tuesday 16 April 2019 9:58 thank you. much appreciated! i'm afraid my lua skills are very limited. could you please give an example on how to call this function and parse its results (three different parameters, if i understand correctly) from the main script? in my case, specifically, i'd like to receive the shell command's "matched" string (or the fact that the shell command did not return a string).
Happy to help if you can share your Lua script where you want to use this function.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
zicht
Posts: 251
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 2023.1+
Location: NL
Contact:

Re: Capture Shell Command Output in Lua script

Post by zicht »

The above should work, but the url you are trying to read can indeed cause delays for many reasons.
You can use curl with the above instructions
Curl alows you to add timeouts

curl --connect-timeout 5 --max-time 9

that will avoid the 10 seconds message, and by tweaking it down to for example

curl --connect-timeout 2 --max-time 2

You have to test what works best for the url you are trying to reach :)

when the timeout is reached you get an empty string in return, but at least domoticz stops waiting so it does not "hang" waiting for your request to end.
Rpi & Win x64. Using : cam's,RFXCom, LaCrosse, RFY, HuE, google, standard Lua, Tasker, Waze traveltime, NLAlert&grip2+,curtains, vacuum, audioreceiver, smart-heating&cooling + many more (= automate all repetitive simple tasks)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest