LUA Error: 'then' expected near '1'

Moderator: leecollings

Post Reply
User avatar
krazny
Posts: 27
Joined: Saturday 01 November 2014 23:31
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Netherlands
Contact:

LUA Error: 'then' expected near '1'

Post 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:
Last edited by krazny on Sunday 20 May 2018 21:23, edited 1 time in total.
Raspberry Pi, Aeon Z-Stick, Fibaro Motion Sensor, 4x Fibaro Wall Plug, 2x Fibaro Dimmer, Netatmo Weatherstation
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
krazny
Posts: 27
Joined: Saturday 01 November 2014 23:31
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Netherlands
Contact:

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

Post 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 ;)
Raspberry Pi, Aeon Z-Stick, Fibaro Motion Sensor, 4x Fibaro Wall Plug, 2x Fibaro Dimmer, Netatmo Weatherstation
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest