Page 1 of 2

LUA, What is wrong in my code

Posted: Friday 24 June 2016 10:26
by Flopp
I get this message in Log

Code: Select all

2016-06-24 10:20:10.245 Error: EventSystem: Warning!, lua script larmstatus has been running for more than 10 seconds
2016-06-24 10:20:10.265 LUA: Ping failed
What I want to do is to Ping the Pi that handle the CGI and if Ping is not successful I want the script to exit.

This is my script

Code: Select all

commandArray = {}
ping_success=os.execute('ping -c1 192.168.xxx.xxx')
if ping_success then
    print("ping success")
    time  = os.date("%Y-%m-%d %H:%M:%S")
    json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
    file = io.open("larm.txt", "a")
    file:write(time.."\n")
    local visonic=assert(io.popen('curl http://192.168.xxx.xxx/cgi-bin/jsongetxplstate.cgi'))
    file:write("curl ok\n")
    local status = visonic:read('*all')
    visonic:close()
    if status == "" then
        --print("h")
        file:write("empty\n")
        file:write(status.."\n")
    
    else
        --print (status)
        file:write(status.."\n")    
        local jsonStatus = json:decode(status)
        print (jsonStatus)
        --file:write(jsonStatus.."\n")    
        powermaxstatus = jsonStatus['pmaxstatus']
        --print(powermaxstatus)
        --print(otherdevices['Visonic Alarm Status'])

        if (powermaxstatus == "disarmed" or powermaxstatus == "armed" or powermaxstatus == "armed-home" or powermaxstatus == "armed-away") then
            
            if powermaxstatus == "disarmed" and otherdevices['Visonic Alarm Status'] == "On" then
                print('<font color="blue">Visonic: Larmat av</font>')
                commandArray['Visonic Alarm Status'] = "Off"
                commandArray[1]={['SendEmail']='Avlarmat# #[email protected]'}
                --commandArray[2]={['SendEmail']='Avlarmat# #[email protected]'}
            end   
            
            if (powermaxstatus == "armed" or powermaxstatus == "armed-home" or powermaxstatus == "armed-away") and otherdevices['Visonic Alarm Status'] == "Off"  then
            print('<font color="red">Visonic: Larmat på</font>')
            commandArray['Visonic Alarm Status']='On'
            commandArray[1]={['SendEmail']='Pålarmat# #[email protected]'}
            --commandArray[2]={['SendEmail']='Pålarmat# #[email protected]'}
            end
        end
    end

    file:close()
else
    print('<font color="red">Ping failed</font>')
end

return commandArray

Re: LUA, What is wrong in my code

Posted: Friday 24 June 2016 10:40
by jannl
Seems the script runs too long. You could add a timeout to the ping. No need to wait several seconds for a ping reply that will not come. Ping in local network is like milliseconds...

Re: LUA, What is wrong in my code

Posted: Friday 24 June 2016 10:45
by Flopp
Yes it runs more than 10 seconds but a Ping should be maximum 1 second according to Domoticz wiki.
How do I set maximum 500 millisecond to a Ping?

Re: LUA, What is wrong in my code

Posted: Friday 24 June 2016 11:24
by jannl
Depends on your os. In windows it seems default ping timeout is 4secs. Please use google to find the ping settings for your os.

LUA, What is wrong in my code

Posted: Saturday 25 June 2016 10:14
by Flopp
I am using RPi.

It works 99 out of 100 times but I want to stop the script if the ping fails which is only 1 out if 100.

What I don't still understand is why the script runs for 10 seconds and then return Ping fail.
It should return Ping fail and then exit script.
So it seems to be that the script runs 10 seconds then ping. Strange

Re: LUA, What is wrong in my code

Posted: Saturday 25 June 2016 10:40
by jannl
If you do a ping from command line to a non-existing local ip-address, how long does it take?
You also do some file writes, not sure how long they take. Why not create a perl/python/badh script for this?

Re: LUA, What is wrong in my code

Posted: Saturday 25 June 2016 11:07
by Flopp
jannl wrote:If you do a ping from command line to a non-existing local ip-address, how long does it take?
It takes around 2 seconds.

Re: LUA, What is wrong in my code

Posted: Saturday 25 June 2016 11:08
by Flopp
jannl wrote: You also do some file writes, not sure how long they take. Why not create a perl/python/badh script for this?
I only write if the ping success otherwise it will/shall exit the script

Thank you for all help so far

Re: LUA, What is wrong in my code

Posted: Saturday 25 June 2016 11:10
by Flopp
jannl wrote:Why not create a perl/python/badh script for this?
I will test with a bash and see if that also gets error maybe the device that I ping is answering but takes to much time

Re: LUA, What is wrong in my code

Posted: Saturday 25 June 2016 11:11
by jannl
Ok, did not read the script that carefully. How long does it take commandline before the ping fails?

Re: LUA, What is wrong in my code

Posted: Saturday 25 June 2016 11:13
by Flopp
jannl wrote:Ok, did not read the script that carefully. How long does it take commandline before the ping fails?
Don't understand the question.
Do you mean before it goes to ELSE if it fails? Then it is 1-2 seconds, same as the test I did manually

Re: LUA, What is wrong in my code

Posted: Saturday 25 June 2016 11:32
by jannl
What I mean is if you type 'ping (ip address)' and the ping fails, how long does it take?

Re: LUA, What is wrong in my code

Posted: Saturday 25 June 2016 12:04
by jvdz
I think this is more a generic issue with the build in event system. I remember having looked at this a while ago and know that some scripts that contain shelling another program and capturing its output, simply hangs for no reason when shelled by the Event system.
In general I always schedule these type of check scripts anyway via a cron job as it will else slow down the event system since it is single threaded.

Jos

Re: LUA, What is wrong in my code

Posted: Saturday 25 June 2016 14:30
by jannl
Yeah, same here

Re: LUA, What is wrong in my code

Posted: Monday 27 June 2016 0:07
by Flopp
Is this true?
Lua is not the best place to run scripts which are purely inserting a delay, as Domoticz can only run 1 Lua script at a time, so while the delaying script is running nothing else will.
viewtopic.php?p=18984#p18984

is Yes, will the second, third.... LUA script run after the first is ready?

Re: LUA, What is wrong in my code

Posted: Monday 27 June 2016 0:15
by Flopp
jannl wrote:What I mean is if you type 'ping (ip address)' and the ping fails, how long does it take?
Sorry for late answer but I didn't know how to timestamp PING now I know :)

This is my replay when fail

[1466979064.011055] From 192.168.x.x icmp_seq=1 Destination Host Unreachable
[1466979064.011917] From 192.168.x.x icmp_seq=2 Destination Host Unreachable
[1466979064.012433] From 192.168.x.x icmp_seq=3 Destination Host Unreachable
[1466979067.011018] From 192.168.x.x icmp_seq=4 Destination Host Unreachable
[1466979067.011728] From 192.168.x.x icmp_seq=5 Destination Host Unreachable
[1466979067.012290] From 192.168.x.x icmp_seq=6 Destination Host Unreachable

Re: LUA, What is wrong in my code

Posted: Monday 27 June 2016 10:14
by jannl
No idea what I am seeing there.
Please use the same command you typed in your code.

Re: LUA, What is wrong in my code

Posted: Monday 27 June 2016 10:37
by Flopp
jannl wrote:No idea what I am seeing there.
Please use the same command you typed in your code.
It is seconds since 1970.
[1466979064.011055]
between pings it is 0.0009 seconds
[1466979064.011917]
0.0005 seconds
[1466979064.012433]

It seems that Ping do 3 attempts and then wait 3 seconds and do 3 again.

I think I will solve it by using Cron.

Thanks for your help

Re: LUA, What is wrong in my code

Posted: Monday 27 June 2016 11:14
by jannl
What ip address exactly are you pinging?

LUA, What is wrong in my code

Posted: Monday 27 June 2016 11:19
by Flopp
ADMIN please remove this post