Page 1 of 1

LUA Error: 'then' expected near '1'

Posted: Sunday 20 May 2018 20:32
by krazny
I am trying to send myself some messages through Telegram with help of a LUA script.
This is working great through a BASH script, but I am trying to get it to work through a LUA script.

Unfortunately I keep getting the error " 'then' expected near '1' " , but I do not know why.
The error is on line "20" ; if ping -c 1 $IPcam > /dev/null; then

Is there someone who can help me?

The script:

Code: Select all

--!/bin/sh
--=================================================================
--======================NETWORK SETTINGS==========================
IPcam="192.168.1.1"                                                        --IP address Camera
--=================================================================
--=====================TELEGRAM SETTINGS==========================
token="123456789:AABBcceeff87XkcggdHHCUiimJJ123q4567"   --Token ID
chatid="91198712"	                                                               --Chat ID
WebcamMessage="Webcam not online"                                      --Message
AlertMessage="Movement in Garden?!"                                       --Message
--=================================================================
commandArray={}

if  otherdevices['Sun'] == 'Off' and
    devicechanged['Sensor_Tuin'] == 'On' then
     
	print('Motion detected in garden. Sending Message')
        
    if ping -c 1 $IPcam > /dev/null ;  then  
       --Send message through Telegram if PING successfull:
       os.execute('curl "https://api.telegram.org/bot'..token..'/sendMessage?chat_id='..chatid..'&text='..AlertMessage..'" ')
       --Send snapshot with Telegram when ping successful:
       os.execute('wget http://127.0.0.1:8080/camsnapshot.jpg?idx=1 && sudo mv camsnapshot.jpg?idx=1 /var/tmp/snapshot.jpg')
   
       --Wait for 5 seconds to be sure the snapshot has been copied
       sleep 5
       os.execute('curl -s -X POST "https://api.telegram.org/bot'..token..'/sendPhoto?chat_id='..chatid..'photo="@/var/tmp/snapshot.jpg"')

       --Wait for 1 second before removing the snapshot
       sleep 1
       os.execute('/bin/rm /var/tmp/snapshot.jpg')
  
     else 
         os.execute('curl "https://api.telegram.org/bot'..token..'/sendMessage?chat_id='..chatid..'&text='..WebcamMessage..'" ')
   end 
end

return commandArray
It seems that I need to 'tell' domoticz it is a regulare commandline, but how? changing it to; os.execute('CommandLineHere') doesn't work in this case.

I am stuck... :roll:

Re: LUA Error: 'then' expected near '1'

Posted: Sunday 20 May 2018 20:55
by jvdz
This line is wrong:

Code: Select all

	if ping - c 1 $IPcam > /dev / null ; then
and probably needs to be something like:

Code: Select all

	if os.execute('ping - c 1 $IPcam')  then
Jos

Re: LUA Error: 'then' expected near '1'

Posted: Sunday 20 May 2018 21:09
by waaren
krazny wrote: Sunday 20 May 2018 20:32 I am trying to send myself some messages through Telegram with help of a LUA script.
This is working great through a BASH script, but I am trying to get it to work through a LUA script.

Unfortunately I keep getting the error " 'then' expected near '1' " , but I do not know why.
I am stuck... :roll:
for what I can see you are mixing Bash and Lua commands.

Standard Lua does not have a sleep function and the following line is not Lua

Code: Select all

if ping -c 1 $IPcam > /dev/null ;  then 

Re: LUA Error: 'then' expected near '1'

Posted: Sunday 20 May 2018 21:17
by jvdz
Good observation @waaren !
@krazny, You do not want to use put any pause in your script since these are ran by the event system and you will pause the whole event system meaning it will become totally unresponsive during the time it takes that each script is ran.
Why are you trying to convert this script anyways into an LUA script as part of the event system?
You could just do only the os.execute() you already have in there: os.execute('/home/pi/domoticz/scripts/telegram_send_snapshot.sh') but change it to : os.execute('/home/pi/domoticz/scripts/telegram_send_snapshot.sh &')
That & at the end will ensure the script is ran but the control is given back to the script immediately.

Jos

Re: LUA Error: 'then' expected near '1'

Posted: Sunday 20 May 2018 21:22
by krazny
jvdz wrote: Sunday 20 May 2018 21:17 Good observation @waaren !
@krazny, You do not want to use put any pause in your script since these are ran by the event system and you will pause the whole event system meaning it will become totally unresponsive during the time it takes that each script is ran.
Why are you trying to convert this script anyways into an LUA script as part of the event system?
You could just do only the os.execute() you already have in there: os.execute('/home/pi/domoticz/scripts/telegram_send_snapshot.sh') but change it to : os.execute('/home/pi/domoticz/scripts/telegram_send_snapshot.sh &')
That & at the end will ensure the script is ran but the control is given back to the script immediately.

Jos
Good point.
I now had the work-around to just execute the script if both the switches are ON.
Forgot to remove it for this example. Srry :)

Like I said;
it is all working OK through the BASH script, but I thought it would be easier if all my scripts are LUA; saved in Domoticz database.

*removed the line which calls the bash script in my original post btw ;)