Can't find what is wrong with this LUA Topic is solved

Moderator: leecollings

Post Reply
void
Posts: 26
Joined: Wednesday 04 November 2015 10:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Can't find what is wrong with this LUA

Post by void »

Hi all,

I've tried to make an event/lua which sends out a Telegram message to either my own alarm group which only contains some family members or to a different Telegram group which also contains neighbours, depending on the parameter given to the bash script (based on the presence of a resident).
The bash script is working fine, but I can't get the LUA part working.
The error in the log is:

Code: Select all

2017-11-04 11:05:39.127 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_SmokeAlarm.lua: /home/pi/domoticz/scripts/lua/script_device_SmokeAlarm.lua:8: syntax error near 'then' 
The LUA code is:

Code: Select all

print('Smoke alarm! Sending out alerts.')

commandArray = {}
        if (devicechanged['Smokealarm'] == 'Panic' and otherdevices['Phone1'] == 'Off' and otherdevices['Phone2'] == 'Off') then
                print("Smoke alarm and nobody is home, sending to neighbours group")
                os.execute("bash /home/pi/domoticz/scripts/SmokeAlarm neighbours")
        else (devicechanged['Smokealarm'] == 'Panic' and otherdevices['Phone1'] == 'On' or otherdevices['Phone2'] == 'On') then
                print("Smoke alarm but someone is home, only sending to own group")
                -- commandArray['MyOtherDeviceName']='On'
                os.execute("bash /home/pi/domoticz/scripts/SmokeAlarm alarm")
        end
return commandArray
The 'else' seems to fail, and I'm not even sure LUA has an 'else' function, so there's that :D
Any pointers would be much appriciated, or other clever ways to send a different parameter depending on a presence.

Cheers,
/void

PS: The bash script, in case anyone is curious:

Code: Select all

#!/bin/sh
date=`/bin/date '+%d-%m-%Y'`;
time=`/bin/date '+%H-%M-%S'`;
today=`/bin/date '+%d-%m-%Y %H:%M:%S'`;
secstat=`curl -s -i -H "Accept: application/json" "http://192.168.x.y:8080/json.htm?type=command&param=getsecstatus" | grep -oP '(?<="secstatus" :).*(?=,)'`

if [ "$1" = neighbour ] ; then
        /usr/bin/curl --data chat_id=-<NEIGHBOURGROUP> --data-urlencode "text=$today Smoke alarm at <MYNAME>!"  "https://api.telegram.org/bot<ID>:<APIKEY>/sendMessage"
        exit
elif [ "$1" = alarm ] ; then
        /usr/bin/curl --data chat_id=-<PERSONALGROUP> --data-urlencode "text=$today Smoke alarm!"  "https://api.telegram.org/bot<ID>:<APIKEY>/sendMessage"
        exit
else
        echo 'No parameter given, please either use "neighbour" to send to the Alarm <NEIGHBOURGROUP> Telegram group or "alarm" to send to own Telegram group.'
        exit
fi
User avatar
jvdz
Posts: 2266
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Can't find what is wrong with this LUA

Post by jvdz »

The Else line should not end with a Then keyword, only If and ElseIf lines do.
It looks like you want that line to be an ElseIf instead of Else which should fix it.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
void
Posts: 26
Joined: Wednesday 04 November 2015 10:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Can't find what is wrong with this LUA

Post by void »

Thanks Jos! ElseIf did the trick, atleast for the error!

Now to fix why it triggers on a random device switch, such as turning on a lightswitch:

Code: Select all

2017-11-04 12:02:44.300 User: Admin initiated a switch command (3/Lamp_3/Off) 
2017-11-04 12:02:44.348 LUA: Smoke alarm! Sending out alerts. 
2017-11-04 12:02:44.348 LUA: Smoke alarm but someone is home, only sending to own group 
2017-11-04 12:02:44.409 (RFXCOM) Lighting 2 (Lamp_3)  
Cheers
edit: added full log.
edit 2: It seems to only print in log, but not actually run?! Why would the 'print' part be parsed but not the actual action in the same 'then' statement?
User avatar
jvdz
Posts: 2266
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Can't find what is wrong with this LUA

Post by jvdz »

Shouldn't those line be like this (also added & so it shells the bash and returns right away without waiting for it to finish)

Code: Select all

print('Smoke alarm! Sending out alerts.')

commandArray = {}
        if (devicechanged['Smokealarm'] == 'Panic' and otherdevices['Phone1'] == 'Off' and otherdevices['Phone2'] == 'Off') then
                print("Smoke alarm and nobody is home, sending to neighbours group")
                os.execute("sh /home/pi/domoticz/scripts/SmokeAlarm neighbours.sh &")
        elseif (devicechanged['Smokealarm'] == 'Panic' and otherdevices['Phone1'] == 'On' or otherdevices['Phone2'] == 'On') then
                print("Smoke alarm but someone is home, only sending to own group")
                -- commandArray['MyOtherDeviceName']='On'
                os.execute("sh /home/pi/domoticz/scripts/SmokeAlarm alarm.sh &")
        end
return commandArray
You also did set the X bit for the file?

Jos

ps: Why are you also retrieving the Secpanel status in the bash script as you don't seem to use it?
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
void
Posts: 26
Joined: Wednesday 04 November 2015 10:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Can't find what is wrong with this LUA

Post by void »

Ah damnit, you're right! I forgot the .sh behind the script name!
Changing bash to sh isn't needed, neither is the & but I do get what you try to accomplish with that.

However, this is actually making it worse. Why is the script reacting on a lightswitch while the devicechanged is defined as 'Smokealarm' ?
Every devicechange (light, movement, doorcontact) triggers this script while it only should react to the Smokesensor device.
Should I be using the IDX instead of a device name, or am I missing something else?

Cheers
User avatar
jvdz
Posts: 2266
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Can't find what is wrong with this LUA

Post by jvdz »

The issue is likely the or's in the elseif. Either use more () around your tests to ensure proper functioning of the logic:

Code: Select all

        elseif (devicechanged['Smokealarm'] == 'Panic' and (otherdevices['Phone1'] == 'On' or otherdevices['Phone2'] == 'On')) then
or split it up like this:

Code: Select all

print('Smoke alarm! Sending out alerts.')

commandArray = {}
    if devicechanged['Smokealarm'] == 'Panic' then
        if otherdevices['Phone1'] == 'Off' and otherdevices['Phone2'] == 'Off' then
            print("Smoke alarm and nobody is home, sending to neighbours group")
            os.execute("sh /home/pi/domoticz/scripts/SmokeAlarm neighbours.sh &")
        else
            print("Smoke alarm but someone is home, only sending to own group")
            -- commandArray['MyOtherDeviceName']='On'
            os.execute("sh /home/pi/domoticz/scripts/SmokeAlarm alarm.sh &")
        end
    end
return commandArray
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
void
Posts: 26
Joined: Wednesday 04 November 2015 10:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Can't find what is wrong with this LUA

Post by void »

You dear sir, are a hero.
The double if statement makes it a lot more easy to read as well.

Thanks, you made my street a bit safer :D
User avatar
jvdz
Posts: 2266
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Can't find what is wrong with this LUA

Post by jvdz »

You're welcome. ;)

by the way: the only reason for the & in the commandline is to avoid blocking the domoticz event system as it is single threaded. It is better to return right away, since there is no need to wait for the telegram script to finish, which keeps the event system responsive.

enjoy,
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest