23.12.2023, 14:13
(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
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