Selbstbau-PV / Selbstbau-PV-Hoymiles-nulleinspeisung-mit-OpenDTU-und-Shelly3EM

Ein Python-Skript, das den aktuellen Hausverbrauch aus einem Shelly 3EM ausliest, die Nulleinspeisung berechnet und die Ausgangsleistung eines Hoymiles-Wechselrichters mit Hilfe der OpenDTU entsprechend anpasst. Somit wird kein unnötiger Strom ins Betreibernetz abgegeben.
61 stars 24 forks source link

Corrections #25

Open s118 opened 7 months ago

s118 commented 7 months ago

The code doesn't work. I pass the code with corrections, although the comments are in Spanish. I have added the "exportar" variable, in case you want to export some energy to the grid. If you do not want to export anything, exportar=0

!/usr/bin/env python3

import requests, time, sys from requests.auth import HTTPBasicAuth

Ajustar estos datos

serial = "xxxxxxxxxxxxxxx" # numero serie microinversor maximum_wr = 2000 # Máxima salida del microinversor minimum_wr = 100 # Minima salida del microinversor dtu_ip = 'xxxxxxxxxxxx' # IP Adresse OpenDTU basicdtu = HTTPBasicAuth('admin', 'xxxxxxxx') dtu_user = 'admin' # usuario openDTU dtu_pass = 'xxxxxxxxxxx' # OpenDTU Password basicShelly = HTTPBasicAuth('admin', 'xxxxxxx') shelly_ip = 'xxxxxxxxxx' # IP Adresse Shelly 3EM grid= 0 power = 0 reachable = 0 exportar = 1500 valor_seguridad = 0 while True: try:

Toma de datos de la API REST de openDTU y los convierte en json-Format

    r = requests.get(url = f'http://{dtu_ip}/api/livedata/status/inverters', headers={'Content-Type': 'application/json'},auth=basicdtu).json()
    # Selecciona los datos especificos de la respuesta json
    reachable = r['inverters'][0]['reachable'] # es accesible la DTU?
    producing = int(r['inverters'][0]['producing']) # El microinversor está produciendo?
    viejo_limite = int(r['inverters'][0]['limit_absolute']) # Viejo límite
    power = r['total']['Power']['v'] # Entrega de corriente alterna en W
except:
    print('Error al recuperar datos de openDTU')
try:
    # Toma datos de Shelly 3EM Rest API y los traduce a formato json
    grid = requests.get(f'http://{shelly_ip}/emeter/2', headers={'Content-Type': 'application/json'},auth=basicShelly).json()['power']
except:
    print('Error al recuperar datos de Shelly 3EM')
# Establecer valores
print(f'\nRed: {round(grid, 1)} W, Produccion: {round(power, 1)} W,  Casa: {round(grid + power, 1)} W')
if reachable:
    setpoint = grid + viejo_limite - valor_seguridad + exportar # Nuevos límites en W
    # Límite superior de captura
    if setpoint > maximum_wr:
        setpoint = maximum_wr
        print(f'Estableciendo Maximo: {maximum_wr} W')
    # Límite inferior de captura
    elif setpoint < minimum_wr:
        setpoint = minimum_wr
        print(f'Estableciendo Minimo: {minimum_wr} W')
    else:
        print(f'Ajuste Calculado: {round(grid, 1)} W + {round(viejo_limite, 1)} W - valor_seguridad W = {round(setpoint, 1)} W')
    if setpoint != viejo_limite:
        print(f'Establecer el límite del inversor de {round(viejo_limite, 1)} W a {round(setpoint, 1)} W... ', end='')
        # Establecer un nuevo límite
        try:
            r = requests.post(
                url = f'http://{dtu_ip}/api/limit/config',
                data = f'data={{"serial":"{serial}", "limit_type":0, "limit_value":{setpoint}}}',
                auth = HTTPBasicAuth(dtu_user, dtu_pass),
                headers = {'Content-Type': 'application/x-www-form-urlencoded'}
            )
            print(f'Configuracion enviada ({r.json()["type"]})')
        except:
            print('Error al enviar la configuracion')
sys.stdout.flush() # escribir mensajes almacenados en caché en stdout
time.sleep(5) # esperar
Selbstbau-PV commented 7 months ago

The script works only with opnedtu lower than V24.1.26

greymda commented 6 months ago

@s118 what opendtu version are you on? thanks for the updated script.

s118 commented 6 months ago

Hy. When I modified the original code, it was on version v24.2.12. I am currently on version v24.2.16 and everything is working without problems. Tomorrow I will test new versions, and in case of news, I will communicate them.

greymda commented 6 months ago

Hy. When I modified the original code, it was on version v24.2.12. I am currently on version v24.2.16 and everything is working without problems. Tomorrow I will test new versions, and in case of news, I will communicate them.

@s118 have you had a chance to test it? cheers

s118 commented 6 months ago

Hello. Today I tested it, and although it was a rainy day, the code worked perfectly. Attached graph that represents the percentage of the microinverter's work limit throughout the day. The production of the microinverter (HOYMILES HMS-2000) is also represented. Captura