vk2him / Enphase-Envoy-mqtt-json

Takes real time stream from Enphase Envoy and publishes to mqtt broker
MIT License
55 stars 23 forks source link

Added debug bool and some cleaning of code. #21

Closed helderfmf closed 1 year ago

helderfmf commented 1 year ago

modified: config.yaml modified: data/options.json modified: envoy_to_mqtt_json.py Code cleaning, added debug

vk2him commented 1 year ago

Hi Helder, I tested your debug changes on my V5 system using a test HA internal addon. I noticed if I enabled the Debug switch there was no output mqtt - turning it off worked. Is this expected on a v5 system? Here are my logs - the first is with debug off.

I'll wait your advise before merging - thanks

image

helderfmf commented 1 year ago

First sorry about the mess in this last commit, reverted the repository.yaml to your repo, still struggling how to work with github. In the last commits I corrected a behavior not needed, if FW 5 is detected no need for generation of a token.

Now to the problem you are having when debug is on, I don't see the connection to mqtt. I made some messages in the function wait to see if it is stuck on connect,

vk2him commented 1 year ago

Hi Helder, thanks for the work - I also struggle with Github as I have not much experience with it :(

I've merged your changes and immediately saw when debug was on that my v5 mqtt stream still didn't work. I narrowed it down to one line that is causing the issue - if I comment the following line then the mqtt output stream works and the log file quickly fills with 1 second readings. I think it can't print the stream because it's a stream, ie, it never ends so it's caching the data ready to print it when the stream ends, and it never ends and so the script "locks up" waiting for the end of streaming which never happens.

    while True:
        try:
            url = 'http://%s/stream/meter' % ENVOY_HOST
            if DEBUG: print(dt_string, 'Url:', url)
            stream = requests.get(url, auth=auth, stream=True, timeout=5)
#            if DEBUG: print(dt_string, 'stream:', stream.content)
            for line in stream.iter_lines():
                if DEBUG: print(dt_string, 'Line:', line)
                if line.startswith(marker):
                    if DEBUG: print(dt_string, 'Line marker:', line)
                    data = json.loads(line.replace(marker, b''))
                    if DEBUG: print(dt_string, 'Data:', data)
                    json_string = json.dumps(data)                                   
                    client.publish(topic= MQTT_TOPIC , payload= json_string, qos=0 )
        except requests.exceptions.RequestException as e:
            print(dt_string, ' Exception fetching stream data: %s' % e)

Since the logs fill so quickly, I've also commented these extra lines as with them also enabled the log gets big very fast:

    while True:
        try:
            url = 'http://%s/stream/meter' % ENVOY_HOST
            if DEBUG: print(dt_string, 'Url:', url)
            stream = requests.get(url, auth=auth, stream=True, timeout=5)
#            if DEBUG: print(dt_string, 'stream:', stream.content)
            for line in stream.iter_lines():
#                if DEBUG: print(dt_string, 'Line:', line)
                if line.startswith(marker):
                    if DEBUG: print(dt_string, 'Line marker:', line)
                    data = json.loads(line.replace(marker, b''))
#                    if DEBUG: print(dt_string, 'Data:', data)
                    json_string = json.dumps(data)                                   
                    client.publish(topic= MQTT_TOPIC , payload= json_string, qos=0 )
        except requests.exceptions.RequestException as e:
            print(dt_string, ' Exception fetching stream data: %s' % e)

I've just published this version and I'll ask the few guys on v7 who are having problems to turn on debug and let us know what they see. Thanks again! Cheers Ian