Skip to content

Instantly share code, notes, and snippets.

@ImUrlaub
Last active December 29, 2021 22:07
Show Gist options
  • Save ImUrlaub/90b104ba6b1aee4e090fd34d651f0e69 to your computer and use it in GitHub Desktop.
Save ImUrlaub/90b104ba6b1aee4e090fd34d651f0e69 to your computer and use it in GitHub Desktop.
SMA Sunnyportal Homemanager

Retrieve the JSON data with the current plant data of your pv system from the SMA sunnyportal. The data is passed as javascript object in the payload of the message.

Change the following lines in the script node to fit your credentials:

var email = 'your email';

var password = 'your password';

[{"id":"8a4e3a5d.967608","type":"function","z":"c8cb51c3.86bc","name":"SMA Sunnyportal Homemanager","func":"var email = 'your email';\nvar password = 'your password';\n\nvar request=context.global.request\n\nvar jar = context.get('jar') || request.jar();\ncontext.set('jar',jar);\n\nnode.on('close', function() {\n context.set('jar',null);\n});\n\nvar LoginURL = 'https://www.sunnyportal.com/Templates/Start.aspx';\nvar LoginRequestOpts = {\n headers: {\n 'Host':'www.sunnyportal.com',\n 'Origin':'https://www.sunnyportal.com',\n 'Referer':'https://www.sunnyportal.com/Templates/Start.aspx',\n 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'\n },\n form:{\n __EVENTTARGET:'',\n __EVENTARGUMENT:'',\n ctl00$ContentPlaceHolder1$Logincontrol1$txtUserName:email,\n ctl00$ContentPlaceHolder1$Logincontrol1$txtPassword:password,\n ctl00$ContentPlaceHolder1$Logincontrol1$LoginBtn:'Login',\n ctl00$ContentPlaceHolder1$Logincontrol1$RedirectURL:'',\n ctl00$ContentPlaceHolder1$Logincontrol1$RedirectPlant:'',\n ctl00$ContentPlaceHolder1$Logincontrol1$RedirectPage:'',\n ctl00$ContentPlaceHolder1$Logincontrol1$RedirectDevice:'',\n ctl00$ContentPlaceHolder1$Logincontrol1$RedirectOther:'',\n ctl00$ContentPlaceHolder1$Logincontrol1$PlantIdentifier:'',\n ctl00$ContentPlaceHolder1$Logincontrol1$ServiceAccess:true,\n ctl00$ContentPlaceHolder1$hiddenLanguage:'en-us'\n },\n strictSSL : false,\n\tjar : jar\n}\n\n\nvar DataURL = 'https://www.sunnyportal.com/homemanager';\nvar DataRequestOpts = {\n headers: {\n 'Host':'www.sunnyportal.com',\n 'Referer':'https://www.sunnyportal.com/FixedPages/HoManLive.aspx',\n 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',\n 'X-Requested-With':'XMLHttpRequest'\n },\n strictSSL : false,\n\tjar : jar\n}\n\nfunction doData(){\n request.get(DataURL,DataRequestOpts,function(err,response){\n if(err){\n doLogin();\n return;\n }\n \n if(response.statusCode != 200){\n doLogin();\n return;\n }\n \n try{\n var data = JSON.parse(response.body);\n }\n catch(err){\n doLogin();\n return;\n }\n \n if(data.Timestamp === null){\n doLogin();\n return;\n }\n\n var newMsg = { payload: data };\n node.send(newMsg);\n \n })\n}\n\nfunction doLogin(){\n request.post(LoginURL, LoginRequestOpts, function (err, response) {\n if(err){\n return;\n }\n\n if(response.headers.location && response.headers.location=='/FixedPages/HoManLive.aspx'){\n node.log(\"Login\");\n doData();\n }\n else{\n return;\n }\n })\n}\n\ndoData();","outputs":1,"noerr":0,"x":451.6666564941406,"y":108.66666412353516,"wires":[["7c9226b9.e83ce8","ccaf7d64.728c4"]]},{"id":"7c9226b9.e83ce8","type":"debug","z":"c8cb51c3.86bc","name":"","active":true,"console":"false","complete":"false","x":719.6666259765625,"y":107.66666412353516,"wires":[]},{"id":"5c3722d4.a7aafc","type":"inject","z":"c8cb51c3.86bc","name":"every 5 seconds","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":true,"x":182.66665649414062,"y":108.66666412353516,"wires":[["8a4e3a5d.967608"]]}]
@iooner
Copy link

iooner commented Jan 4, 2017

Hi and thank :)
But i get an error : TypeError: Cannot read property 'jar' of undefined

@ThorstenF
Copy link

Auch von mir vielen Dank!
Ich bekomme ebenfalls den Error: TypeError: Cannot read property 'jar' of undefined
Ich habe einen RaspberryPi3 muss ich etwas umstellen?

@alport
Copy link

alport commented Dec 9, 2018

I also get the error: TypeError: Cannot read property 'jar' of undefined.
Any chance of looking at this for us?
Would really like to read Sunny Portal - and doing it in Node-Red would really be an elegant solution.
Thanks,

@christiankenel
Copy link

Geht mir leider auch so. => "TypeError: Cannot read property 'jar' of undefined"
Läuft bei mir im Docker-Container

@vdoucet
Copy link

vdoucet commented Jun 9, 2019

Hi, does the code actually works or does it require a library that I don't have ? I get the same error as in the above posts.

@ImUrlaub
Copy link
Author

ImUrlaub commented Jun 9, 2019 via email

@vdoucet
Copy link

vdoucet commented Jun 9, 2019

Thank's ! I got it running without error .. I'll try to get something out of it now.
One question though ... why going inverter>sunnyportal>request data rather than attacking directly the inverter ?

@ImUrlaub
Copy link
Author

ImUrlaub commented Jun 9, 2019 via email

@vdoucet
Copy link

vdoucet commented Jun 9, 2019

Ok. I have a Sunnyboy 1.5 and it has a web interface to configure and monitor that's why I thought I could eventually get the data directly from it. But I guess that without knowing the inner structure it will be difficult. I think I'll install a small current monitoring on the inverter output to keep it local ...

@rlindner
Copy link

rlindner commented Sep 4, 2019

Hey together, I wanted to get some metrics from my PV system as well, but didn't like the approach of retrieving those from the sunny portal over the internet. So, in the last two weeks, I've tried to get them from the inverters web interface directly. I've published an early version of a Node-RED node under my GitHub account. It's not yet submitted to the npm registry, because I'd like to be confident it's working as intended, but if any of you is interested in trying it, one can install it directly from the repository with npm install git+https://github.com/rlindner/node-red-contrib-sma-webconnect.git. I'd appreciate any feedback.

@poochitball
Copy link

I can't get the value from homemanager. It show {"__type":"LiveDataUI","Timestamp":{"__type":"DateTime","DateTime":"2020-03-12T10:58:52","Kind":"Unspecified"},"PV":null,"FeedIn":null,"GridConsumption":null,"DirectConsumption":null,"SelfConsumption":null,"SelfSupply":null,"TotalConsumption":null,"DirectConsumptionQuote":null,"SelfConsumptionQuote":null,"AutarkyQuote":null,"BatteryIn":null,"BatteryOut":null,"BatteryChargeStatus":null,"OperationHealth":null,"BatteryStateOfHealth":null,"ModuleTemperature":null,"EnvironmentTemperature":null,"WindSpeed":null,"Insolation":null,"BatteryMode":null,"InfoMessages":["The current consumption could not be determined. The current purchased electricity is unknown. "],"WarningMessages":["The current PV generation could not be fully determined. An inverter did not respond in time.","The battery values could not be determined. The battery did not respond in time."],"ErrorMessages":[],"Info":{}}
How can I solve this problem?

@fritserasmus
Copy link

Hey together, I wanted to get some metrics from my PV system as well, but didn't like the approach of retrieving those from the sunny portal over the internet. So, in the last two weeks, I've tried to get them from the inverters web interface directly. I've published an early version of a Node-RED node under my GitHub account. It's not yet submitted to the npm registry, because I'd like to be confident it's working as intended, but if any of you is interested in trying it, one can install it directly from the repository with npm install git+https://github.com/rlindner/node-red-contrib-sma-webconnect.git. I'd appreciate any feedback.

Hi Roland,

I will be happy to try your code and work with you to get it working!

I have Two SunnyBoys, a Sunny Island and Home Manger 2.0.

It will be awesome to get the production, utilisation and battery status in Node-RED.

Are you still up to get this to work?
Obviously I have some time to spend with you and we can even do a zoom session and I can share my screen so you can see first hand the results.

Regards

Frits
https://www.sunnyportal.com/Templates/PublicPage.aspx?page=6fe8b064-5428-46b6-8333-92cd2ece51c7#
https://www.sunnyportal.com/Templates/PublicPage.aspx?page=31a8994f-6e7d-4a42-8fd4-9aa5b8422317

@rlindner
Copy link

Hi Frits,

I've released a version 0.0.1 of the node a few months ago here: https://flows.nodered.org/node/node-red-contrib-sma-webconnect
And I'm using it since then without any issues so far. Unfortunately I do not have a battery installed, so I cannot contribute anything into that direction. But one thing I'd like to point out, which was the motivation for creating the node, is that it's communicating directly with the inverter (a Sunny Tripower 10.0 in my case) instead of the sunny portal. So it's not talking to the Home Manager.

Roland

@fritserasmus
Copy link

@rlinder,

I was not too lucky:
See the text echoed back:

frits@MQTTonUBUNTU:$ date
Thu Apr 30 12:16:55 SAST 2020
frits@MQTTonUBUNTU:
$ sudo npm install git+https://github.com/rlindner/node-red- contrib-sma-webconnect.git
[sudo] password for frits:
loadDep:requestretry → 20 ▄ ╢██████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░╟
WARN engine request@2.88.2: wanted: {"node":">= 6"} (current: {"node":"4.2.6","n npm WARN deprecated request@2.88.2: request has been deprecated, see https://git hub.com/request/request/issues/3142
loadDep:uuid → resolveWit ▄ ╢██████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░╟
WARN engine har-validator@5.1.3: wanted: {"node":">=6"} (current: {"node":"4.2.6 loadDep:punycode → reques ▐ ╢████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░╟
WARN engine punycode@2.1.1: wanted: {"node":">=6"} (current: {"node":"4.2.6","np /home/frits
├── node-red-contrib-bigtimer@2.3.1
└─┬ node-red-contrib-sma-webconnect@0.0.1 (git+https://github.com/rlindner/node-red-contrib-sma-webconnect.git#c88791699d64b74d604b94978740dfe82166e3fe)
├─┬ request@2.88.2
│ ├── aws-sign2@0.7.0
│ ├── aws4@1.9.1
│ ├── caseless@0.12.0
│ ├─┬ combined-stream@1.0.8
│ │ └── delayed-stream@1.0.0
│ ├── extend@3.0.2
│ ├── forever-agent@0.6.1
│ ├─┬ form-data@2.3.3
│ │ └── asynckit@0.4.0
│ ├─┬ har-validator@5.1.3
│ │ ├─┬ ajv@6.12.2
│ │ │ ├── fast-deep-equal@3.1.1
│ │ │ ├── fast-json-stable-stringify@2.1.0
│ │ │ ├── json-schema-traverse@0.4.1
│ │ │ └── uri-js@4.2.2
│ │ └── har-schema@2.0.0
│ ├─┬ http-signature@1.2.0
│ │ ├── assert-plus@1.0.0
│ │ ├─┬ jsprim@1.4.1
│ │ │ ├── extsprintf@1.3.0
│ │ │ ├── json-schema@0.2.3
│ │ │ └─┬ verror@1.10.0
│ │ │ └── core-util-is@1.0.2
│ │ └─┬ sshpk@1.16.1
│ │ ├── asn1@0.2.4
│ │ ├── bcrypt-pbkdf@1.0.2
│ │ ├── dashdash@1.14.1
│ │ ├── ecc-jsbn@0.1.2
│ │ ├── getpass@0.1.7
│ │ ├── jsbn@0.1.1
│ │ ├── safer-buffer@2.1.2
│ │ └── tweetnacl@0.14.5
│ ├── is-typedarray@1.0.0
│ ├── isstream@0.1.2
│ ├── json-stringify-safe@5.0.1
│ ├─┬ mime-types@2.1.27
│ │ └── mime-db@1.44.0
│ ├── oauth-sign@0.9.0
│ ├── performance-now@2.1.0
│ ├── qs@6.5.2
│ ├── safe-buffer@5.2.0
│ ├─┬ tough-cookie@2.5.0
│ │ ├── psl@1.8.0
│ │ └── punycode@2.1.1
│ ├── tunnel-agent@0.6.0
│ └── uuid@3.4.0
└─┬ requestretry@4.1.0
├── lodash@4.17.15
└── when@3.7.8

npm WARN enoent ENOENT: no such file or directory, open '/home/frits/package.json'
npm WARN frits No description
npm WARN frits No repository field.
npm WARN frits No README data
npm WARN frits No license field.
frits@MQTTonUBUNTU:$ cd //
-bash: cd: //: No such file or directory
frits@MQTTonUBUNTU:
$ cd ~
frits@MQTTonUBUNTU:$ pwd
/home/frits
frits@MQTTonUBUNTU:
$ ls -l
total 12
drwxrwxr-x 2 root root 4096 Jun 8 2018 backup
drwxrwxr-x 2 frits frits 4096 Jun 8 2018 downloads
drwxrwxr-x 56 frits frits 4096 Apr 30 12:17 node_modules
frits@MQTTonUBUNTU:~$

@rlindner
Copy link

It looks like you've executed the npm install from your home directory, but you need to install the module within your node installation. See https://nodered.org/docs/user-guide/runtime/adding-nodes#installing-with-npm

Since the node is now published, you can install it with npm install node-red-contrib-sma-webconnect instead of using the the GitHub URL (git+https://github.com/rlindner/node-red-contrib-sma-webconnect.git). The README.md is already updated accordingly, I just haven't released a new version reflecting that.

@rlindner
Copy link

rlindner commented May 4, 2020

You're still running the npm install from your home directory, which doesn't seem to be the right location. Some tutorials I've just found for installing node-red on a Raspberry Pi mention ~/.node-red as installation dir. Do you have such a directory? Try cd ~/.node-red. If not, you first have to find that directory. This directory should contain a package.json then. This is what the installer is complaining about.

@fritserasmus
Copy link

Roland,

I apologize, I did not read your instructions properly - sorry.

I success installed it - I think.

I restarted the node-RED service and rebooted the server just to be sure.

How do I get to the SMA node and flow setup?

@fritserasmus
Copy link

Roland,
I also found out this morning my Node-Red, and Node.js is very old.

I will update that first and then retest and revert back if I cannot fix the issue.

@rlindner
Copy link

rlindner commented May 6, 2020

Sounds reasonable and I'm happy to assist if you run into further issues, but I'd suggest to move further discussion to the issue you've opened here.

@Tobi266
Copy link

Tobi266 commented May 27, 2021

Can someone explain me how to add the request lib to settings.js like mentioned

You need the request lib. https://www.npmjs.com/package/request After that it needed to be added to the node-red settings.js. Similar how they did it here with fs. https://flows.nodered.org/flow/cdf160c36f082dc2a20cd1183ac6cd7d

I didn't get it ...
Also request is depricated, is there any option to change code to another lib?
regards

@Whastie
Copy link

Whastie commented Dec 29, 2021

Modify SMA sunny portal plant parameter

I have a photovoltaic plant with:
1 energy meter 2.0
1 sunny boy 5.0
1 Sunny Island 4.4

I would need to switch from node-red when activating battery charging as done from sunny portal.
Does anyone know how to change these parameters from node.red?

I have SMA integration in Home assistant, but these parameters are not accessible.
I have tried the node-webconnect, but they are all output parameters
Since modbus-tcp I have not seen these parameters, since they are accessible through the sunny portal, not directly from the inverters (SB and SI)

I put an image of the parameters that I would like to access from node.red-

Do you know if with this integration of node-red in sunny portal these parameters can be accessed?

Can somebody help me?
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment