Skip to content

Commit

Permalink
Octopus free events support (#1647)
Browse files Browse the repository at this point in the history
* Octopus free events support

* Update apps.yaml

* Update givenergy_givtcp.yaml

* Update fox.yaml

* Update ginlong_solis.yaml

* Update givenergy_cloud.yaml

* Update givenergy_ems.yaml

* Update huawei.yaml

* Update luxpower.yaml

* Update sofar.yaml

* Update solaredge.yaml

* Update solax_sx4.yaml

* Update sunsynk.yaml

* [pre-commit.ci lite] apply automatic fixes

* Update energy-rates.md

* [pre-commit.ci lite] apply automatic fixes

* Typo

* [pre-commit.ci lite] apply automatic fixes

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
springfall2008 and pre-commit-ci-lite[bot] authored Nov 23, 2024
1 parent c9eeb5f commit b2aed2c
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# These are supported funding model platforms

custom: ['https://paypal.me/predbat?country.x=GB&locale.x=en_GB']
custom: ['https://paypal.me/predbat?country.x=GB&locale.x=en_GB']
4 changes: 4 additions & 0 deletions apps/predbat/config/apps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
61 changes: 60 additions & 1 deletion apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import asyncio
import json

THIS_VERSION = "v8.7.0"
THIS_VERSION = "v8.7.1"
PREDBAT_FILES = ["predbat.py", "config.py", "prediction.py", "utils.py", "inverter.py", "ha.py", "download.py", "unit_test.py", "web.py", "predheat.py", "futurerate.py"]
from download import predbat_update_move, predbat_update_download, check_install

Expand Down Expand Up @@ -3270,6 +3270,40 @@ def add_now_to_octopus_slot(self, octopus_slots, now_utc):
self.log("Car is charging now - added new IO slot {}".format(slot))
return octopus_slots

def load_free_slot(self, octopus_free_slots, export=False, rate_replicate={}):
"""
Load octopus free session slot
"""
start_minutes = 0
end_minutes = 0

for octopus_free_slot in octopus_free_slots:
start = octopus_free_slot["start"]
end = octopus_free_slot["end"]
rate = octopus_free_slot["rate"]

if start and end:
try:
start = self.str2time(start)
end = self.str2time(end)
except (ValueError, TypeError):
start = None
end = None
self.log("Warn: Unable to decode Octopus free session start/end time")
if start and end:
start_minutes = self.minutes_to_time(start, self.midnight_utc)
end_minutes = min(self.minutes_to_time(end, self.midnight_utc), self.forecast_minutes)

if start_minutes >= 0 and end_minutes != start_minutes and start_minutes < self.forecast_minutes:
self.log("Setting Octopus free session in range {} - {} export {} rate {}".format(self.time_abs_str(start_minutes), self.time_abs_str(end_minutes), export, rate))
for minute in range(start_minutes, end_minutes):
if export:
self.rate_export[minute] = rate
else:
self.rate_import[minute] = rate
self.load_scaling_dynamic[minute] = self.load_scaling_free
rate_replicate[minute] = "saving"

def load_saving_slot(self, octopus_saving_slots, export=False, rate_replicate={}):
"""
Load octopus saving session slot
Expand Down Expand Up @@ -9173,6 +9207,29 @@ def fetch_sensor_data(self):
# Basic rates defined by user over time
self.rate_export = self.basic_rates(self.get_arg("rates_export", [], indirect=False), "rates_export")

# Octopus free session
octopus_free_slots = []
if "octopus_free_session" in self.args:
entity_id = self.get_arg("octopus_free_session", indirect=False)
if entity_id:
events = self.get_state_wrapper(entity_id=entity_id, attribute="events")
if events:
for event in events:
start = event.get("start", None)
end = event.get("end", None)
code = event.get("code", None)
if start and end and code:
start_time = self.str2time(start) # reformat the saving session start & end time for improved readability
end_time = self.str2time(end)
diff_time = start_time - self.now_utc
if abs(diff_time.days) <= 3:
self.log("Octopus free events code {} {}-{}".format(code, start_time.strftime("%a %d/%m %H:%M"), end_time.strftime("%H:%M")))
octopus_free_slot = {}
octopus_free_slot["start"] = start
octopus_free_slot["end"] = end
octopus_free_slot["rate"] = 0
octopus_free_slots.append(octopus_free_slot)

# Octopus saving session
octopus_saving_slots = []
if "octopus_saving_session" in self.args:
Expand Down Expand Up @@ -9251,6 +9308,7 @@ def fetch_sensor_data(self):
self.rate_import, self.rate_import_replicated = self.rate_replicate(self.rate_import, self.io_adjusted, is_import=True)
self.rate_import = self.rate_add_io_slots(self.rate_import, self.octopus_slots)
self.load_saving_slot(octopus_saving_slots, export=False, rate_replicate=self.rate_import_replicated)
self.load_free_slot(octopus_free_slots, export=False, rate_replicate=self.rate_import_replicated)
self.rate_import = self.basic_rates(self.get_arg("rates_import_override", [], indirect=False), "rates_import_override", self.rate_import, self.rate_import_replicated)
self.rate_import = self.rate_scan(self.rate_import, print=True)
else:
Expand Down Expand Up @@ -9763,6 +9821,7 @@ def fetch_config_options(self):
self.load_scaling = self.get_arg("load_scaling")
self.load_scaling10 = self.get_arg("load_scaling10")
self.load_scaling_saving = self.get_arg("load_scaling_saving")
self.load_scaling_free = self.get_arg("load_scaling_free")
self.battery_rate_max_scaling = self.get_arg("battery_rate_max_scaling")
self.battery_rate_max_scaling_discharge = self.get_arg("battery_rate_max_scaling_discharge")

Expand Down
24 changes: 24 additions & 0 deletions docs/energy-rates.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ If you do not have an export tariff then forced export will not apply and Predba
If you do not want Predbat to automatically join Octopus saving sessions and manage your battery activity for the session,
simply delete or comment out the **octopus_saving_session** entry in apps.yaml.

## Octopus free (power up) events

Predbat can automatically detect Octopus free events and adjust your battery plan according.

For Predbat to automatically manage Octopus free sessions the following additional configuration item in apps.yaml is used.

Note: **You must have signed up to Octoplus to benefit from these events**

Like the electricity rates this is set in the apps.yaml template to a regular expression that should auto-discover the Octopus Energy integration.

**octopus_free_session** - Will point to the free event sensor that is exposed by the Octopus Energy Integration. This event sensor contains the dates/times of
all the free events.

```yaml
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'
```
Note: **This event may need to be enabled in Home Assistant first [How to Enable Octopus events](https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/faq/#there-are-entities-that-are-disabled-why-are-they-disabled-and-how-do-i-enable-them)**
If you normally increase your house usage during a free session then you can change **input_number.predbat_load_scaling_free** to allow Predbat to assume an energy
increase in this period. E.g. setting to a value of 1.2 would indicate you will use 20% more energy that normal during this period. (Default is 1.2)
If you do not want Predbat to see these sessions then comment out the **octopus_free_session** setting.
## Octopus Rates URL
If you do not wish to use the Octopus Energy integration and are an Octopus Energy customer then you can configure Predbat to get the electricity rates
Expand Down
4 changes: 4 additions & 0 deletions templates/fox.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
20 changes: 20 additions & 0 deletions templates/ginlong_solis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@ pred_bat:
octopus_ready_time: 're:(time.octopus_energy_([0-9a-z_]+|)_intelligent_target_time)'
octopus_charge_limit: 're:(number.octopus_energy([0-9a-z_]+|)_intelligent_charge_target)'

# Carbon Intensity data from National grid
carbon_intensity: 're:(sensor.carbon_intensity_uk)'

# Example alternative configuration for Ohme integration release >=v0.6.1
#octopus_intelligent_slot: 'binary_sensor.ohme_slot_active'
#octopus_ready_time: 'time.ohme_target_time'
#octopus_charge_limit: 'number.ohme_target_percent'

# Set this to False if you use Octopus Intelligent slot for car planning but when on another tariff e.g. Agile
#octopus_slot_low_rate: False

# Octopus saving session points to the saving session Sensor in the Octopus plugin, when enabled saving sessions will be at the assumed
# Rate is read automatically from the add-in and converted to pence using the conversion rate below (default is 8)
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
4 changes: 4 additions & 0 deletions templates/givenergy_cloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
4 changes: 4 additions & 0 deletions templates/givenergy_ems.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
4 changes: 4 additions & 0 deletions templates/givenergy_givtcp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
21 changes: 16 additions & 5 deletions templates/huawei.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,29 @@ pred_bat:
# Will automatically disable if not found, or comment out to disable fully
# When enabled it overrides the 'car_charging_planned' feature and predict the car charging based on the intelligent plan (unless octopus intelligent charging is False)
# This matches either the intelligent slot from the Octopus Plugin or from the Intelligent plugin

octopus_intelligent_slot: 're:(binary_sensor.octopus_intelligent_slot|re:binary_sensor.octopus_energy_intelligent_dispatching)'
octopus_intelligent_slot: 're:(binary_sensor.octopus_energy_([0-9a-z_]+|)_intelligent_dispatching)'
octopus_ready_time: 're:(time.octopus_energy_([0-9a-z_]+|)_intelligent_target_time)'
octopus_charge_limit: 're:(number.octopus_energy([0-9a-z_]+|)_intelligent_charge_target)'
octopus_charge_limit: 're:(number.octopus_energy_([0-9a-z_]+|)_intelligent_charge_target)'

# Carbon Intensity data from National grid
carbon_intensity: 're:(sensor.carbon_intensity_uk)'

# Example alternative configuration for Ohme integration release >=v0.6.1
#octopus_intelligent_slot: 'binary_sensor.ohme_slot_active'
#octopus_ready_time: 'time.ohme_target_time'
#octopus_charge_limit: 'number.ohme_target_percent'

# Carbon Intensity data from National grid
carbon_intensity: 're:(sensor.carbon_intensity_uk)'
# Set this to False if you use Octopus Intelligent slot for car planning but when on another tariff e.g. Agile
#octopus_slot_low_rate: False

# Octopus saving session points to the saving session Sensor in the Octopus plugin, when enabled saving sessions will be at the assumed
# Rate is read automatically from the add-in and converted to pence using the conversion rate below (default is 8)
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric
Expand Down
4 changes: 4 additions & 0 deletions templates/luxpower.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
4 changes: 4 additions & 0 deletions templates/sofar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
4 changes: 4 additions & 0 deletions templates/solaredge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
4 changes: 4 additions & 0 deletions templates/solax_sx4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down
4 changes: 4 additions & 0 deletions templates/sunsynk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ pred_bat:
octopus_saving_session: 're:(binary_sensor.octopus_energy([0-9a-z_]+|)_saving_session(s|))'
octopus_saving_session_octopoints_per_penny: 8

# Octopus free session points to the free session Sensor in the Octopus plugin
# Note: You must enable this event sensor in the Octopus Integration in Home Assistant for it to work
octopus_free_session: 're:(event.octopus_energy_([0-9a-z_]+|)_octoplus_free_electricity_session_events)'

# Energy rates
# Please set one of these three, if multiple are set then Octopus is used first, second rates_import/rates_export and latest basic metric

Expand Down

0 comments on commit b2aed2c

Please sign in to comment.