def run_once(settings, city): global last_eday_kwh # Check if we only want to run during daylight if city: now = datetime.time(datetime.now()) if now < city.dawn().time() or now > city.dusk().time(): logging.debug("Skipped upload as it's night") return # Fetch the last reading from GoodWe gw = gw_api.GoodWeApi(settings.gw_station_id, settings.gw_account, settings.gw_password) data = gw.getCurrentReadings() # Check if we want to abort when offline if settings.skip_offline: if data['status'] == 'Offline': logging.debug("Skipped upload as the inverter is offline") return # Append reading to CSV file if settings.csv: if data['status'] == 'Offline': logging.debug("Don't append offline data to CSV file") else: locale.setlocale(locale.LC_ALL, locale.getlocale()) csv = gw_csv.GoodWeCSV(settings.csv) csv.append(data) # Submit reading to PVOutput, if they differ from the previous set eday_kwh = data['eday_kwh'] if data['pgrid_w'] == 0 and abs(eday_kwh - last_eday_kwh) < 0.001: logging.debug("Ignore unchanged reading") else: last_eday_kwh = eday_kwh temperature = get_temperature(settings, data['latitude'], data['longitude']) if temperature: logging.info( "Current local temperature is {:.1f} °C".format(temperature)) data['temperature'] = temperature voltage = data['grid_voltage'] if settings.pv_voltage: voltage = data['pv_voltage'] if settings.pvo_system_id and settings.pvo_api_key: pvo = pvo_api.PVOutputApi(settings.pvo_system_id, settings.pvo_api_key) pvo.add_status(data['pgrid_w'], last_eday_kwh, data.get('temperature'), voltage) else: logging.debug(str(data)) logging.warning("Missing PVO id and/or key")
def run_once(args): global last_eday_kwh # Check if we only want to run during daylight if args.city: a = Astral() sun = a[args.city].sun(local=True) now = datetime.time(datetime.now()) if now < sun['dawn'].time() or now > sun['dusk'].time(): logging.debug("Skipped upload as it's night") return # Fetch the last reading from GoodWe gw = gw_api.GoodWeApi(args.gw_station_id, args.gw_account, args.gw_password) data = gw.getCurrentReadings() # Check if we want to abort when offline if args.skip_offline: if data['status'] == 'Offline': logging.debug("Skipped upload as the inverter is offline") return # Append reading to CSV file if args.csv: if data['status'] == 'Offline': logging.debug("Don't append offline data to CSV file") else: locale.setlocale(locale.LC_ALL, locale.getlocale()) csv = gw_csv.GoodWeCSV(args.csv) csv.append(data) # Submit reading to PVOutput, if they differ from the previous set eday_kwh = data['eday_kwh'] if data['pgrid_w'] == 0 and abs(eday_kwh - last_eday_kwh) < 0.001: logging.debug("Ignore unchanged reading") else: last_eday_kwh = eday_kwh if args.darksky_api_key: ds = ds_api.DarkSkyApi(args.darksky_api_key) data['temperature'] = ds.get_temperature(data['latitude'], data['longitude']) voltage = data['grid_voltage'] if args.pv_voltage: voltage = data['pv_voltage'] pvo = pvo_api.PVOutputApi(args.pvo_system_id, args.pvo_api_key) pvo.add_status(data['pgrid_w'], last_eday_kwh, data.get('temperature'), voltage)
def run_once(settings, city): global last_eday_kwh global last_energy_used # Check if we only want to run during daylight if city: now = datetime.time(datetime.now()) # if now < city.dawn().time() or now > city.dusk().time(): # logging.debug("Skipped upload as it's night") # return if settings.mqtt_host: if settings.gw_station_id: logging.error( "Bad configuration options. Choose either Goodwe or MQTT as source for inverter data. Both cannot be used simultaniously." ) sys.exit(1) # Fetch the latest reading from MQTT broker mqtt_broker = mqtt.MQTT(settings.telegram_token, settings.telegram_chatid, settings.mqtt_host, settings.mqtt_port, settings.mqtt_user, settings.mqtt_password, settings.mqtt_topic) data = mqtt_broker.getCurrentReadings() elif settings.gw_station_id: # Fetch the last reading from GoodWe goodwe = gw_api.GoodWeApi(settings.gw_station_id, settings.gw_account, settings.gw_password) data = goodwe.getCurrentReadings() # Check if we want to abort when offline if settings.skip_offline: if data['status'] == 'Offline': logging.debug("Skipped upload as the inverter is offline") return # Append reading to CSV file if settings.csv: if data['status'] == 'Offline': logging.debug("Don't append offline data to CSV file") else: locale.setlocale(locale.LC_ALL, locale.getlocale()) csv = gw_csv.GoodWeCSV(settings.csv) csv.append(data) # Submit reading to PVOutput, if they differ from the previous set eday_kwh = data['eday_kwh'] energy_used = data['energy_used'] if data['pgrid_w'] == 0 and abs(eday_kwh - last_eday_kwh) < 0.001: logging.debug("Ignore unchanged eday_kwh reading") else: last_eday_kwh = eday_kwh if data['load'] == 0 and abs(energy_used - last_energy_used) < 0.001: logging.debug("Ignore unchanged energy_used reading") else: last_energy_used = energy_used # Get the temperature if pulling data from GoodWe if settings.gw_station_id: temperature = get_temperature(settings, data['latitude'], data['longitude']) if temperature: data['temperature'] = temperature voltage = data['grid_voltage'] if settings.pv_voltage: voltage = data['pv_voltage'] if settings.pvo_system_id and settings.pvo_api_key: pvo = pvo_api.PVOutputApi(settings.telegram_token, settings.telegram_chatid, settings.pvo_system_id, settings.pvo_api_key) pvo.add_status(data['pgrid_w'], last_eday_kwh, data.get('temperature'), voltage, data['energy_used'], data['load']) else: logging.debug(str(data)) logging.warning("Missing PVO id and/or key")
def run_once(settings, city): global last_eday_kwh # Check if we only want to run during daylight if city: now = datetime.time(datetime.now()) if now < city.dawn().time() or now > city.dusk().time(): logging.debug("Skipped upload as it's night") return # Fetch the last reading from GoodWe gw = gw_api.GoodWeApi(settings.gw_station_id, settings.gw_account, settings.gw_password) data = gw.getCurrentReadings() # Check if we want to abort when offline if settings.skip_offline: if data['status'] == 'Offline': logging.debug("Skipped upload as the inverter is offline") return # Append reading to CSV file if settings.csv: if data['status'] == 'Offline': logging.debug("Don't append offline data to CSV file") else: locale.setlocale(locale.LC_ALL, locale.getlocale()) csv = gw_csv.GoodWeCSV(settings.csv) csv.append(data) # Submit reading to PVOutput, if they differ from the previous set eday_kwh = data['eday_kwh'] if data['pgrid_w'] == 0 and abs(eday_kwh - last_eday_kwh) < 0.001: logging.debug("Ignore unchanged reading") else: last_eday_kwh = eday_kwh temperature = get_temperature(settings, data['latitude'], data['longitude']) if temperature: logging.info( "Current local temperature is {:.1f} °C".format(temperature)) data['temperature'] = temperature voltage = data['grid_voltage'] vpv1 = data['vpv1'] ipv1 = data['ipv1'] vpv2 = data['vpv2'] ipv2 = data['ipv2'] if settings.pv_voltage: voltage = data['pv_voltage'] if settings.pvo_system_id and settings.pvo_api_key: pvo = pvo_api.PVOutputApi(settings.pvo_system_id, settings.pvo_api_key) pvo.add_status(data['pgrid_w'], last_eday_kwh, data.get('temperature'), voltage, vpv1, ipv1, vpv2, ipv2) else: logging.debug(str(data)) logging.warning("Missing PVO id and/or key") # Write to Influxdb if settings.influx: logging.debug('Logging to influxdb') # updateDate = data['last_refresh_time'] updateDate = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') from influxdb import InfluxDBClient json_body = [{ "measurement": settings.influx_measurement, "tags": { "deviceId": settings.gw_station_id }, "time": updateDate, "fields": { "DC_Voltage_PV1": float(vpv1), "DC_Voltage_PV2": float(vpv2), "DC_Current1": float(ipv1), "DC_Current2": float(ipv2), "AC_Voltage": float(voltage), "AC_Current": float(data['iac1']), "AC_Power": float(data['pgrid_w']), "AC_Frequency": float(data['fac1']), "Inverter_Temperature": float(data['inv_temperature']), "Daily_Generation": float(data['eday_kwh']), #"Monthly_Generation": float(data['emonth']), #"Annual_Generation": float(Annual_Generation), #"updateDate": int(updateDate), "Total_Generation": float(data['etotal_kwh']), #"Generation_Last_Month": float(Generation_Last_Month), } }] client = InfluxDBClient(host=settings.influx_server, port=settings.influx_port, username=settings.influx_user, password=settings.influx_password) client.switch_database(settings.influx_database) success = client.write_points(json_body, time_precision='ms') if not success: logging.warning('Error writing to influx database')