smokkelaar / CarChargeWithHourCurrentRates

Charing on the cheapest power rates
GNU General Public License v3.0
18 stars 3 forks source link

Prijsentsoe cannot resolve #5

Open liquidacid opened 3 years ago

liquidacid commented 3 years ago

By the looks of the website it seems they changed a bit of stuff. Probably filling datafields from another source than before which can't be handled by requests. Found out that selenum can do this.

I would like to put in a pull request but im not sure how things get handled when summer and wintertime changes. Please let me know what you think!


import datetime as dt
from selenium import webdriver
from bs4 import BeautifulSoup

#######################################################
###Input: none
###Function: Get the day ahead prices form entsoe, and transform them to price per kWh including taxation. (*0.00121)
###Output: List of upcoming prices so far as possible. in the format: list[time, price buy, price sell]
#######################################################

def Ophalen():
    nu = dt.datetime.now(dt.timezone.utc)
    morgen = nu + dt.timedelta(days=1)
    urlvandaag = "https://transparency.entsoe.eu/transmission-domain/r2/dayAheadPrices/show?name=&defaultValue=false&viewType=TABLE&areaType=BZN&atch=false&dateTime.dateTime="+"{:02d}".format(nu.day)+"."+"{:02d}".format(nu.month)+"."+str(nu.year)+"+00:00|CET|DAY&biddingZone.values=CTY|10YNL----------L!BZN|10YNL----------L&resolution.values=PT60M&dateTime.timezone=CET_CEST&dateTime.timezone_input=CET+(UTC+1)+/+CEST+(UTC+2)"
    urlmorgen = "https://transparency.entsoe.eu/transmission-domain/r2/dayAheadPrices/show?name=&defaultValue=false&viewType=TABLE&areaType=BZN&atch=false&dateTime.dateTime="+"{:02d}".format(morgen.day)+"."+"{:02d}".format(morgen.month)+"."+str(morgen.year)+"+00:00|CET|DAY&biddingZone.values=CTY|10YNL----------L!BZN|10YNL----------L&resolution.values=PT15M&resolution.values=PT30M&resolution.values=PT60M&dateTime.timezone=CET_CEST&dateTime.timezone_input=CET+(UTC+1)+/+CEST+(UTC+2)"

    #print(urlvandaag)
    #print (urlmorgen)

    pagevandaag = webdriver.Chrome()
    pagemorgen = webdriver.Chrome()
    pagevandaag.get(urlvandaag)
    pagemorgen.get(urlmorgen)   

    soupv = BeautifulSoup(pagevandaag.page_source, 'html.parser')
    soupm = BeautifulSoup(pagemorgen.page_source, 'html.parser')

    pagevandaag.quit()
    pagemorgen.quit()

    price_hidev = soupv.find_all(class_='dv-value-cell')
    price_hidem = soupm.find_all(class_='dv-value-cell')
    returnvalues = []
    for price in price_hidev:
        try:
            #print(price)
            noISOdatum = str(price).split("\', \'")[2]

            datum = dt.datetime.fromisoformat(noISOdatum[:-1]) + dt.timedelta(hours=3)

            if ((datum.timestamp() - nu.timestamp()) >= 0):
                #print(datum + dt.timedelta(hours=-1))
                #print(price.text)
                returnvalues.append([datum, str(round(float(price.text)*0.00121, 7)), str(round(float(price.text)*0.00121, 7))])
        except:
            pass
    try:
        for price in price_hidem:
            try:
                #print(price)
                noISOdatum = str(price).split("\', \'")[2]
                datum = dt.datetime.fromisoformat(noISOdatum[:-1]) + dt.timedelta(hours=3)

                if ((datum.timestamp() - nu.timestamp()) >= 0):
                    #print(datum + dt.timedelta(hours=-1))
                    #print(price.text)
                    returnvalues.append([datum, str(round(float(price.text)*0.00121, 7)), str(round(float(price.text)*0.00121, 7))])
            except:
                pass
    except:
        pass
    return returnvalues
nhoening commented 3 years ago

Just wanted to note that it's way easier to get an API key from ENTSO-E and then use the entsoe-py library.

Each data call is literally two lines of code, like we do here, for instance.

liquidacid commented 3 years ago

Thanks, i will implement the API from Entso-e, i didn't mention it was there actually!

liquidacid commented 2 years ago

I managed to do this with the API from Entsoe which is much better. I'd complete it but i did not test it with your Tesla API since im not using that. I also do not fill the array with times corresponding to the prices since i will not use it that way. Im just calculating the price starting from the current hour.

I need to mention it would be nessecary to put some more information regarding this in the readme.