what am I doing wrong?? I can't get my head round it!!

Moderator: leecollings

Post Reply
chrisfraser05
Posts: 32
Joined: Sunday 08 January 2017 18:59
Target OS: Windows
Domoticz version:
Contact:

what am I doing wrong?? I can't get my head round it!!

Post by chrisfraser05 »

I have a very simple script that works to ping my phone then turn on and off a virtual device.

This was no problem.

What I then went on to do was add a user variable, and try to make a counter.

I want to reset the counter to 0 when it pings correctly, and each time it fails +1 until it hits 3, where it turns off the virtual device.

As the script runs every minute this gives a bit of a debounce.

Can anyone tell me what I've done wrong here, and why it won't work?

Thanks

Code: Select all

commandArray = {}
 
local device
local chrisvar

 device = 'Phone Chris'
 chrisvar = (uservariable['chrisVar'])

ping_success=os.execute('ping 192.168.1.72 -n 1 -w 100>nul')

 if ping_success then
 --  print("ping success - " ..device)
   commandArray[device]='On'
   chrisvar = 0
 else
     
   print("ping fail - " ..device)
   chrisvar = chrisvar + 1
   
  if (chrisvar == 3) then
   commandArray[device]='Off'	
   end
 end
 
return commandArray
chrisfraser05
Posts: 32
Joined: Sunday 08 January 2017 18:59
Target OS: Windows
Domoticz version:
Contact:

Re: what am I doing wrong?? I can't get my head round it!!

Post by chrisfraser05 »

This is the working version without the couter

Code: Select all

commandArray = {}
 
local device

 device = 'Phone Chris'
 
ping_success=os.execute('ping 192.168.1.72 -n 1 -w 100>nul')

 if ping_success then
--   print("ping success - " ..device)
   commandArray[device]='On'
 else
   print("ping fail - " ..device)
   commandArray[device]='Off'	
 end
 
return commandArray
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: what am I doing wrong?? I can't get my head round it!!

Post by Nautilus »

change this:

Code: Select all

chrisvar = (uservariable['chrisVar'])
to this

Code: Select all

chrisvar = tonumber(uservariables['chrisVar'])
Also, you need to update the new uservariable value:

Code: Select all

commandArray['Variable:chrisVar'] = tostring(chrisvar)
The check if chrisvar == 3 should be before "chrisvar = chrisvar + 1" if you want it to be three fails before changing the switch to off. Also consider adding conditions not to update the variable when not needed (= value 0 or 3 ?).
chrisfraser05
Posts: 32
Joined: Sunday 08 January 2017 18:59
Target OS: Windows
Domoticz version:
Contact:

Re: what am I doing wrong?? I can't get my head round it!!

Post by chrisfraser05 »

Thanks very much for your help.

I'd tried most other things but I wasn't aware I needed to update the uservariable again.
That makes sense.

So rather than declaring that chrisvar is the same is the same as the user variable at the start I'm actually transferring the value across but need to transfer it back again after!

Cheers
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest