Page 1 of 1

simple Lua Script

Posted: Friday 12 February 2021 19:25
by AndrewmHull
Hello everyone.

Below is a simple script i have created to detect if my garage door is open. It only progresses to the 'if' statements when the time is after 20:00 every 15 minutes.
this bit works fine..
I would now like to track how many times it is continuously open, then if it is open for lets say 4 times (ie an hour) then send a different notification.

I have set the variable 'timesopen' to "0" and then I increment this by 1 each time it detects it is open.

the problem is... if i leave it open it always prints the value 1 to my logs... is there a way to stop it from re-setting the value to 0 at the start of the script?

Code: Select all

time = os.date("*t") -- put the time/date table in variable time
timesopen = 0

commandArray = {}
if (time.hour >=20) and (time.min==0 or time.min==15 or time.min==30 or time.min==45) then 


    if (otherdevices['ZB-Garage Sensor'] == 'Open') 
       commandArray['SendNotification']='Garage Door#Close Garage Door#0#sound#extradata#telegram'
       timesopen = timesopen+1
       print ('Door has been open: ' .. timesopen ..' times')

end
   if (otherdevices['ZB-Garage Sensor'] == 'Closed') then
       count = timesopen=0

end
end
return commandArray




Many thanks in advance

Andy

Re: simple Lua Script

Posted: Friday 12 February 2021 20:40
by waaren
AndrewmHull wrote: Friday 12 February 2021 19:25 the problem is... if i leave it open it always prints the value 1 to my logs... is there a way to stop it from re-setting the value to 0 at the start of the script?
Short answer; No
somewhat longer answer; if you need to keep a value between separate runs of the script you will have to store it outside the script. Probably easiest to do this in classic Lua is to store the value in a domoticz uservariable.

below example should help to get you going.

Code: Select all

commandArray = {}

-- regardless of the uservariable type, the values are always stored as strings. So conversion is needed
local counter = tonumber(uservariables.Counter)

print ("Countervalue is now : " .. counter )
    
if counter > 5 then
    commandArray['Variable:Counter'] = '0' -- reset 
else
   commandArray['Variable:Counter'] = tostring(counter + 1)
end

return commandArray


Re: simple Lua Script

Posted: Friday 12 February 2021 20:55
by AndrewmHull
ok thanks

I will try it.

Cheers

Andy