Tesla Gateway Local Cookie handling
This Script automatically handles the cookie authentication for Local Tesla Powerwall access. https://github.com/vloschiavo/powerwall2
You need to set the credentials you would normally use local in your browser and put them in the corresponding function node and to adjust the local powerwall IP Adress in the HTTP Getters
[{"id":"c819a55e.a1005","type":"inject","z":"ea16d6e7150b2aee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":650,"y":400,"wires":[["4b6d8588.f6bd94"]]},{"id":"2cf2873.7252af8","type":"inject","z":"ea16d6e7150b2aee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":640,"y":340,"wires":[[]]},{"id":"4b6d8588.f6bd94","type":"function","z":"ea16d6e7150b2aee","name":"Tesla pre Request","func":"TeslaCookie = flow.get(\"TeslaCookie\")\n\nif (!TeslaCookie)\n{\n //Enter your local Gateway user email and password (last 5 Digits of the gateway serial Number)\n msg.payload = {\"username\":\"customer\",\"password\":\"<GatewayPW>\",\"email\":\"<useremail>\",\"clientInfo\":{\"timezone\":\"Europe/Berlin\"}}\n return [msg, null];\n}\n\nmsg.headers = {'Cookie': TeslaCookie};\n\n\nreturn [null, msg];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":890,"y":400,"wires":[["840ca1c1.98902"],["16e929e7.1c9426","82edd33f.04a7e"]]},{"id":"840ca1c1.98902","type":"http request","z":"ea16d6e7150b2aee","name":"Get Tesla Cookie","method":"GET","ret":"txt","paytoqs":"body","url":"https://<Gateway-IP>/api/login/Basic","tls":"355c8d70.949f62","persist":false,"proxy":"","authType":"","credentials":{},"x":1170,"y":380,"wires":[["7a6a1b78.4b6304"]]},{"id":"7a6a1b78.4b6304","type":"function","z":"ea16d6e7150b2aee","name":"Eval Tesla Auth","func":"if(msg.statusCode!=200)\n{\n node.status({fill:\"red\",shape:\"ring\",text:\"Error \\\"\"+msg.payload+\"\\\"\"});\n return null;\n}\n\ncookietext = msg.headers[\"set-cookie\"][0]+\"; \"+msg.headers[\"set-cookie\"][0],\n\nflow.set(\"TeslaCookie\", msg.headers[\"set-cookie\"]);\nnode.status({fill:\"green\",shape:\"dot\",text:\"Success Receiving cookie\"});\n\nreturn;","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1420,"y":380,"wires":[]},{"id":"16e929e7.1c9426","type":"http request","z":"ea16d6e7150b2aee","name":"Get Tesla aggregates","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://<Gateway-IP>/api/meters/aggregates","tls":"355c8d70.949f62","persist":false,"proxy":"","authType":"","x":1180,"y":440,"wires":[["de26d890.f63ce"]]},{"id":"de26d890.f63ce","type":"function","z":"ea16d6e7150b2aee","name":"Eval Tesla Request","func":"if(msg.statusCode!=200)\n{\n node.status({fill:\"red\",shape:\"ring\",text:\"Error \\\"\"+msg.payload+\"\\\"\"});\n flow.set(\"TeslaCookie\", null);\n return;\n}\n\nnode.status({fill:\"green\",shape:\"dot\",text:\"Success\"});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1430,"y":440,"wires":[[]]},{"id":"d0da8c5e.aeaf3","type":"inject","z":"ea16d6e7150b2aee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":640,"y":700,"wires":[["299c630c.7afd9c"]]},{"id":"299c630c.7afd9c","type":"function","z":"ea16d6e7150b2aee","name":"For Testing - crash Tesla cookie","func":"flow.set(\"TeslaCookie\", \"dummy\");\n\nreturn null;","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":700,"wires":[]},{"id":"82edd33f.04a7e","type":"http request","z":"ea16d6e7150b2aee","name":"Get Tesla SOE","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://<Gateway-IP>/api/system_status/soe","tls":"355c8d70.949f62","persist":false,"proxy":"","authType":"","x":1160,"y":500,"wires":[["dd204d2c.06ecf8"]]},{"id":"dd204d2c.06ecf8","type":"function","z":"ea16d6e7150b2aee","name":"Eval Tesla Request","func":"if(msg.statusCode!=200)\n{\n node.status({fill:\"red\",shape:\"ring\",text:\"Error \\\"\"+msg.payload+\"\\\"\"});\n flow.set(\"TeslaCookie\", null);\n return;\n}\n\nnode.status({fill:\"green\",shape:\"dot\",text:\"Success\"});\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1430,"y":500,"wires":[[]]},{"id":"764813e8.a562e4","type":"http request","z":"ea16d6e7150b2aee","name":"Get Tesla status","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://<Gateway-IP>/api/status","tls":"355c8d70.949f62","persist":false,"proxy":"","authType":"","x":1160,"y":580,"wires":[[]]},{"id":"f68ee1ab.6062a8","type":"inject","z":"ea16d6e7150b2aee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1800","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":650,"y":580,"wires":[["764813e8.a562e4"]]},{"id":"dd376f683425ea7e","type":"comment","z":"ea16d6e7150b2aee","name":"Set Credentials","info":"","x":880,"y":360,"wires":[]},{"id":"527b535dc1e74919","type":"comment","z":"ea16d6e7150b2aee","name":"Set IP in all getters","info":"","x":1170,"y":340,"wires":[]},{"id":"355c8d70.949f62","type":"tls-config","name":"Tesla Selbstsignatur ignorieren","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"","servername":"","verifyservercert":false,"credentials":{}}]