Logic Machine Forum
Counter for a script. - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Counter for a script. (/showthread.php?tid=2787)



Counter for a script. - kropfm - 11.08.2020

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


RE: Counter for a script. - Snoolik - 11.08.2020

(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.


RE: Counter for a script. - Erwin van der Zwart - 11.08.2020

Hi,

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

BR,

Erwin


RE: Counter for a script. - admin - 11.08.2020

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)



RE: Counter for a script. - kropfm - 11.08.2020

(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)



RE: Counter for a script. - Erwin van der Zwart - 11.08.2020

Admin ~= Erwin so you need to thank Admin not me  Big Grin


RE: Counter for a script. - kropfm - 11.08.2020

thanks Admin. Looks like I'm still a bit challenged with the forum;-)