LUA, What is wrong in my code

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

LUA, What is wrong in my code

Post 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
jannl
Posts: 825
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.5.x
Location: Geleen
Contact:

Re: LUA, What is wrong in my code

Post 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...
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: LUA, What is wrong in my code

Post 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?
jannl
Posts: 825
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.5.x
Location: Geleen
Contact:

Re: LUA, What is wrong in my code

Post 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.
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

LUA, What is wrong in my code

Post 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
jannl
Posts: 825
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.5.x
Location: Geleen
Contact:

Re: LUA, What is wrong in my code

Post 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?
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: LUA, What is wrong in my code

Post 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.
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: LUA, What is wrong in my code

Post 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
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: LUA, What is wrong in my code

Post 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
jannl
Posts: 825
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.5.x
Location: Geleen
Contact:

Re: LUA, What is wrong in my code

Post by jannl »

Ok, did not read the script that carefully. How long does it take commandline before the ping fails?
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: LUA, What is wrong in my code

Post 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
jannl
Posts: 825
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.5.x
Location: Geleen
Contact:

Re: LUA, What is wrong in my code

Post by jannl »

What I mean is if you type 'ping (ip address)' and the ping fails, how long does it take?
User avatar
jvdz
Posts: 2445
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA, What is wrong in my code

Post 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
Last edited by jvdz on Saturday 25 June 2016 15:45, edited 2 times in total.
jannl
Posts: 825
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.5.x
Location: Geleen
Contact:

Re: LUA, What is wrong in my code

Post by jannl »

Yeah, same here
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: LUA, What is wrong in my code

Post 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?
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: LUA, What is wrong in my code

Post 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
jannl
Posts: 825
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.5.x
Location: Geleen
Contact:

Re: LUA, What is wrong in my code

Post by jannl »

No idea what I am seeing there.
Please use the same command you typed in your code.
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: LUA, What is wrong in my code

Post 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
jannl
Posts: 825
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.5.x
Location: Geleen
Contact:

Re: LUA, What is wrong in my code

Post by jannl »

What ip address exactly are you pinging?
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

LUA, What is wrong in my code

Post by Flopp »

ADMIN please remove this post
Last edited by Flopp on Monday 27 June 2016 11:20, edited 2 times in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest