This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Netatmo
#50
(23.11.2023, 14:35)stigen Wrote: Hi!

My netatmo script, the old one, doesnt work anymore because they changed there API etc..
I have tried diffrent things but it doesnt work....
Does anyone have a step by step and example script with main module and outdoor module example? 
Very nice if there is also in the script examples of how too grp.write to objects on the Logimachine Smile

Thanx!

Hi stigen,

in the first step you need to setup a Netatmo Developer account: https://dev.netatmo.com

Then create a application inside Netatmo development area to get the relevant tokens.

Then generate a new user script, name it "netatmo" and add the token in the following script:

Code:
require 'ltn12'
require 'socket.http'
json = require("json")
https = require 'ssl.https'
socket.http.TIMEOUT = 5

netatmo_debug = true

client_id = "XXX"
client_secret = "xxx"
refresh_token_org = "xxx" -- get this token from the netatmo dev page, generate first auth and refresh_token

request_token_url = "https://api.netatmo.net/oauth2/token"
request_url = "https://api.netatmo.net/api"
response_body = { }

if (netatmo_debug) then
  log("netatmo start")
end


-- get auth token from refresh token
-- https://github.com/akbooer/Netatmo/blob/master/L_Netatmo.lua#L430
function refresh_token()
    refresh = storage.get('netatmo_refresh_1', refresh_token_org)
 
  local request_body = "grant_type=refresh_token&client_id=" .. client_id .."&client_secret=" .. client_secret .. "&refresh_token=" .. refresh

  local body, code, hdrs, stat = https.request
  {
    url = request_token_url;
    method = "POST";
    headers =
    {
      ["Content-Type"] = "application/x-www-form-urlencoded";
      ["Content-Length"] = #request_body;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }

  if (code ~= 200) then
    if (netatmo_debug) then
      log("netatmo auth error: "..tostring(code)..", "..tostring(body))
    end
    return
  end

  local response_decode = json.decode(table.concat(response_body))
  local access_token = response_decode.access_token
  if response_decode.refresh_token then
        storage.set('netatmo_refresh_1', response_decode.refresh_token)
  end
  if (netatmo_debug) then
    log(response_body)
    log("netatmo token %s",access_token)
  end
  return "access_token=" .. access_token 
end


-- init
if not init then
  if (netatmo_debug) then
      log("netatmo init")
    end
     request_body = refresh_token()
  init = true
end
if not request_body and netatmo_debug then
    log("netatmo token error")
end


function get_stationdata()
  if not request_body then return end
  --to limit to only one device : -- local request_body = "access_token=" .. access_token .. "&device_id=" .. device_id
  --https://dev.netatmo.com/dev/resources/technical/reference/weatherstation/getstationsdata
  local response_body = { }
  local body, code, hdrs, stat = https.request
  {
    url = request_url..'/getstationsdata';
    method = "POST";
    headers =
    {
      ["Content-Type"] = "application/x-www-form-urlencoded";
      ["Content-Length"] = #request_body;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  if (code ~= 200) then
    if (netatmo_debug) then
      log("netatmo devicelist error: "..tostring(code)..", "..tostring(body)..", "..tostring(hdrs)..", "..tostring(stat))
    end
    return
  end
  local response_decode = json.decode(table.concat(response_body))
  if (netatmo_debug) then
        log(response_decode)
        log(response_decode.body.devices[1].modules[4].dashboard_data)
  end

--write station data to KNX
   
    grp.write("Netatmo_Main_CO2",response_decode.body.devices[1].dashboard_data.CO2)
    grp.write("Netatmo_Main_Humidity",response_decode.body.devices[1].dashboard_data.Humidity)
    grp.write("Netatmo_Main_Temp",response_decode.body.devices[1].dashboard_data.Temperature)
   
    grp.write("Netatmo_Aussen_Temp",response_decode.body.devices[1].modules[1].dashboard_data.Temperature)
    grp.write("Netatmo_Aussen_Humidity",response_decode.body.devices[1].modules[1].dashboard_data.Humidity)
    grp.write("Netatmo_Aussen_Battery",response_decode.body.devices[1].modules[1].battery_percent)
   
    grp.write("Netatmo_Bad_Temp",response_decode.body.devices[1].modules[2].dashboard_data.Temperature)
    grp.write("Netatmo_Bad_Humidity",response_decode.body.devices[1].modules[2].dashboard_data.Humidity)
    grp.write("Netatmo_Bad_Battery",response_decode.body.devices[1].modules[2].battery_percent)
    grp.write("Netatmo_Bad_CO2",response_decode.body.devices[1].modules[2].dashboard_data.CO2)
   
    grp.write("Netatmo_Küche_Temp",response_decode.body.devices[1].modules[3].dashboard_data.Temperature)
    grp.write("Netatmo_Küche_Humidity",response_decode.body.devices[1].modules[3].dashboard_data.Humidity)
    grp.write("Netatmo_Küche_Battery",response_decode.body.devices[1].modules[3].battery_percent)
    grp.write("Netatmo_Küche_CO2",response_decode.body.devices[1].modules[3].dashboard_data.CO2)
   
    grp.write("Netatmo_Rain",response_decode.body.devices[1].modules[4].dashboard_data.Rain)
    grp.write("Netatmo_Rain_24h",response_decode.body.devices[1].modules[4].dashboard_data.sum_rain_24)
    grp.write("Netatmo_Rain_1h",response_decode.body.devices[1].modules[4].dashboard_data.sum_rain_1)
    grp.write("Netatmo_Rain_Battery",response_decode.body.devices[1].modules[4].battery_percent)
end

if (netatmo_debug) then
  log("netatmo end")
end

In the next step you generate scheduled script (*/5****) and add the following code:

Code:
require('user.netatmo')
get_stationdata()

Run the scheduled script and looking inside the log which module is the right category (outdoor, rain, wind etc.) and adjust the second number ...body.devices[1].modules[4]... .Furthermore change the object name to your KNX group address name.

Now you are ready to go. I hope it helps.

Best Regards
Steffen 
Reply


Messages In This Thread
Netatmo - by Matt - 13.07.2015, 21:43
RE: Netatmo - by George - 16.08.2015, 10:59
RE: Netatmo - by gjniewenhuijse - 04.01.2016, 15:24
RE: Netatmo - by Matt - 04.01.2016, 21:10
RE: Netatmo - by gjniewenhuijse - 11.01.2016, 14:59
RE: Netatmo - by Matt - 11.01.2016, 20:38
RE: Netatmo - by gjniewenhuijse - 12.02.2016, 06:40
RE: Netatmo - by buuuudzik - 22.08.2016, 11:46
RE: Netatmo - by admin - 12.02.2016, 07:09
RE: Netatmo - by Matt - 12.02.2016, 07:37
RE: Netatmo - by gjniewenhuijse - 12.02.2016, 09:20
RE: Netatmo - by gjniewenhuijse - 23.02.2016, 07:05
RE: Netatmo - by buuuudzik - 11.05.2016, 12:02
RE: Netatmo - by admin - 11.05.2016, 12:38
RE: Netatmo - by buuuudzik - 11.05.2016, 13:59
RE: Netatmo - by Peter - 17.05.2016, 21:57
RE: Netatmo - by Matt - 18.05.2016, 10:27
RE: Netatmo - by admin - 24.08.2016, 11:25
RE: Netatmo - by buuuudzik - 24.08.2016, 13:16
RE: Netatmo - by gjniewenhuijse - 10.10.2017, 08:20
RE: Netatmo - by Slok - 15.10.2017, 06:53
RE: Netatmo - by Evens - 14.10.2018, 16:03
RE: Netatmo - by Matt - 20.10.2018, 20:09
RE: Netatmo - by Peter - 29.12.2018, 23:46
RE: Netatmo - by paalidar - 30.01.2019, 17:56
RE: Netatmo - by Matt - 30.01.2019, 20:11
RE: Netatmo - by paalidar - 30.01.2019, 20:15
RE: Netatmo - by Erwin van der Zwart - 30.01.2019, 21:27
RE: Netatmo - by victor.back - 26.09.2020, 10:58
RE: Netatmo - by pioneersteffen - 14.09.2021, 19:48
RE: Netatmo - by Daniel - 15.09.2021, 07:35
RE: Netatmo - by pioneersteffen - 15.09.2021, 11:00
RE: Netatmo - by stemic01 - 10.02.2022, 20:32
RE: Netatmo - by Erwin van der Zwart - 11.02.2022, 00:22
RE: Netatmo - by stemic01 - 11.02.2022, 12:08
RE: Netatmo - by iridium - 13.07.2023, 09:53
RE: Netatmo - by gjniewenhuijse - 13.07.2023, 13:41
RE: Netatmo - by admin - 14.07.2023, 08:46
RE: Netatmo - by gjniewenhuijse - 14.07.2023, 11:52
RE: Netatmo - by gjniewenhuijse - 17.07.2023, 13:40
RE: Netatmo - by iridium - 18.07.2023, 07:26
RE: Netatmo - by stemic01 - 20.07.2023, 08:28
RE: Netatmo - by gjniewenhuijse - 21.07.2023, 06:44
RE: Netatmo - by stemic01 - 03.08.2023, 20:45
RE: Netatmo - by Daniel - 04.08.2023, 07:30
RE: Netatmo - by stemic01 - 04.08.2023, 08:35
RE: Netatmo - by Daniel - 04.08.2023, 09:05
RE: Netatmo - by MLa - 04.08.2023, 10:34
RE: Netatmo - by stigen - 23.11.2023, 14:35
RE: Netatmo - by pioneersteffen - 23.12.2023, 14:13
RE: Netatmo - by MLa - 19.07.2024, 07:59
RE: Netatmo - by MaLe - 14.10.2024, 11:41
RE: Netatmo - by gjniewenhuijse - 07.11.2024, 09:28
RE: Netatmo - by Daniel - 07.11.2024, 09:31
RE: Netatmo - by gjniewenhuijse - 08.11.2024, 14:21

Forum Jump: