Page 1 of 1

user variable not updated

Posted: Tuesday 21 April 2020 22:15
by Manudax
Hi there,
I spent a lot of time tryng to finalise a LUA script.
This script ping an ip address and depending of the result switch on/off a switch.
I use a user variable to validate the result after few tries arroud 5 is a good count to have a revelant test, sometime one or two pings are not sufficient.
The LUA script is activated each minute by Domoticz and make a test period of 5mn, (one ping each minute is a revelant test I think) .
The user variable Var_Ping_PC1 is not updated to zero when the count is achieved.

Code: Select all

commandArray['Variable:Var_Ping_PC1'] = '0'

Code: Select all

commandArray={}
debug=1
if(otherdevices['Ping_PC1']=='Off') then
ping_success=os.execute('ping -n 1 -w 1000 10.10.0.8')
    if ping_success then
    else
        commandArray['Variable:Var_Ping_PC1'] = tostring(uservariables["Var_Ping_PC1"] + 1)
        if uservariables["Var_Ping_PC1"] > 4 then
			commandArray['OpenURL']='https://smsapi.free-mobile.fr/sendmsg?user=MyName&pass=123456&msg=Pas%20de%20ping%20vers%20PC1'
            print(' Pas de ping vers PC1' ..uservariables["Var_Ping_PC1"])
            commandArray['Ping_PC1']='On'
            commandArray['Variable:Var_Ping_PC1'] = '0'
            print('Var_Ping_PC1 = ' ..uservariables["Var_Ping_PC1"])
        end
    
    end
end
return commandArray

Re: user variable not updated

Posted: Wednesday 22 April 2020 13:04
by waaren
Manudax wrote: Tuesday 21 April 2020 22:15 This script ping an ip address and depending of the result switch on/off a switch.
The user variable Var_Ping_PC1 is not updated to zero when the count is achieved.
Some remarks:
  • os.execute does never return anything. ping_success will therefore always be nil
  • the option -w 1000 will potentially block the entire event system for 1000 seconds (domoticz event system is single threaded)
  • the line

    Code: Select all

    print('Var_Ping_PC1 = ' ..uservariables["Var_Ping_PC1"])
    will print what the uservar was when the script started. The update of the value of the uservar will take place when the script has finished and commandArray is processed by domoticz.

Re: user variable not updated

Posted: Tuesday 28 April 2020 22:01
by Manudax
Hello,
thanks for your reply,
the -w is exprimed in millisecondes using the windows command line.

I need the uservariable changed during this script execution not after.

sincerely,
Manudax.

Re: user variable not updated

Posted: Monday 04 May 2020 0:02
by Manudax
HI,
finally I used the internal Domticz ping function and it was ok for me.

I dont know how to close this topic.