dracoventions / TWCManager

Control power delivered by a Tesla Wall Charger using two wires screwed into its RS-485 terminals.
The Unlicense
182 stars 110 forks source link

Add Enphase Local Green Data #25

Open integlikewhoa opened 4 years ago

integlikewhoa commented 4 years ago

Enphase API has been down due to server issues and also has limits on pulls. Is there a way to add pulling local data to this version.

Someone has already made a fork off the original version that does this and I'm thinking pulling local data is much better then cloud.

Here is what it looked it.

try:
        url = urllib.request.urlopen("http://192.168.1.138/production.json?details=1&classic-1")
    except HTTPError as error:
        print(time_now() + " ERROR: Data not retrieved because %s", error)
    except URLError as error:
        if isinstance(error.reason, socket.timeout):
            print(time_now() + " ERROR: socket.timeout")
        else:
            print(time_now() + " ERROR: Some other error happened")
    else:
        urlGreenData = url.read().decode('utf-8')
        greenEnergyData = json.loads(urlGreenData)

        consW = greenEnergyData["consumption"][0]["wNow"]
        prodW = greenEnergyData["production"][1]["wNow"]
        VoltN = greenEnergyData["consumption"][0]["rmsVoltage"]
        netW = int(float(prodW))-int(float(consW))

        consA = int(float(consW)) / VoltN 
    #excessA = int(float(netW))/VoltN
    # In case, greenEnergyData will contain something like this:
    #   MTU, Time, Power, Cost, Voltage
    #   Solar,11/11/2017 14:20:43,-2.957,-0.29,124.3
    # The only part we care about is -2.957 which is negative
    # kW currently being generated. When 0kW is generated, the
    # negative disappears so we make it optional in the regex
    # below.
    #m = re.search(b'^Solar,[^,]+,-?([^, ]+),', greenEnergyData, re.MULTILINE)
    #remove total car current to work out actual greenEnergyAmpsOffset
        totalAmps = total_amps_actual_all_twcs()
        greenEnergyAmpsOffset = -1*consA + totalAmps + .1 
                #.1 is for slight bias to import ( 100 watts or so)
    # print (totalAmps)
    # print (prodW)
        if(prodW>0):
            #solarW = int(float(m.group(1)) * 1000)
            solarW = int(float(prodW))
        # Use backgroundTasksLock to prevent changing maxAmpsToDivideAmongSlaves
        # if the main thread is in the middle of examining and later using
        # that value.
            backgroundTasksLock.acquire()

        # Watts = Volts * Amps
        # Car charges at 240 volts in North America so we figure
        # out how many amps * 240 = solarW and limit the car to
        # that many amps.
            maxAmpsToDivideAmongSlaves = (solarW / VoltN) + \
                                          greenEnergyAmpsOffset
            if(debugLevel >= 1):
                print("%s: Solar generating %dW so limit car charging to:\n" \
                     "          %.2fA + %.2fA = %.2fA.  Charge when above %.0fA (minAmpsPerTWC)." % \
                     (time_now(), solarW, (solarW / VoltN),
                     greenEnergyAmpsOffset, maxAmpsToDivideAmongSlaves,
                     minAmpsPerTWC))

            backgroundTasksLock.release()
        else:
            print(time_now() +
                " ERROR: Can't determine current solar generation from:\n" +
                str(greenEnergyData))