forked from dracoventions/TWCManager
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New picing module for Spanish PVPC model #237
Open
juanjoqg
wants to merge
20
commits into
ngardiner:pricing_modules
Choose a base branch
from
juanjoqg:pricing_modules
base: pricing_modules
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dfa6109
Add files via upload
ngardiner 7ac3cc2
Bump changelog and screenshot
ngardiner 69903d5
Removed confusing statement from WebUI
ngardiner 216754f
Merge branch 'v1.2.1' of https://github.com/ngardiner/TWCManager into…
ngardiner 5a7c7f2
Provide the ability to override the stored API bearer/refresh tokens …
ngardiner d9170a3
Update Tesla API authentication to work with oAuth2 flow (no MFA supp…
ngardiner 3ee4dfb
Remove spoofed UA headers from requests (not needed), standardise var…
ngardiner 872cf7e
Black on TeslaAPI.py
MikeBishop e654722
New feature that allows to limit the max power TWC will take from the…
juanjoqg 0825b6d
Avoid using a hardcode path for the PID file, take the path from conf…
juanjoqg 4e781c0
Rollback to the namespace remove
juanjoqg 5aec8ff
Bug fix in the limit amps from the grid integration with track green …
juanjoqg 8666768
Change to ensure it just limit the amps from the grid when the right …
juanjoqg 3514c10
New menu Graphs, it allows to represent energy graphs base on the SQL…
juanjoqg 32f6d41
Change step on the graphs from seconds to minutes
juanjoqg 6c7921d
New picing module for Spanish PVPC model, i adds a new schedule char…
juanjoqg 8a4ee41
Merge remote-tracking branch 'origin/limit_amps_from_grid' into prici…
juanjoqg 729a0bd
Merge remote-tracking branch 'origin/energy_graphs' into pricing_modules
juanjoqg 4f78ad9
New feature related with the pricing modules that allows to schedule …
juanjoqg e676a37
New features related to the pricing modules, schedule charging relate…
juanjoqg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
from datetime import datetime | ||
from datetime import timedelta | ||
|
||
class PVPCesPricing: | ||
|
||
import requests | ||
import time | ||
|
||
# https://www.esios.ree.es/es/pvpc publishes at 20:30CET eveyday the prices for next day | ||
# There is no limitation to fetch prices as it's updated onces a day | ||
cacheTime = 1 | ||
config = None | ||
configConfig = None | ||
configPvpc = None | ||
exportPrice = 0 | ||
fetchFailed = False | ||
importPrice = 0 | ||
lastFetch = 0 | ||
status = False | ||
timeout = 10 | ||
headers = {} | ||
todayImportPrice = {} | ||
|
||
def __init__(self, master): | ||
|
||
self.master = master | ||
self.config = master.config | ||
try: | ||
self.configConfig = master.config["config"] | ||
except KeyError: | ||
self.configConfig = {} | ||
|
||
try: | ||
self.configPvpc = master.config["pricing"]["PVPCes"] | ||
except KeyError: | ||
self.configPvpc = {} | ||
|
||
self.status = self.configPvpc.get("enabled", self.status) | ||
self.debugLevel = self.configConfig.get("debugLevel", 0) | ||
|
||
token=self.configPvpc.get("token") | ||
if self.status: | ||
self.headers = { | ||
'Accept': 'application/json; application/vnd.esios-api-v1+json', | ||
'Content-Type': 'application/json', | ||
'Host': 'api.esios.ree.es', | ||
'Cookie': '', | ||
} | ||
self.headers['Authorization']="Token token="+token | ||
|
||
# Unload if this module is disabled or misconfigured | ||
if not self.status: | ||
self.master.releaseModule("lib.TWCManager.Pricing", self.__class__.__name__) | ||
return None | ||
|
||
def getExportPrice(self): | ||
|
||
if not self.status: | ||
self.master.debugLog( | ||
10, | ||
"$PVPCes", | ||
"PVPCes Pricing Module Disabled. Skipping getExportPrice", | ||
) | ||
return 0 | ||
|
||
# Perform updates if necessary | ||
self.update() | ||
|
||
# Return current export price | ||
return float(self.exportPrice) | ||
|
||
def getImportPrice(self): | ||
|
||
if not self.status: | ||
self.master.debugLog( | ||
10, | ||
"$PVPCes", | ||
"PVPCes Pricing Module Disabled. Skipping getImportPrice", | ||
) | ||
return 0 | ||
|
||
# Perform updates if necessary | ||
self.update() | ||
|
||
|
||
|
||
# Return current import price | ||
return float(self.importPrice) | ||
|
||
def update(self): | ||
|
||
# Fetch the current pricing data from the https://www.esios.ree.es/es/pvpc API | ||
self.fetchFailed = False | ||
now=datetime.now() | ||
tomorrow=datetime.now() + timedelta(days=1) | ||
if self.lastFetch == 0 or (now.hour < self.lastFetch.hour): | ||
# Cache not feched or was feched yesterday. Fetch values from API. | ||
ini=str(now.year)+"-"+str(now.month)+"-"+str(now.day)+"T"+"00:00:00" | ||
end=str(tomorrow.year)+"-"+str(tomorrow.month)+"-"+str(tomorrow.day)+"T"+"23:00:00" | ||
|
||
url = "https://api.esios.ree.es/indicators/1014?start_date="+ini+"&end_date="+end | ||
|
||
try: | ||
r = self.requests.get(url,headers=self.headers, timeout=self.timeout) | ||
except self.requests.exceptions.ConnectionError as e: | ||
self.master.debugLog( | ||
4, | ||
"$PVPCes", | ||
"Error connecting to PVPCes API to fetch market pricing", | ||
) | ||
self.fetchFailed = True | ||
return False | ||
|
||
self.lastFetch= now | ||
|
||
try: | ||
r.raise_for_status() | ||
except self.requests.exceptions.HTTPError as e: | ||
self.master.debugLog( | ||
4, | ||
"$PVPCes", | ||
"HTTP status " | ||
+ str(e.response.status_code) | ||
+ " connecting to PVPCes API to fetch market pricing", | ||
) | ||
return False | ||
|
||
if r.json(): | ||
self.todayImportPrice=r.json() | ||
|
||
if self.todayImportPrice: | ||
try: | ||
self.importPrice = float( | ||
self.todayImportPrice['indicator']['values'][now.hour]['value'] | ||
) | ||
# Convert MWh price to KWh | ||
self.importPrice = round(self.importPrice / 1000,5) | ||
|
||
except (KeyError, TypeError) as e: | ||
self.master.debugLog( | ||
4, | ||
"$PVPCes", | ||
"Exception during parsing PVPCes pricing", | ||
) | ||
|
||
def getCheapestStartHour(self,numHours,ini,end): | ||
# Perform updates if necessary | ||
self.update() | ||
|
||
minPriceHstart=ini | ||
if self.todayImportPrice: | ||
try: | ||
if end < ini: | ||
# If the scheduled hours are bettween days we consider hours going from 0 to 47 | ||
# tomorrow 1am will be 25 | ||
end = 24 + end | ||
|
||
i=ini | ||
minPrice=999999999 | ||
while i<=(end-numHours): | ||
j=0 | ||
priceH=0 | ||
while j<numHours: | ||
price = float(self.todayImportPrice['indicator']['values'][i+j]['value']) | ||
|
||
priceH = priceH + price | ||
|
||
j=j+1 | ||
if priceH<minPrice: | ||
minPrice=priceH | ||
minPriceHstart=i | ||
i=i+1 | ||
|
||
|
||
except (KeyError, TypeError) as e: | ||
self.master.debugLog( | ||
4, | ||
"$PVPCes", | ||
"Exception during cheaper pricing analice", | ||
) | ||
|
||
if minPriceHstart > 23: | ||
minPriceHstart = minPriceHstart - 24 | ||
|
||
return minPriceHstart | ||
|
||
def getPricingInAdvanceAvailable(self): | ||
return True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few thoughts about this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, just thinking out loud about the scheduled charging logic here:
I'd be interested in feedback from those with power pricing APIs to hear how they'd want to use the functionality.