Welcome to Smart Home Forum by FIBARO
Dear Guest,
as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.
As a member you can:
- Start new topics and reply to others
- Follow topics and users to get email updates
- Get your own profile page and make new friends
- Send personal messages
- ... and learn a lot about our system!
Regards,
Smart Home Forum by FIBARO Team
Question
Ros 22
Hi guys,
Good news for the happy customers of Nibe! I am sharing the code for the Nibe Uplink scene.
This scene has to be heavily customized for each user, since there are so many different Nibe products that work with the Uplink cloud. My scene is done for the F1255PC.
So far this code has been used by @perjar, @[email protected] - you guys are welcome to share your versions of the code and any improvements!
Replace XXXXX with your Auth credentials - Please follow the instructions in STEP2 here https://www.marshflattsfarm.org.uk/wordpress/?page_id=3480
Please, look though the code and create the necessary Global Variables.
--[[
%% properties
%% autostart
%% globals
--]]
local Authorized = true --Set to true once you have saved the OAuth data
local access_token
local refresh_token
local JRefresh
local cHM
local http = net.HTTPClient({timeout=5000})
-- if scene is started by autorun and is running in the loop then set selfRun to true
-- else if it is called by another scene like Main scene for time based events then
-- set selfRun to false
local selfRun = true
-- if above selfRun is set to true then set refresh rate in seconds
local refreshloop = 300
-- DEBUGGING VARIABLES ---------------------------------------------------
-- setup debugging, true is turned on, false turned off. If set to false
-- only ERROR messages will be shown in debug window
local deBug = true
-- debug function
function logbug(color, message)
if deBug then
for line in message:gmatch("[^\010\013]+") do
local txt = line:gsub("([\038\060\062])",
function(c)
return "&#"..string.byte(c)..";"
end)
fibaro:debug(('<span style="color:%s">%s</span>'):format(color,txt))
end
end
end
function UpdateNibeTable ()
fibaro:setGlobal('NibeParameters', json.encode(NibeParameters))
logbug('green', 'Table updated')
end
-- SETUP NIBE ACCOUNT (Please follow the instructions in STEP2 here
Please login or register to see this link.
)--------------------------------------------------------local client_id = XXXXX'
local client_secret = 'XXXX'
local redirect_uri = 'https://www.marshflattsfarm.org.uk/nibeuplink/oauth2callback/index.php'
local code = 'XXXX'
local requestAuth = 'grant_type=authorization_code&client_id=' ..client_id..
'&client_secret=' ..client_secret..
'&code=' ..code..
'&redirect_uri=' ..redirect_uri..
'&scope=READSYSTEM%20WRITESYSTEM'
--Making sure that only one instance of the scene is running.
if (fibaro:countScenes() > 1) then
fibaro:abort()
end
--Main function (looping)
function OAuth(nextFunction)
if not Authorized then
http:request('https://api.nibeuplink.com/oauth/token', {
options={
headers = {
['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
},
data = requestAuth,
method = 'POST',
timeout = 20000
},
success = function(Oauth)
logbug('green', 'Initial authorisation response '..Oauth.status)
fibaro:setGlobal("NibeOauth", Oauth.data)
Authorized = true
fibaro:debug('Variables ok')
end,
error = function(error)
print "Initial Auth ERROR"
print(error)
end
})
else
local Oauth = fibaro:getGlobalValue("NibeOauth")
local JOauth = json.decode(Oauth)
local refresh_token = JOauth.refresh_token
local refreshAuth = 'grant_type=refresh_token&client_id=' ..client_id..
'&client_secret=' ..client_secret..
'&refresh_token=' ..refresh_token
http:request('https://api.nibeuplink.com/oauth/token', {
options={
headers = {
['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
},
data = refreshAuth,
method = 'POST',
timeout = 20000
},
success = function(Refresh)
logbug('green', 'Refresh response '..Refresh.status)
JRefresh = json.decode(Refresh.data)
access_token = JRefresh.access_token
refresh_token = JRefresh.refresh_token
if refresh_token == nil then
logbug ("red","Error: No Refresh Token Found")
else
logbug("green","Refresh token obtained from Nibe")
fibaro:setGlobal("NibeOauth", Refresh.data)
NibeParameters = json.decode(fibaro:getGlobalValue("NibeParameters"))
setTimeout(UpdateNibeTable, 20000)
ConnectionStatus ()
CategoryStatus ()
Parameters ()
GSHPStatus ()
GSHPMode ()
fibaro:debug('Variables ok')
end
end,
error = function(error)
print "Refresh auth ERROR"
print(error)
end
})
end
if selfRun then
logbug("violet", "Wait "..refreshloop.." seconds for next run")
setTimeout(OAuth, refreshloop*1000)
end
end
-- Checks Nibe Uplink connection
function ConnectionStatus (NextFunction)
http:request('https://api.nibeuplink.com/api/v1/systems/35842', {
options={
headers = {
['Authorization'] = 'Bearer '..access_token,
},
method = 'GET',
timeout = 5000
},
success = function(NibeData)
local JNibeData = json.decode(NibeData.data)
cHM = os.date("%H:%M")
if JNibeData.connectionStatus ~= nil then
NibeConnectionStatus = JNibeData.connectionStatus..' в '..cHM
logbug('yellow', JNibeData.connectionStatus)
fibaro:setGlobal("NibeConnectStatus", NibeConnectionStatus)
NibeParameters.connectionStatus = NibeConnectionStatus
end
end,
error = function(error)
print "Connection status request - ERROR"
print(error)
end
})
end
-- Updates main parameters as in Service Info/Status
function CategoryStatus (NextFunction)
http:request('https://api.nibeuplink.com/api/v1/systems/35842/serviceinfo/categories/status', {
options={
headers = {
['Authorization'] = 'Bearer '..access_token,
},
method = 'GET',
timeout = 5000
},
success = function(NibeData)
local JNibeData = json.decode(NibeData.data)
for i = 1, #JNibeData do
if JNibeData.title == 'outdoor temp.' then
logbug('yellow',JNibeData.title..' '..JNibeData.rawValue)
OutsideTemp = tonumber(JNibeData.rawValue)/10
gui_weather = {id=3, properties={Temperature=OutsideTemp}}
api.put("/devices", gui_weather)
logbug("green", "GUI Weather temp updated")
end
if JNibeData.rawValue ~= nil then
NibeParameters[JNibeData.title] = JNibeData.rawValue
end
end
end,
error = function(error)
print "Category status request - ERROR"
print(error)
end
})
end
-- Get room temperature
function Parameters ()
http:request('https://api.nibeuplink.com/api/v1/systems/35842/parameters?parameterIds=hot_water_boost', {
options={
headers = {
['Authorization'] = 'Bearer '..access_token,},
method = 'GET',
timeout = 5000
},
success = function(NibeData)
local JNibeData = json.decode(NibeData.data)
for i = 1,#JNibeData do
if JNibeData.title == 'Temporary Lux' then
logbug('yellow',JNibeData.title..' '..JNibeData.displayValue)
fibaro:setGlobal("LuxWater", JNibeData.displayValue)
NibeParameters[JNibeData.title] = JNibeData.displayValue
end
end
end,
error = function(error)
print "Parameters request - ERROR"
print(error)
end
})
end
--Set heat pump mode (Home/Vacation)
function GSHPStatus ()
SmartHomeMode = fibaro:getGlobalValue("GSHPStatus")
if SmartHomeMode == 'Home' then body = json.encode({mode="DEFAULT_OPERATION"})
elseif SmartHomeMode == 'Away' then body = json.encode({mode="AWAY_FROM_HOME"})
elseif SmartHomeMode == 'Vacation' then body = json.encode({mode="VACATION"})
end
http:request('https://api.nibeuplink.com/api/v1/systems/35842/smarthome/mode', {
options={
headers = {
['Authorization'] = 'Bearer '..access_token,
['Content-Type'] = 'application/json'},
data = body,
method = 'PUT',
timeout = 5000
},
success = function(Response)
logbug('yellow', 'SmartHome Response '..Response.status)
end,
error = function(error)
print "GSHPStatus setting - ERROR"
print(error)
end
})
end
-- Updates Heat Pump Mode (Nil/Heating/Cooling/Hot water)
function GSHPMode (NextFunction)
http:request('https://api.nibeuplink.com/api/v1/systems/35842/status/system', {
options={
headers = {
['Authorization'] = 'Bearer '..access_token,
},
method = 'GET',
timeout = 5000
},
success = function(NibeData)
--fibaro:debug(NibeData.data)
local JNibeData = json.decode(NibeData.data)
if fibaro:getGlobalValue("GSHPStatus") == 'Home'
then
if (JNibeData[1] == nil) then fibaro:setGlobal('NibeMode', 'Idle')
elseif (JNibeData[1].image.name == 'Supply') then fibaro:setGlobal('NibeMode', 'Idle')
elseif (JNibeData[1].image.name == 'Heating') then fibaro:setGlobal('NibeMode', 'Heating')
elseif (JNibeData[1].image.name == 'Cooling') then fibaro:setGlobal('NibeMode', 'Cooling')
elseif (JNibeData[1].image.name == 'Drop') then fibaro:setGlobal('NibeMode', 'Hot Water')
end
else
if (JNibeData[2] == nil) then fibaro:setGlobal('NibeMode', 'Idle')
elseif (JNibeData[2].image.name == 'Heating') then fibaro:setGlobal('NibeMode', 'Heating')
elseif (JNibeData[2].image.name == 'Cooling') then fibaro:setGlobal('NibeMode', 'Cooling')
elseif (JNibeData[2].image.name == 'Drop') then fibaro:setGlobal('NibeMode', 'Hot Water')
end
end
Compressor = false
for i = 1,#JNibeData do
if JNibeData.image.name == 'Compressor' then
Compressor = true
NibeParameters.compressorFrequency = JNibeData.parameters[4].rawValue
end
end
if Compressor == false then NibeParameters.compressorFrequency = nil end
end,
error = function(error)
print "GSHPMode request - ERROR"
print(error)
end
})
end
--Main part launching the loop
OAuth()
Link to comment
Share on other sites
19 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.