vk2him / Enphase-Envoy-mqtt-json

Takes real time stream from Enphase Envoy and publishes to mqtt broker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error

edwin123456 opened this issue · comments

I get this error

21/07/2023 07:14:56 Connected to 192.168.2.130:1883
21/07/2023 07:14:56 Subscribed to MQTT_TOPIC: /envoy/json
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "envoy_to_mqtt_json.py", line 353, in scrape_stream_meters
stream = requests.get(url, timeout=5, verify=False, headers=headers)
File "/home/pi/.local/lib/python3.7/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/home/pi/.local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.7/http/client.py", line 1260, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.7/http/client.py", line 1301, in _send_request
self.putheader(hdr, value)
File "/usr/lib/python3.7/http/client.py", line 1238, in putheader
raise ValueError('Invalid header value %r' % (values[i],))

What could be the problem?

Hi, do you have v5 or v7 envoy firmware? The error is from the v7 section so would be good if you can confirm. Are you new to using this or has it worked on previous versions?

I have v7 version ,version 7.6.172
And new to this,
At first the script couldn’t find the json file so i added the right path in the sript , then it couldn’t find the text file I added that path as well
Then it worked a bit thill the error.

thanks for helping me.
Sorry for my English

Ok thanks - it's not clear, are you running this in Home Assistant or stand alone? When you mentioned it couldn't find the json file or the text file, it sounds like you're running it standalone because these are usually set in the configuration page in Home Assistant. If you're running it standalone, have a read towards the bottom of the following as another user is running stand alone and he posted some tips
#5 (comment)
#5 (comment)

@edwin123456 - any update please? Thanks

I have version v7
And using it stand alone
The tip from the other user didnt help.
error stays the same

Thanks

Hi, can you please try updating as some debug code has been added to hopefully identify where the issues may be. Turn on the Debug switch in Configuration and copy/paste the log here please after you remove your long token. Then turn off debug as it can fill the log quickly. Thanks

If you're running this standalone, then you have to enter all configuration parameters manually in the file data/options.json - have you done that? The error is strange, it's trying to connect via http port 80 however for a V7 system it should be https port 443 . The final error No route to host may give some clues - is the system you're running this on the same network as your envoy, ie, 192.168.2.xxx?

On this line of the log, have you truncated/redacted/edited the output before posting as it should be displaying the very long token?

28/07/2023 08:37:03 Read token from file data/token.txt :

Similarly, this line should have printed the same very long token - did you remove it before posing here?

28/07/2023 08:37:03 headers: {'Authorization': 'Bearer \n'}

If you didn't remove it, then what is actually contained in the file data/token.txt as it should be the valid 400+ APK Token? If it's empty, delete it and run it again as it should create a new one.

No need to enter anything into data/token.txt - the script will do it for you. Try deleting data/token.txt then restart the script

What is the error in log now?

Hi - as a test please delete data/token.txt - I'm hoping that might show different detail. Now it's not writing the token to your file, so before you delete it, what are the permissions for data/token.txt - is it writable to the user that's running the script?

Have you tried deleting it and retrying

You're also missing options.json - that needs to be in the data directory ... this is mine

ls -l
-rw------- 1 root root 285 Jul 28 16:33 options.json
-rw-r--r-- 1 root root 408 Jul 20 21:25 token.txt

What does ls-l show now for token.txt - and is your token now in that file with nothing else

ok - can you put your token into token.txt - it should be the token only, nothing else - as a hint here is a short version of what's in mine (start and end).

eyJraWQiOiI3ZDEwMDA1ZC03 cut RMfo3drAdnEQ

Can you look at the contents of token.txt - it appears to have this at the end which may be a new line character - \n
There should not be a new line at the end, it has to be the token only. Maybe dump it in hex to verify?

I'm fairly certain the new line at the end is the problem you're seeing - I have a v5 system but I can "force" a token to be generated - when I do you can see from my log that there isn't a blank line:

28/07/2023 19:37:26 Read token from file data/token.txt :  eyJra   cut  StXK2WrenrlYpNSHMc_XA
28/07/2023 19:37:26 Connected to 192.168.1.74:1883

However yours displays a blank line:

28/07/2023 11:02:16 Read token from file data/token.txt :  xxxxxxxxxDEwMDA1ZC03OXxxxxxxxxxYmNiNC0XxxxxxxxxxxxxxxxThlZTE1NmIiLCJ0eXAiOiJKV1QiLCJhbGcXxxxx

28/07/2023 11:02:16 Will wait for mqtt connect
28/07/2023 11:02:16 Finished waiting for mqtt connect
28/07/2023 11:02:16 Connected to 192.168.2.130:1883

And it's then verified as I print the full token later in the log, and you can see the trailing \n which shouldn't be there - it's in your token.txt file

28/07/2023 11:02:16 headers: {'Authorization': 'Bearer xxxxxxxiI3ZDEwMDA1ZC0xxxxxxxxTRkMGQtYmNiNC0yNXxxxx\n'}

To dump in hex, do this:

od -x token.txt

It will output the file in hex - I suspect the last 4 digits will decode to \n

If I'm correct, what did you use to edit the token.txt file?

This is my last line - my token is much longer than yours - it has to be 408 characters and yours is only 122 charachters.

0000600 7666 304b 6171 526d 6d4e 2d68 4d52 6f66
0000620 6433 4172 6e64 5145
0000630

I don't think you got your correct 408 charachter token , can you follow these instructions to get your new token and put it into token.txt - replace the xxxx with your email and password

curl -s -d 'user[email]=xxxxxx@gmail.com&user[password]=xxxxxxxxx' \
https://enlighten.enphaseenergy.com/login/login.json \
| jq -r .session_id

Result:
32CHARACTERSESSIONID

Then use the envoy serial number and that session ID in the next command to get your token.

curl -s 'https://enlighten.enphaseenergy.com/entrez-auth-token?serial_num=ENVOYSERIALNUMBER' \
-H 'cookie: _enlighten_4_session=32CHARACTERSESSIONID' \
| jq -r '.token'

Result:
408CHARACTERJWT

P.S. - Your last two hex digits are 000a - this is a linefeed so definitely wrong - it displays as \n which can also denote new line