This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Counter for a script.
#1
Hi there

For the past two hours I've been trying to declare a variable that would count how many times the LogiMachine has not reached the Internet.

If I don't declare the variable PingFailure, I get to following error message:

Resident script:17: attempt to perform arithmetic on local 'PingFailure' (a nil value)

If I do declare it, it never counts above 1 as it resets itself each time I carry out the script.  How is this done, I've searched this forum and the Internet?



function ping(ip)
  local res = os.execute('ping -c 2 -W 5 ' .. ip)
  return res == 0
end

local PingFailure = 0

ping = ping('google.com')

if ping then
  log('INTERNET connected')
  log(ping)
 
else
  log('INTERNET disconnected')
  log(ping)
  PingFailure = PingFailure + 1
end 

log(PingFailure)


I look forward to hearing from you.

Cheers!
Martin
Reply
#2
(11.08.2020, 11:46)kropfm Wrote: Hi there

For the past two hours I've been trying to declare a variable that would count how many times the LogiMachine has not reached the Internet.

If I don't declare the variable PingFailure, I get to following error message:

Resident script:17: attempt to perform arithmetic on local 'PingFailure' (a nil value)

If I do declare it, it never counts above 1 as it resets itself each time I carry out the script.  How is this done, I've searched this forum and the Internet?



function ping(ip)
  local res = os.execute('ping -c 2 -W 5 ' .. ip)
  return res == 0
end

local PingFailure = 0

ping = ping('google.com')

if ping then
  log('INTERNET connected')
  log(ping)
 
else
  log('INTERNET disconnected')
  log(ping)
  PingFailure = PingFailure + 1
end 

log(PingFailure)


I look forward to hearing from you.

Cheers!
Martin

You should use next construcion:

Code:
if not PingFailure then
  PingFailure = 0
end
In this situation it'll refresh only when you restart script.
Reply
#3
Hi,

You overwrite the function with ping = ping('google.com')

BR,

Erwin
Reply
#4
Try this:
Code:
if not ping then
  function ping(ip)
    local res = os.execute('ping -c 2 -W 5 ' .. ip)
    return res == 0
  end

  failcount = 0
end

res = ping('google.com')

if res then
  log('INTERNET connected')
  failcount = 0
else
  log('INTERNET disconnected')
  failcount = failcount + 1
end

log(failcount)
Reply
#5
(11.08.2020, 12:37)admin Wrote: Thank you Erwin this solves the problem. Looks like I have to do some more reading up on LUA.

Thanks!!

Try this:
Code:
if not ping then
  function ping(ip)
    local res = os.execute('ping -c 2 -W 5 ' .. ip)
    return res == 0
  end

  failcount = 0
end

res = ping('google.com')

if res then
  log('INTERNET connected')
  failcount = 0
else
  log('INTERNET disconnected')
  failcount = failcount + 1
end

log(failcount)
Reply
#6
Admin ~= Erwin so you need to thank Admin not me  Big Grin
Reply
#7
thanks Admin. Looks like I'm still a bit challenged with the forum;-)
Reply


Forum Jump: