def get_BOM_data(): # Get all sites that have vrm id sites = Sesh_Site.objects.exclude(vrm_site_id__isnull=True).exclude(vrm_site_id__exact='') logger.info("Running VRM data collection") for site in sites: logger.debug("Getting VRM data for %s"%site) try: v_client = VictronAPI(site.vrm_account.vrm_user_id,site.vrm_account.vrm_password) if v_client.IS_INITIALIZED: bat_data = v_client.get_battery_stats(int(site.vrm_site_id)) sys_data = v_client.get_system_stats(int(site.vrm_site_id)) #This data is already localazied logger.debug("got raw date %s with timezone %s"%( sys_data['VE.Bus state']['timestamp'], site.time_zone )) date = time_utils.epoch_to_datetime(float(sys_data['VE.Bus state']['timestamp']) , tz=site.time_zone) #logger.debug("saving before localize BOM data point with time %s"%date) logger.debug("saving BOM data point with time %s"%date) mains = False #check if we have an output voltage on inverter input. Indicitave of if mains on if sys_data['Input voltage phase 1']['valueFloat'] > 0: mains = True data_point = BoM_Data_Point( site = site, time = date, soc = bat_data.get('Battery State of Charge (System)',{}).get('valueFloat',0), battery_voltage = bat_data.get('Battery voltage',{}).get('valueFloat',0), AC_Voltage_in = sys_data['Input voltage phase 1']['valueFloat'], AC_Voltage_out = sys_data['Output voltage phase 1']['valueFloat'], AC_input = sys_data['Input power 1']['valueFloat'], AC_output = sys_data['Output power 1']['valueFloat'], AC_output_absolute = float(sys_data['Output power 1']['valueFloat']) + float(sys_data.get('PV - AC-coupled on output L1',{}).get('valueFloat',0)), AC_Load_in = sys_data['Input current phase 1']['valueFloat'], AC_Load_out = sys_data['Output current phase 1']['valueFloat'], inverter_state = sys_data['VE.Bus state']['nameEnum'], pv_production = sys_data.get('PV - AC-coupled on output L1',{}).get('valueFloat',0), #TODO these need to be activated genset_state = 0, main_on = mains, relay_state = 0, ) with transaction.atomic(): data_point.save() # Send to influx send_to_influx(data_point, site, date, to_exclude=['time']) # Alert if check(data_point) fails except IntegrityError, e: logger.debug("Duplicate entry skipping data point") pass except KeyError, e: logger.warning("Sites %s is missing key while getting vrm data%s"%(site,e))
def get_enphase_daily_stats(date=None): """ Get enphase daily data or get aggregate data """ calc_range = timedelta(minutes=15) #create enphaseAPI sites = Sesh_Site.objects.all() #return 'The test task executed with argument "%s" ' % param #get dates we want to get datetime_now = datetime.now() datetime_start = datetime_now - timedelta(days=1) system_results = {} if date: datetime_now = date datetime_start = datetime_now - timedelta(days=1) #turn them into epoch seconds datetime_start_epoch = time_utils.get_epoch_from_datetime(datetime_start) for site in sites: en_client = EnphaseAPI(settings.ENPHASE_KEY,site.enphase_ID) system_id = site.enphase_site_id print "gettig stats for %s"%system_id system_results = en_client.get_stats(system_id,start=datetime_start_epoch) #TODO handle exception of empty result print len(system_results['intervals']) for interval in system_results['intervals']: #store the data print interval end_time_str = time_utils.epoch_to_datetime(interval['end_at']) system_pv_data = PV_Production_Point( site = site, time = end_time_str, wh_production = interval['enwh'], w_production = interval['powr'], #TODO time interval shouldn't be static this needs to be calculated based on data returned, data_duration = calc_range ) system_pv_data.save() return "updated enphase data %s"%site
def get_enphase_daily_stats(date=None): """ Get enphase daily data or get aggregate data """ calc_range = timedelta(minutes=15) #create enphaseAPI sites = Sesh_Site.objects.all() #return 'The test task executed with argument "%s" ' % param #get dates we want to get default this is last 24 hours datetime_now = datetime.now() datetime_start = datetime_now - timedelta(days=1) system_results = {} if date: datetime_now = date datetime_start = datetime_now - timedelta(days=1) #turn them into epoch seconds datetime_start_epoch = time_utils.get_epoch_from_datetime(datetime_start) for site in sites: en_client = EnphaseAPI(settings.ENPHASE_KEY, site.enphase_ID) system_id = site.enphase_site_id #print "gettig stats for %s"%system_id system_results = en_client.get_stats(system_id, start=datetime_start_epoch) #TODO handle exception of empty result #print len(system_results['intervals']) for interval in system_results['intervals']: #store the data #print interval end_time_str = time_utils.epoch_to_datetime(interval['end_at']) system_pv_data = PV_Production_Point( site=site, time=end_time_str, wh_production=interval['enwh'], w_production=interval['powr'], #TODO time interval shouldn't be static this needs to be calculated based on data returned, data_duration=calc_range) system_pv_data.save() return "updated enphase data %s" % site
def get_BOM_data(): """ Get data related to system voltage, SoC, battery voltage through Victro VRM portal """ sites = Sesh_Site.objects.all() for site in sites: v_client = VictronAPI(site.vrm_user_id,site.vrm_password) #TODO figure out a way to get these automatically or add #them manually to the model for now #Also will the user have site's under thier account that they wouldn't like to pull data form? #This will throw an error when the objects are getting created for site_id in v_client.SYSTEMS_IDS: print "##### system id's %s out of %s"%(site_id,v_client.SYSTEMS_IDS) bat_data = v_client.get_battery_stats(site_id[0]) sys_data = v_client.get_system_stats(site_id[0]) date = time_utils.epoch_to_datetime(sys_data['VE.Bus state']['timestamp'] ) print bat_data.keys() print sys_data data_point = BoM_Data_Point( site = site, time = date, soc = bat_data['Battery State of Charge (System)']['valueFloat'], battery_voltage = bat_data['Battery voltage']['valueFloat'], AC_input = sys_data['Input power 1']['valueFloat'], AC_output = sys_data['Output power 1']['valueFloat'], AC_Load_in = sys_data['Input current phase 1']['valueFloat'], AC_Load_out = sys_data['Output current phase 1']['valueFloat'], inverter_state = sys_data['VE.Bus state']['nameEnum'], #TODO these need to be activated genset_state = "off", relay_state = "off", ) data_point.save() #TODO get bulk historical data with enphase and weather print "BoM Data saved"
def get_BOM_data(): # Get all sites that have vrm id sites = Sesh_Site.objects.exclude(vrm_site_id__isnull=True).exclude( vrm_site_id__exact='') logger.info("Running VRM data collection") for site in sites: logger.debug("Getting VRM data for %s" % site) v_client = VictronAPI(site.vrm_account.vrm_user_id, site.vrm_account.vrm_password) bat_data = {} sys_data = {} pv_data = {} try: if v_client.IS_INITIALIZED: if site.has_batteries: bat_data = v_client.get_battery_stats(int( site.vrm_site_id)) if site.has_pv: pv_data = v_client.get_pv_stats(int(site.vrm_site_id)) sys_data = v_client.get_system_stats(int(site.vrm_site_id)) #This data is already localazied logger.debug("got raw date %s with timezone %s" % (sys_data.get('VE.Bus state', {}).get( 'timestamp', 0), site.time_zone)) date = time_utils.epoch_to_datetime(float( sys_data.get('VE.Bus state', {}).get('timestamp', 0)), tz=site.time_zone) if not date: date = timezone.now() #logger.debug("saving before localize BOM data point with time %s"%date) data_point = BoM_Data_Point(site=site, time=date) logger.debug("saving BOM data point with time %s" % date) mains = False #check if we have an output voltage on inverter input. Indicitave of if mains on if sys_data.get('Input voltage phase 1', {}).get('valueFloat') > 0: mains = True data_point = BoM_Data_Point(site=site, time=date) data_point.soc = bat_data.get( 'Battery State of Charge (System)', {}).get('valueFloat', 0) data_point.battery_voltage = bat_data.get( 'Battery voltage', {}).get('valueFloat', 0) data_point.battery_current = bat_data.get( 'Battery current', {}).get('valueFloat', 0) data_point.AC_Voltage_in = sys_data.get( 'Input voltage phase 1', {}).get('valueFloat', 0) data_point.AC_Voltage_out = sys_data.get( 'Output voltage phase 1', {}).get('valueFloat', 0) data_point.AC_input = sys_data.get('Input power 1', {}).get('valueFloat', 0) data_point.AC_output = sys_data.get('Output power 1', {}).get('valueFloat', 0) data_point.AC_output_absolute = float( sys_data.get('Output power 1', {}).get('valueFloat', 0) + float( sys_data.get('PV - AC-coupled on output L1', {}).get( 'valueFloat', 0))) data_point.AC_Load_in = sys_data.get('Input current phase 1', {}).get('valueFloat', 0) data_point.AC_Load_out = sys_data.get('Output current phase 1', {}).get('valueFloat', 0) data_point.inverter_state = sys_data.get('VE.Bus state', {}).get( 'nameEnum', '') data_point.relay_state = 0 # Does the ste have PV? if site.has_pv: # AC coupled or DC coupled if sys_data.get('PV - AC-coupled on output L1', {}).get('valueFloat', 0): data_point.pv_production = sys_data.get( 'PV - AC-coupled on output L1', {}).get('valueFloat', 0) # For AC coupled systems else: data_point.pv_production = pv_data.get( 'PV - DC-coupled', {}).get('valueFloat', 0) #TODO these need to be activated data_point.genset_state = 0 data_point.main_on = mains with transaction.atomic(): data_point.save() # Send to influx send_to_influx(data_point, site, date, to_exclude=['time']) except IntegrityError, e: logger.debug("Duplicate entry skipping data point") pass except Exception, e: message = "error with geting site %s data exception %s" % (site, e) logger.exception("error with geting site %s data exception" % site) handle_task_failure(message=message, exception=e, name='get_BOM_data') pass