Advertisement
boomx

netatmo wind & rain 1.3b

Aug 9th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. --[[
  2. %% properties
  3. %% autostart
  4. %% globals
  5. --]]
  6.  
  7. -- DIESE DATEN ANPASSEN
  8. local client_id = 'xxx'
  9. local client_secret = 'xxx'
  10. local username = 'xxx'
  11. local password = 'xxx'
  12. local refresh = 300
  13. local debug = 1
  14.  
  15. local debug_developer = true
  16.  
  17. local vd_ID = 178
  18.  
  19. -- AB HIER NICHTS MEHR ANPASSEN
  20. local token = ''
  21. local request_body = ''
  22. local rains = {hour = -1000, day = -1000, week = -1000, month = -1000}
  23.  
  24. local sourceTrigger = fibaro:getSourceTrigger()
  25.  
  26. Debug = function ( color, message )
  27. fibaro:debug(string.format('<%s style="color:%s;">%s', "span", color, message, "span"))
  28. end
  29.  
  30. DebugError = function ( color, message )
  31. fibaro:debug(string.format('<%s style="color:%s;">%s', "span", color, message, "span"))
  32. end
  33.  
  34. Debug( 'orange', 'netatmo Zusatzmodul Integration v.1.3b gestartet.' )
  35. Debug( 'white', 'Daten werden alle ' ..(refresh/60).. ' min aktualisiert. Bei debug = 0 wird nichts debuggt' )
  36.  
  37. function oAuth()
  38. local request_body = 'grant_type=password&client_id=' .. client_id .. '&client_secret=' .. client_secret .. '&username=' .. username .. '&password=' .. password .. '&scope=read_station'
  39. getResponseData('https://api.netatmo.net/oauth2/token', request_body,
  40. function(data)
  41. if (data.access_token ~= nil) then
  42. token = data.access_token
  43. if (debug == 1) then
  44. Debug( 'green', 'oAuth 2.0 durchgeführt.' )
  45. end
  46. getDevices()
  47. fibaro:call(vd_ID, 'pressButton', '9')
  48. else
  49. DebugError( 'red', 'oAuth 2.0 konnte nicht durchgeführt werden! Bitte die Anmeldedaten überprüfen')
  50. end
  51. end
  52. )
  53. setTimeout(oAuth, refresh*1000);
  54. end
  55.  
  56. function getResponseData(url, body, func)
  57. local http = net.HTTPClient()
  58. http:request(url, {
  59. options = {
  60. method = 'POST',
  61. headers = {
  62. ['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  63. },
  64. data = body
  65. },
  66. success = function(response)
  67. if (debug_developer) then
  68. Debug( 'blue', '_________DEV-INFO_____________')
  69. fibaro:debug('ERKANNTER DATENTYP: ' ..type(response.data))
  70. fibaro:debug(response.data)
  71. Debug( 'blue', '_______DEV-INFO-END___________')
  72. end
  73. func(json.decode(response.data))
  74. end
  75. })
  76. end
  77.  
  78. function getDevices()
  79. getResponseData('https://api.netatmo.net/api/devicelist','access_token='..token,
  80. function(data)
  81. if (data.body ~= nil) then
  82. if (debug == 1) then
  83. Debug( 'green', 'netatmo Sensoren werden gesucht...' );
  84. end
  85. for _, v in pairs(data.body.modules) do
  86. --fibaro:debug('Tabelle auslesen')
  87. if (v.data_type[1] == 'Rain') then
  88. rain_id = v._id
  89. if (debug == 1) then
  90. Debug( 'green', 'Regensensor ' ..rain_id.. ' erkannt.' );
  91. end
  92. if rain_id ~= nil then
  93. getSumRain(60 * 60, 'hour')
  94. getSumRain(60 * 60 * 24, 'day')
  95. getSumRain(60 * 60 * 24 * 7, 'week')
  96. getSumRain(60 * 60 * 24 * 30, 'month')
  97. end
  98. elseif (v.data_type[1] == 'Wind') then
  99. wind_id = v._id
  100. if (debug == 1) then
  101. Debug( 'green', 'Windmesser ' ..wind_id.. ' erkannt.' );
  102. end
  103. if wind_id ~= nil then
  104. getmeasureWind()
  105. end
  106. end
  107. int_id = data.body.devices[1]._id
  108. end
  109. else
  110. DebugError( 'red', 'device-list konnte nicht abgefragt werden! Bitte nächsten Durchlauf abwarten')
  111. end
  112. end
  113. )
  114. end
  115.  
  116. function getmeasureWind()
  117. request_body_wind = 'access_token='..token..'&device_id='..int_id..'&module_id='..wind_id..'&scale=max&type=WindStrength,WindAngle,GustStrength,GustAngle&date_end=last'
  118. getResponseData('https://api.netatmo.net/api/getmeasure', request_body_wind,
  119. function(getData)
  120. if (getData.body ~= nil) then
  121. if (debug == 1) then
  122. Debug( 'green', 'Windmesser wird ausgelesen...' );
  123. end
  124. WindStrength = getData.body[1].value[1][1]
  125. WindAngle = getData.body[1].value[1][2]
  126. GustStrength = getData.body[1].value[1][3]
  127. GustAngle= getData.body[1].value[1][4]
  128. if (debug == 1) then
  129. Debug( 'green', 'Windgeschwindigkeit: ' .. WindStrength .. ' km/h' );
  130. end
  131. if fibaro:getGlobalValue('windstaerke') ~= nil then
  132. fibaro:setGlobal('windstaerke', WindStrength)
  133. else
  134. DebugError( 'red', 'Varible windstaerke nicht gefunden. Bitte anlegen')
  135. end
  136. if fibaro:getGlobalValue('windrichtung') ~= nil then
  137. fibaro:setGlobal('windrichtung', WindAngle)
  138. else
  139. DebugError( 'red', 'Varible windrichtung nicht gefunden. Bitte anlegen')
  140. end
  141. if fibaro:getGlobalValue('boenstaerke') ~= nil then
  142. fibaro:setGlobal('boenstaerke', GustStrength)
  143. else
  144. DebugError( 'red', 'Varible boenstaerke nicht gefunden. Bitte anlegen')
  145. end
  146. if fibaro:getGlobalValue('boenrichtung') ~= nil then
  147. fibaro:setGlobal('boenrichtung', GustAngle)
  148. else
  149. DebugError( 'red', 'Varible boenrichtung nicht gefunden. Bitte anlegen')
  150. end
  151. if (debug == 1) then
  152. Debug( 'green', 'Windmesser auslesen beendet.' );
  153. end
  154. else
  155. DebugError( 'red', 'API-Call konnte nicht durchgeführt werden! API nicht erreichbar! Bitte nächsten Durchlauf abwarten.')
  156. end
  157. end
  158. )
  159. end
  160.  
  161. function getSumRain(dauer, variable)
  162. local now = os.time();
  163. getResponseData('https://api.netatmo.net/api/getmeasure','access_token='..token..'&device_id='..int_id..'&module_id='..rain_id..'&scale=1hour&type=sum_rain&real_time=true&date_begin='..os.date('!%c', (now - dauer)),
  164. function(data)
  165. local sum_rain = 0
  166. for k, v in pairs(data.body) do
  167. for l, w in pairs(v.value) do
  168. sum_rain = sum_rain + w[1]
  169. end
  170. end
  171. if fibaro:getGlobalValue('rain_' ..variable) ~= nil then
  172. fibaro:setGlobal('rain_' ..variable, sum_rain)
  173. else
  174. DebugError( 'red', 'Varible rain_' ..variable.. ' nicht gefunden. Bitte anlegen')
  175. end
  176. if (debug == 1) then
  177. fibaro:debug('Regenmenge: ' ..sum_rain.. ' mm2 (' .. variable .. ')')
  178. end
  179. end
  180. )
  181. end
  182.  
  183. if (sourceTrigger['type'] == 'autostart') then
  184. oAuth();
  185. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement