Example: Netatmo weather station integration with LogicMachine

Task

How to get my Netatmo weather station readings into my KNX network using LogicMachine?

Things to prepare in advance

Netatmo function

Source code    
  1. require 'ltn12'
  2. require 'socket.http'
  3. json = require("json")
  4.  
  5. function refresh_netatmo()
  6.  
  7. local netatmo_debug = false
  8. local client_id = ""
  9. local client_secret = ""
  10. local username = ""
  11. local password = ""
  12. -- optional
  13. --local device_id = ""
  14. local request_token_url = "http://api.netatmo.net/oauth2/token"
  15. local request_device_list = "http://api.netatmo.net/api/devicelist"
  16. local response_body = { }
  17.  
  18.  
  19. if (netatmo_debug) then
  20. alert("netatmo start")
  21. end
  22.  
  23. local request_body = "grant_type=password&client_id=" .. client_id .."&client_secret=" .. client_secret .. "&username=" .. username .. "&password=" .. password
  24.  
  25. local body, code, hdrs, stat = socket.http.request
  26. {
  27. url = request_token_url;
  28. method = "POST";
  29. headers =
  30. {
  31. ["Content-Type"] = "application/x-www-form-urlencoded";
  32. ["Content-Length"] = #request_body;
  33. };
  34. source = ltn12.source.string(request_body);
  35. sink = ltn12.sink.table(response_body);
  36. }
  37.  
  38. if (code ~= 200) then
  39. alert("netatmo auth error")
  40. return
  41. end
  42.  
  43. local response_decode = json.decode(table.concat(response_body))
  44. local access_token = response_decode.access_token
  45. if (netatmo_debug) then
  46. alert("netatmo tocken %s",access_token)
  47. end
  48.  
  49.  
  50. local request_body = "access_token=" .. access_token .. "&app_type=app_station"
  51. -- to limit to only one device :
  52. -- local request_body = "access_token=" .. access_token .. "&app_type=app_station&device_id=" .. device_id
  53.  
  54. local response_body = { }
  55.  
  56. local body, code, hdrs, stat = socket.http.request
  57. {
  58. url = request_device_list;
  59. method = "POST";
  60. headers =
  61. {
  62. ["Content-Type"] = "application/x-www-form-urlencoded";
  63. ["Content-Length"] = #request_body;
  64. };
  65. source = ltn12.source.string(request_body);
  66. sink = ltn12.sink.table(response_body);
  67. }
  68.  
  69. if (code ~= 200) then
  70. alert("netatmo devicelist error")
  71. return
  72. end
  73.  
  74. local response_decode = json.decode(table.concat(response_body))
  75.  
  76. -- Module int
  77. if (netatmo_debug) then
  78. alert("netatmo device 1")
  79. end
  80.  
  81. grp.write("Netatmo Température intérieure",response_decode.body.devices[1].dashboard_data.Temperature,dt.float16)
  82. grp.write("Netatmo Humidité intérieure",response_decode.body.devices[1].dashboard_data.Humidity,dt.scale)
  83. grp.write("Netatmo Pression",response_decode.body.devices[1].dashboard_data.Pressure,dt.uint16)
  84. grp.write("Netatmo CO2",response_decode.body.devices[1].dashboard_data.CO2,dt.uint16)
  85. grp.write("Netatmo Sonomètre",response_decode.body.devices[1].dashboard_data.noise,dt.uint8)
  86. grp.write("Netatmo Température intérieure minimum",response_decode.body.devices[1].dashboard_data.min_temp,dt.float16)
  87. grp.write("Netatmo Température intérieure maximum",response_decode.body.devices[1].dashboard_data.max_temp,dt.float16)
  88. grp.write("Netatmo Pression absolue",response_decode.body.devices[1].dashboard_data.AbsolutePressure,dt.uint16)
  89.  
  90. -- Module ext
  91. if (netatmo_debug) then
  92. alert("netatmo module 1")
  93. end
  94.  
  95. grp.write("Netatmo Température extérieure",response_decode.body.modules[1].dashboard_data.Temperature,dt.float16)
  96. grp.write("Netatmo Humidité extérieure",response_decode.body.modules[1].dashboard_data.Humidity,dt.float16)
  97. grp.write("Netatmo Température extérieure minimum",response_decode.body.modules[1].dashboard_data.min_temp,dt.float16)
  98. grp.write("Netatmo Température extérieure maximum",response_decode.body.modules[1].dashboard_data.max_temp,dt.float16)
  99.  
  100. -- Module Pluvio
  101. if (netatmo_debug) then
  102. alert("netatmo module 2")
  103. end
  104.  
  105. grp.write("Netatmo Pluviomètre",response_decode.body.modules[2].dashboard_data.Rain,dt.float16)
  106. grp.write("Netatmo Pluviomètre 1h",response_decode.body.modules[2].dashboard_data.sum_rain_1,dt.float16)
  107. grp.write("Netatmo Pluviomètre 24h",response_decode.body.modules[2].dashboard_data.sum_rain_24,dt.float16)
  108.  
  109. if (netatmo_debug) then
  110. alert("netatmo end")
  111. end
  112.  
  113. end

Use the function in your programs

Either in Event-based or Resident script run the function by command refresh_netatmo().

Created by Matthieu Bouthors