User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

The following script check temperature for one or more zones, to find out if windows or doors are open: in that case send notification by Telegram

Useful to avoid energy consumption during winter or summer, when someone forget windows/doors open.

The up-to-date version of this file is avaible at http://docs.crasol.it/script_time_openwindows.lua

  1. -- Script that checks temperature and try to discover if doors/windows are open: in that case sends notification by Telegram.
  2. -- Useful to limit energy consumption in winter and summer, if someone forgets windows or doors open.
  3. --
  4. -- This script should be named DOMOTICZ_HOME/scripts/lua/script_time_SCRIPTNAME.lua , e.g. /home/pi/domoticz/scripts/lua/script_time_openwindows.lua
  5. -- It will be called every minute and will check and compare, for each zone (defined below), the current temperature with previous temperature.
  6. --
  7. -- The following user variables must be set:
  8. -- telegramChatid : the Telegram chat ID where notifications should be sent : see https://www.domoticz.com/wiki/Telegram_Bot
  9. -- telegramToken : the Telegram token : see https://www.domoticz.com/wiki/Telegram_Bot
  10. -- HeatingOn: a variables that assumes the following values
  11. -- 0 => Heating/Cooling system is OFF
  12. -- 1 => Heating is ON
  13. -- 2 => Cooling is ON
  14. --
  15. -- Creasol - https://www.creasol.it/products
  16.  
  17. debug=1 -- if True, print debug information
  18. domoticzUrl='http://192.168.3.230:8080'
  19.  
  20. -- zones: array that associate for each zone the name of the temperature device, and max difference for temperature
  21. -- This script automatically create a variable zTemp_ZONENAME that contains the temperature measured before
  22. zones={ --zonename {tempsensor,difference}
  23. ['Cucina']={'Temp_Cucina',0.6},
  24. ['Bagno']={'Temp_Bagno',0.6},
  25. ['Camera']={'Temp_Camera',0.6},
  26. ['Camera_Valentina']={'Temp_Camera_Valentina',0.6},
  27. ['Camera_Ospiti']={'Temp_Camera_Ospiti',0.6},
  28. ['Stireria']={'Temp_Camera',0.6}, --TODO: replace Temp_Camera with Temp_Stireria
  29. }
  30.  
  31. commandArray={} -- reset commandArray, an associative array that will contain the list of commands for Domoticz.
  32.  
  33. function telegramNotify(msg)
  34. os.execute('curl --data chat_id='..uservariables['telegramChatid']..' --data-urlencode "text='..msg..'" "https://api.telegram.org/bot'..uservariables['telegramToken']..'/sendMessage" ')
  35. end
  36.  
  37. function min2hours(mins)
  38. -- convert minutes in hh:mm format
  39. return string.format('%02d:%02d',mins/60,mins%60)
  40. end
  41.  
  42. function timedifference (s)
  43. year = string.sub(s, 1, 4)
  44. month = string.sub(s, 6, 7)
  45. day = string.sub(s, 9, 10)
  46. hour = string.sub(s, 12, 13)
  47. minutes = string.sub(s, 15, 16)
  48. seconds = string.sub(s, 18, 19)
  49. t1 = os.time()
  50. t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
  51. difference = os.difftime (t1, t2)
  52. return difference
  53. end
  54.  
  55. function checkVar(varname,vartype,value)
  56. -- check if create, if not exist, a variable with defined type and value
  57. -- type=
  58. -- 0=Integer
  59. -- 1=Float
  60. -- 2=String
  61. -- 3=Date in format DD/MM/YYYY
  62. -- 4=Time in format HH:MM
  63. local url
  64. if (uservariables[varname] == nil) then
  65. telegramNotify('Created variable ' .. varname..' = ' .. value)
  66. url=domoticzUrl..'/json.htm?type=command&param=saveuservariable&vname=' .. varname .. '&vtype=' .. vartype .. '&vvalue=' .. value
  67. -- openurl works, but can open only 1 url per time. If I have 10 variables to initialize, it takes 10 minutes to do that!
  68. -- commandArray['OpenURL']=url
  69. os.execute('curl "'..url..'"')
  70. uservariables[varname] = value;
  71. end
  72. end
  73.  
  74. if (debug) then print('------------------------- openwindows --------------------------------') end
  75.  
  76. timenow = os.date("*t")
  77. minutesnow = timenow.min + timenow.hour * 60
  78.  
  79. checkVar('HeatingOn',0,0) -- 0=heating off, 1=heating on, 2=cooling on
  80.  
  81. for n,v in pairs(zones) do -- check that temperature setpoint exist
  82. -- n=zone name, v=array with temperature sensor name and max acceptable temperature drop in degrees
  83. checkVar('zTemp_'..n,1,otherdevices[v[1]]) -- if zTemp_Cucina does not exist, create variable and store current temperature
  84. if (otherdevices[v[1]]==nil) then
  85. telegramNotify('Zone '..n..': temperature sensor '..v[1]..' does not exist')
  86. end
  87. end
  88.  
  89. if (uservariables['HeatingOn']==1) then
  90. -- Heating enabled
  91. -- compare zTemp_ZONE (old temperature) with current temperature
  92. for n,v in pairs(zones) do
  93. -- n=zonename (HeatingSP_n = setpoint temperature)
  94. -- v[1]=tempsensor
  95. -- v[2]=max difference
  96. diffTemp=tonumber(otherdevices[v[1]])-uservariables['zTemp_'..n]
  97. if (diffTemp>=0) then
  98. -- current temperature > old temperature: update old temperature
  99. commandArray['Variable:zTemp_'..n]=otherdevices[v[1]]
  100. else
  101. -- current temperature < old temperature
  102. -- compute gradient (diffTemp/TIME)
  103. gradient=diffTemp*60/timedifference(uservariables_lastupdate['zTemp_'..n]) -- degrees/minute
  104. --if (diffTemp<=-0.2) then
  105. if (debug) then print('Zone='..n..' gradient='..string.format('%0.3f',gradient)..' Temp='..otherdevices[v[1]]..'C diffTemp='..diffTemp) end
  106. if (math.abs(gradient)<(0.2/30)) then -- temperature falls slowly, less than 0.2C on 30 minutes
  107. -- |gradient|=v[2]) then
  108. telegramNotify('Zone '..n..': window open!! Gradient='..string.format('%0.2f',gradient*60)..'K/hour');
  109. commandArray['Variable:zTemp_'..n]=otherdevices[v[1]] -- update zTemp_ZONE temperature
  110. end
  111. end
  112. --end
  113. end
  114. end
  115. elseif (uservariables['HeatingOn']==2) then
  116. -- HeatingOn=2 => Cooling
  117. -- TODO
  118. else
  119. -- HeatingOn=0 => Heating or Cooling = OFF => don't check windows
  120. end
  121.  
  122. if (debug) then
  123. for i, v in pairs(commandArray) do
  124. print('### ++++++> Device Changes in commandArray: '..i..':'..v)
  125. end
  126. end
  127. ::mainEnd::
  128. return commandArray

Our products for Domoticz: Creasol DomESP1 and DomBus1

 Need help about DomBus protocol and devices? join the DomBus channel on Telegram!

Pin It
product_id=126