Skip to content

Commit

Permalink
Merge pull request #8 from DragonQ/yanson-export
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanson authored May 18, 2024
2 parents 68bb26c + 6b2a014 commit 6ac8f1b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ If you have not built the image locally, change the image and tag to one from [D

## Known Issues

* Import meters have not been tested and are not supported.
* The cubic meter to kWh conversion used fixed values whereas they actually fluctuate (see link below).

## Links
Expand Down
23 changes: 13 additions & 10 deletions app/octopus_to_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,39 @@ def _process_emp(self, emp, collect_from: datetime, collect_to: datetime, base_t
if self._included_meters and em['serial_number'] not in self._included_meters:
click.echo(f'Skipping electricity meter {em['serial_number']} as it is not in octopus.included_meters')
else:
self._process_em(emp['mpan'], em, collect_from, collect_to, standard_unit_rates, standing_charges, tags)
self._process_em(emp['mpan'], em, emp['is_export'], collect_from, collect_to, standard_unit_rates, standing_charges, tags)

def _process_em(self, mpan: str, em, collect_from: datetime, collect_to: datetime, standard_unit_rates, standing_charges, base_tags: dict[str, str]):
def _process_em(self, mpan: str, em, is_export: bool, collect_from: datetime, collect_to: datetime, standard_unit_rates, standing_charges, base_tags: dict[str, str]):
tags = base_tags.copy()
click.echo(f'Processing electricity meter: {em["serial_number"]}')
if 'meter_serial_number' in self._included_tags:
tags['meter_serial_number'] = em['serial_number']
last_interval_start = DateUtils.iso8601(collect_to - timedelta(minutes=self._resolution_minutes))
data = self._octopus_api.retrieve_electricity_consumption(mpan, em['serial_number'], DateUtils.iso8601(collect_from), last_interval_start)
consumption = self._series_maker.make_series(data)
self._store_em_consumption(consumption, standard_unit_rates, standing_charges, tags)
self._store_em_consumption(is_export, consumption, standard_unit_rates, standing_charges, tags)

def _store_em_consumption(self, consumption: dict, standard_unit_rates, standing_charges, base_tags: [str, str]):
self._store_consumption('electricity_consumption', consumption, standard_unit_rates, standing_charges, base_tags)
def _store_em_consumption(self, is_export: bool, consumption: dict, standard_unit_rates, standing_charges, base_tags: [str, str]):
self._store_consumption('electricity_consumption', consumption, standard_unit_rates, is_export, standing_charges, base_tags)

def _store_gm_consumption(self, consumption: dict, standard_unit_rates, standing_charges, base_tags: [str, str]):
self._store_consumption('gas_consumption', consumption, standard_unit_rates, standing_charges, base_tags)
self._store_consumption('gas_consumption', consumption, standard_unit_rates, False, standing_charges, base_tags)

def _store_consumption(self, measurement: str, consumption: dict, standard_unit_rates, standing_charges, base_tags: [str, str]):
def _store_consumption(self, measurement: str, consumption: dict, standard_unit_rates, export_meter: bool, standing_charges, base_tags: [str, str]):
points = []
net_modifier = 1.0
if export_meter:
net_modifier = -1.0
for t, c in consumption.items():
usage_cost_exc_vat_pence = standard_unit_rates[t]['value_exc_vat'] * c['consumption']
usage_cost_inc_vat_pence = standard_unit_rates[t]['value_inc_vat'] * c['consumption']
usage_cost_exc_vat_pence = standard_unit_rates[t]['value_exc_vat'] * c['consumption'] * net_modifier
usage_cost_inc_vat_pence = standard_unit_rates[t]['value_inc_vat'] * c['consumption'] * net_modifier
standing_charge_cost_exc_vat_pence = standing_charges[t]['value_exc_vat'] / (60 * 24 / self._resolution_minutes)
standing_charge_cost_inc_vat_pence = standing_charges[t]['value_inc_vat'] / (60 * 24 / self._resolution_minutes)
points.append(Point.from_dict({
'measurement': measurement,
'time': t,
'fields': {
'usage_kwh': c['consumption'],
'usage_kwh': c['consumption'] * net_modifier,
'usage_cost_exc_vat_pence': usage_cost_exc_vat_pence,
'usage_cost_inc_vat_pence': usage_cost_inc_vat_pence,
'standing_charge_cost_exc_vat_pence': standing_charge_cost_exc_vat_pence,
Expand Down

0 comments on commit 6ac8f1b

Please sign in to comment.