simple Lua Script

Moderator: leecollings

Post Reply
AndrewmHull
Posts: 5
Joined: Tuesday 08 December 2020 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

simple Lua Script

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: simple Lua Script

Post 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

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AndrewmHull
Posts: 5
Joined: Tuesday 08 December 2020 10:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: simple Lua Script

Post by AndrewmHull »

ok thanks

I will try it.

Cheers

Andy
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest