Skip to content

Commit

Permalink
add ability to set start and end limits when searching today
Browse files Browse the repository at this point in the history
  • Loading branch information
badguy99 committed Apr 13, 2020
1 parent 6c1f72f commit 7e64457
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ octo_block_90minutes:
class: OctoBlock
region: H
hour: 1.5
start_period: now
start_period: today
use_timezone: True
limits:
start_time: '07:00'
end_time: '16:00'
```
The module and class sections need to remain as above, other sections should be changed as required.
Expand All @@ -56,6 +59,9 @@ The module and class sections need to remain as above, other sections should be
| use_timezone | Yes | True |
| import | Yes | True |
| export | Yes | False |
| limits | | |
| start_time | Yes | '07:00' |
| end_time | Yes | '16:00' |
You can have multiple blocks with different time periods (`hour` setting) or starting points (`start_period` setting) as needed. It will work with whole hour or half hour blocks in the `hour` setting.

Expand All @@ -79,6 +85,8 @@ Using `today` `start_period` this has only turned on once during the day

Setting `start_period` to `now` and `hours` to `0` will give the current import or export price.

When using `today` for the `start_period` it can be limited further usings `limits > start_time` and/or `limits > end_time` (please note the formating in the example yaml above) to restrict the period searched. This may be useful for example if you have something that you only want to run within certain times of the day, due to noise issues etc.

`use_timezone` can be set to True or False, and defaults to False, it allows you to specify if the date/time should be displayed in UTC (False), or using Europe/London (True) as the timezone. For example, `2020-03-29T02:00:00Z` or `2020-03-29T03:00:00 BST` respectively.

`import` and `export` should be set to True or False as required, `import = True` and `export = False` for the Agile Octopus tariff and `import = False` and `export = True` for the Agile Outgoing Octopus tariff.
Expand Down
61 changes: 50 additions & 11 deletions apps/octoblock/octoblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def period_and_cost_callback(self, kwargs):
region = self.args.get('region', 'H')
start_period = self.args.get('start_period', 'now')
start_period = str(start_period).lower()
limits = self.args.get('limits', None)
limit_start = None
limit_end = None
if limits:
limit_start = limits.get('start_time', None)
limit_end = limits.get('end_time', None)
self.use_timezone = self.args.get('use_timezone', False)
self.incoming = self.args.get('import', True)
self.outgoing = self.args.get('export', False)
Expand All @@ -34,7 +40,26 @@ def period_and_cost_callback(self, kwargs):
now = datetime.datetime.now()
if start_period == 'today':
if now.time() < datetime.time(23, 30, 0):
d = datetime.date.today().isoformat() + 'T00:00:00'
if limit_end:
try:
datetime.datetime.strptime(limit_end, "%H:%M")
except ValueError:
self.log('end_time not in correct HH:MM format',
level='ERROR')

limit_end = (datetime.date.today().isoformat() + 'T' +
limit_end + ':00')
if limit_start:
try:
datetime.datetime.strptime(limit_start, "%H:%M")
except ValueError:
self.log('start_time not in correct HH:MM format',
level='ERROR')

d = (datetime.date.today().isoformat() + 'T' +
limit_start + ':00')
else:
d = datetime.date.today().isoformat() + 'T00:00:00'
else:
d = ((datetime.date.today() +
datetime.timedelta(days=1)).isoformat() +
Expand All @@ -47,7 +72,7 @@ def period_and_cost_callback(self, kwargs):
' defaulting to "now"')
d = now.isoformat()

self.get_period_and_cost(region, d)
self.get_period_and_cost(region, d, limit_end)

hours = str(self.hours).replace(".", "_")
if self.incoming:
Expand Down Expand Up @@ -79,18 +104,32 @@ def period_and_cost_callback(self, kwargs):
attributes={'unit_of_measurement': 'p/kWh',
'icon': 'mdi:flash-outline'})

def get_period_and_cost(self, region, timeperiod):
def get_period_and_cost(self, region, timeperiod, timeperiodend):
baseurl = 'https://api.octopus.energy/v1/products/'
if self.incoming:
r = requests.get(
baseurl + 'AGILE-18-02-21/electricity-tariffs/' +
'E-1R-AGILE-18-02-21-' + str(region).upper() +
'/standard-unit-rates/?period_from=' + timeperiod)
if not timeperiodend:
r = requests.get(
baseurl + 'AGILE-18-02-21/electricity-tariffs/' +
'E-1R-AGILE-18-02-21-' + str(region).upper() +
'/standard-unit-rates/?period_from=' + timeperiod)
else:
r = requests.get(
baseurl + 'AGILE-18-02-21/electricity-tariffs/' +
'E-1R-AGILE-18-02-21-' + str(region).upper() +
'/standard-unit-rates/?period_from=' + timeperiod +
'&period_to=' + timeperiodend)
elif self.outgoing:
r = requests.get(
baseurl + 'AGILE-OUTGOING-19-05-13/electricity-tariffs/' +
'E-1R-AGILE-OUTGOING-19-05-13-' + str(region).upper() +
'/standard-unit-rates/?period_from=' + timeperiod)
if not timeperiodend:
r = requests.get(
baseurl + 'AGILE-OUTGOING-19-05-13/electricity-tariffs/' +
'E-1R-AGILE-OUTGOING-19-05-13-' + str(region).upper() +
'/standard-unit-rates/?period_from=' + timeperiod)
else:
r = requests.get(
baseurl + 'AGILE-OUTGOING-19-05-13/electricity-tariffs/' +
'E-1R-AGILE-OUTGOING-19-05-13-' + str(region).upper() +
'/standard-unit-rates/?period_from=' + timeperiod +
'&period_to=' + timeperiodend)

tariff = json.loads(r.text)
tariffresults = tariff[u'results']
Expand Down

0 comments on commit 7e64457

Please sign in to comment.