Skip to content

Instantly share code, notes, and snippets.

@9000cats
Created April 20, 2024 22:48
Show Gist options
  • Save 9000cats/6e5b447a5e56dd6e5fef8691dc6d6a53 to your computer and use it in GitHub Desktop.
Save 9000cats/6e5b447a5e56dd6e5fef8691dc6d6a53 to your computer and use it in GitHub Desktop.
Enphase Einstein Local API (Production Meter Reading) to InfluxDB 2.x
import json
import requests
from time import sleep
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
import os
import urllib3
# Disable SSL warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Give the Python console window a title (I'm running this on Windows)
os.system('title Enphase 2 InfluxDB')
# InfluxDB configuration
org = '{InfluxDB Org}'
bucket = '{InfluxDB bucket name}'
token = '{InfluxDB API Token}'
client = InfluxDBClient(url="https://localhost:8086", token=token, verify_ssl=False)
write_api = client.write_api(write_options=SYNCHRONOUS)
# Enphase IQ Gateway configuration
envoy_ip = '{Enphase Envoy IP Address}' # Could also use envoy.local instead of IP
envoy_url = f'http://{envoy_ip}/ivp/meters/readings'
# Pre-generated access token, good for one year
# Get a token here: https://entrez.enphaseenergy.com/
# Start typing your site name into the select system box if it's empty
access_token = '{Enphase Entrez API Auth Token}'
def push_data_point(series_name, data):
point_val = {
"measurement": series_name,
"fields": data
}
write_api.write(bucket, org, point_val)
last_production_time = 0
while True:
headers = {
'Accept': 'application/json',
'Authorization': f'Bearer {access_token}'
}
try:
response = requests.get(envoy_url, headers=headers, verify=False)
if response.status_code == 200:
data = response.json()
if data and len(data) > 0:
production_meter = data[0]
reading_time = production_meter['timestamp']
watts_now = production_meter['instantaneousDemand']
print("=" * 40)
print(f"Time: {reading_time}")
print(f"Current Production: {watts_now:.2f} W")
if watts_now > 0:
if reading_time > last_production_time:
production_eim_data = {
"readingTime": reading_time,
"wNow": watts_now
}
push_data_point("productionEimData", production_eim_data)
last_production_time = reading_time
print("Data pushed to InfluxDB successfully.")
else:
print("No production data available.")
print("=" * 40)
else:
print("Error: No meter data found in the response.")
else:
print(f"Error: {response.status_code}")
print(f"Response content: {response.text}")
sleep(30)
except Exception as e:
print(f"Error: {e}")
sleep(15)
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment