NIBE Uplink API Login
For retrieving data from the NIBE Uplink API. After connecting to the API the tokens are automatically refreshed before they expire.
How to use:
- You need an account for https://api.nibeuplink.com (and a NIBE heatpump)
- Create a new application (https://api.nibeuplink.com/Applications/Create) Set Callback URL: [Your NodeRed-URL] + "/callback-nibe"
- Enter the access data in node "set nibe uplink login data"
- Deploy and then inject the node "set nibe uplink login data". This stores the login data in the flow
Connect to your application by calling [Your NodeRed-URL] + "/connect-nibe"
This flow will refresh it self and hold the access_token alive.
You are able to disconnect it by calling [Your NodeRed-URL] + "/disconnect-nibe"
The last line of nodes in this flow is an examlpe how to use the acces token, which is stored.
[{"id":"78234a9abbbcd9c2","type":"tab","label":"Nibe Uplink Login","disabled":false,"info":"","env":[]},{"id":"0e2d6badb32f92a8","type":"http in","z":"78234a9abbbcd9c2","name":"","url":"/callback-nibe","method":"get","upload":false,"swaggerDoc":"","x":110,"y":440,"wires":[["5a3775ab45e75b14","5a1fbc97c908353e"]]},{"id":"ac3d6e10e340dd69","type":"http request","z":"78234a9abbbcd9c2","name":"get nibe login page","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":true,"proxy":"","authType":"","senderr":false,"x":610,"y":180,"wires":[["8711891339ab1bbf"]]},{"id":"5bc411de85c4043e","type":"http response","z":"78234a9abbbcd9c2","name":"","statusCode":"200","headers":{},"x":1100,"y":180,"wires":[]},{"id":"1d3a269d07206cdc","type":"http request","z":"78234a9abbbcd9c2","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"https://api.nibeuplink.com/oauth/token","tls":"","persist":true,"proxy":"","authType":"","senderr":false,"x":630,"y":440,"wires":[["fbb164bbc70356c3"]]},{"id":"5a3775ab45e75b14","type":"function","z":"78234a9abbbcd9c2","name":"create callback request","func":"var code = msg.payload.code;\n\nmsg = {};\nmsg.headers = {};\nmsg.headers[\"Content-Type\"] = \"application/x-www-form-urlencoded;charset=UTF-8\";\n\nvar NibeApplication = flow.get(\"NibeApplication\");\n\n//encodeURIComponent\nmsg.payload = \n\"grant_type=\" + \"authorization_code\"\n+ \"&client_id=\" + NibeApplication.Identifier\n+ \"&client_secret=\" + encodeURIComponent(NibeApplication.Secret)\n+ \"&code=\" + code\n+ \"&redirect_uri=\" + encodeURIComponent(NibeApplication.CallbackURL)\n+ \"&scope=READSYSTEM\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":440,"wires":[["1d3a269d07206cdc"]]},{"id":"8711891339ab1bbf","type":"change","z":"78234a9abbbcd9c2","name":"make website look nice","rules":[{"t":"change","p":"payload","pt":"msg","from":"/Account/LogIn?mobile=true","fromt":"str","to":"https://api.nibeuplink.com/Account/LogIn?mobile=true","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"/favicon.ico","fromt":"str","to":"https://api.nibeuplink.com/favicon.ico","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"/Content/","fromt":"str","to":"https://api.nibeuplink.com/Content/","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"/bundles/","fromt":"str","to":"https://api.nibeuplink.com/bundles/","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":180,"wires":[["5bc411de85c4043e"]]},{"id":"65b1cb7b049a7cae","type":"comment","z":"78234a9abbbcd9c2","name":"1. Call login page","info":"","x":100,"y":140,"wires":[]},{"id":"ca0386d8bd02580e","type":"comment","z":"78234a9abbbcd9c2","name":"2. Callback","info":"Auth durchgeführt request erstellen","x":80,"y":400,"wires":[]},{"id":"06ca2c7a6fd0b088","type":"http in","z":"78234a9abbbcd9c2","name":"","url":"/connect-nibe","method":"get","upload":false,"swaggerDoc":"","x":110,"y":180,"wires":[["d5c88e2112aea0fc"]]},{"id":"fbb164bbc70356c3","type":"json","z":"78234a9abbbcd9c2","name":"","property":"payload","action":"","pretty":false,"x":810,"y":440,"wires":[["35137674b51c827d","ce21ed0fd20b01fb"]]},{"id":"35137674b51c827d","type":"function","z":"78234a9abbbcd9c2","name":"save tokens","func":"flow.set(\"NIBE_READSYSTEM\", {\n \"NIBE_refresh_token\": msg.payload.refresh_token,\n \"NIBE_expires_in\": new Date(Date.now() + (msg.payload.expires_in * 1000))\n});\nglobal.set(\"NIBE_READ_TOKEN\", msg.payload.access_token);","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1110,"y":440,"wires":[]},{"id":"5a1fbc97c908353e","type":"template","z":"78234a9abbbcd9c2","name":"create ui response","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n <head></head>\n <body>\n <h1>Verbindung hergestellt.</h1>\n <p>Wenn sich dieses Fenster nicht schließt, schließen Sie es bitte.</p>\n <script>\ndocument.onreadystatechange = function () {\n if (document.readyState == \"complete\") {\n if (window.opener != null) {\n window.close();\n }\n }\n}\n </script>\n </body>\n</html>","output":"str","x":370,"y":480,"wires":[["99f770740964c6ed"]]},{"id":"99f770740964c6ed","type":"http response","z":"78234a9abbbcd9c2","name":"","statusCode":"200","headers":{},"x":1100,"y":480,"wires":[]},{"id":"3811d1844a675c7f","type":"delay","z":"78234a9abbbcd9c2","name":"wait for updating tokens","pauseType":"delayv","timeout":"1500","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":870,"y":320,"wires":[["6712fe28998e3741"]]},{"id":"ce21ed0fd20b01fb","type":"function","z":"78234a9abbbcd9c2","name":"set delay by token expire date","func":"msg.delay = ((msg.payload.expires_in - 300) * 1000)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":340,"wires":[["3811d1844a675c7f"]]},{"id":"6712fe28998e3741","type":"function","z":"78234a9abbbcd9c2","name":"create refresh request","func":"msg = {};\n\nmsg.headers = {};\nmsg.headers[\"Content-Type\"] = \"application/x-www-form-urlencoded;charset=UTF-8\";\n\nvar data = flow.get(\"NIBE_READSYSTEM\");\nvar NibeApplication = flow.get(\"NibeApplication\");\n\nmsg.payload = \n\"grant_type=\" + \"refresh_token\"\n+ \"&client_id=\" + NibeApplication.Identifier\n+ \"&client_secret=\" + encodeURIComponent(NibeApplication.Secret)\n+ \"&refresh_token=\" + data.NIBE_refresh_token;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":400,"wires":[["1d3a269d07206cdc"]]},{"id":"d5c88e2112aea0fc","type":"function","z":"78234a9abbbcd9c2","name":"create login request","func":"var NibeApplication = flow.get(\"NibeApplication\");\n\nmsg.url = \"https://api.nibeuplink.com/oauth/authorize?response_type=code&client_id=\" + NibeApplication.Identifier + \"&scope=READSYSTEM&redirect_uri=\" + NibeApplication.CallbackURL + \"&state=0\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":180,"wires":[["ac3d6e10e340dd69"]]},{"id":"108f2b84bd865b0d","type":"http in","z":"78234a9abbbcd9c2","name":"","url":"/disconnect-nibe","method":"get","upload":false,"swaggerDoc":"","x":120,"y":260,"wires":[["d9f0563f7dbb3420"]]},{"id":"5c060e30e4ddae44","type":"template","z":"78234a9abbbcd9c2","name":"create ui response","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n <head></head>\n <body>\n <h1>OK</h1>\n <p>Sie wurden erfolgreich getrennt.</p>\n <script>\ndocument.onreadystatechange = function () {\n if (document.readyState == \"complete\") {\n if (window.opener != null) {\n setTimeout(window.close, 2000);\n }\n }\n}\n </script>\n </body>\n</html>","output":"str","x":610,"y":260,"wires":[["b9fa78a821be4e52"]]},{"id":"b9fa78a821be4e52","type":"http response","z":"78234a9abbbcd9c2","name":"","statusCode":"200","headers":{},"x":1100,"y":260,"wires":[]},{"id":"d9f0563f7dbb3420","type":"function","z":"78234a9abbbcd9c2","name":"delete tokens","func":"flow.set(\"NIBE_READSYSTEM\", null);\nglobal.set(\"NIBE_READ_TOKEN\", null);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":260,"wires":[["5c060e30e4ddae44","24f4df098b17df43"]]},{"id":"24f4df098b17df43","type":"function","z":"78234a9abbbcd9c2","name":"reset delay","func":"return { reset: 1 };","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":300,"wires":[["3811d1844a675c7f"]]},{"id":"96cc0fe28e42ebe5","type":"comment","z":"78234a9abbbcd9c2","name":"Init","info":"","x":70,"y":40,"wires":[]},{"id":"06f0c5719afd5d3f","type":"inject","z":"78234a9abbbcd9c2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":80,"wires":[["48ba1db1becae832"]]},{"id":"48ba1db1becae832","type":"function","z":"78234a9abbbcd9c2","name":"set nibe uplink login data","func":"flow.set(\"NibeApplication\", {\n \"CallbackURL\": \"\",\n \"Identifier\": \"\",\n \"Secret\": \"\"\n});","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":80,"wires":[]},{"id":"c3de62c1908c182c","type":"inject","z":"78234a9abbbcd9c2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":580,"wires":[["643bba9070f6a7b7"]]},{"id":"643bba9070f6a7b7","type":"function","z":"78234a9abbbcd9c2","name":"get api/v1/systems","func":"var readtoken = global.get(\"NIBE_READ_TOKEN\");\nvar request = {};\nrequest.headers = { Authorization: \"Bearer \" + readtoken };\n\nrequest.url = \"https://api.nibeuplink.com/\" +\n \"api/v1/systems\";\n\nreturn request;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":580,"wires":[["44918d08d53fd59b"]]},{"id":"44918d08d53fd59b","type":"http request","z":"78234a9abbbcd9c2","name":"","method":"GET","ret":"txt","paytoqs":"body","url":"","tls":"","persist":false,"proxy":"","authType":"bearer","senderr":false,"x":650,"y":580,"wires":[["9d35229b69923ed2"]]},{"id":"9d35229b69923ed2","type":"json","z":"78234a9abbbcd9c2","name":"","property":"payload","action":"","pretty":true,"x":890,"y":580,"wires":[["bb428c087391d42d"]]},{"id":"bb428c087391d42d","type":"debug","z":"78234a9abbbcd9c2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1090,"y":580,"wires":[]}]