Frank Energie Daily & hourly prices with GraphQL and Python

Photo by Matthew Henry on Unsplash

Frank Energie Daily & hourly prices with GraphQL and Python

Daily Gas and Energy market prices per hour and day

Theo van der Sluijs
·Apr 4, 2022·

7 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • TL;DR Python GraphQL Frank Energie script
  • Frank Energie prices are fair and right!
  • Big Frank Energie advantage
  • Frank energie API
  • Frank Energie script
  • Become a Frank energie Friend!

TL;DR Python GraphQL Frank Energie script

You don't have time to read all of this right? So don't, just jump to the script. It almost works out of the box. You only need to pip install requests and run run run!

Or, even faster! Go to my Github repo

Frank Energie prices are fair and right!

Energy prices are currently rising enormously. Until the end of March I paid 80 euro cents per m3 for gas. That is now 3 euro per m3.

Same with electricity, first 16 cents now 65 cents. I did not want to pay that much. So I started looking for a solution with a good friend of mine.

We soon found out that the standard energy supplier is making a bizarre profit. They do say that they have the best price for you with their permanent contracts. But actually you are paying way too much.

Now that the permanent contracts are not possible for the time being, due to the high energy prices, it is easy to switch.

And why would you take a permanent contract if you can also get variable prices (just like at the gas station) and for the purchase price.

We found a company called "Frank Energie", they sell energy at purchase price, you get real-time insight, there is no fixed contract, there are no fines and you are paying for your actual consumption.

Cons? Yes!

  • Every month the costs will differ as prices fluctuate;
  • You probably pay almost nothing in the summer and a lot more in the winter (high gas consumption).
  • In the end the average month price will be less, but you have to have a strong heart when prices go up and down.

Benefits? Yes, much more!

  • You pay the purchase price without margin (so a much lower price!);
  • You pay the actual cost;
  • There are no surprises (afterwards), you pay per month what you use;
  • Energy supplier can (almost) not go bankrupt because they sell the real price and you pay every month what you used;
  • On very good days there is free electricity for a few hours.

You pay what you use per month

Okay! There is one other disadvantage when you have solar panels! These run through the netting trajectory, but if you produce (overproduce) energy at the moments when the energy is not worth shit, then you don't get shit back. So when the price is 1 cent... you get 1 cent.

Big Frank Energie advantage

There is however one other very very big advantage! You will see the hourly price of electricity 1 day in advance! So you can plan your day accordingly. Tomorrow between 12 and 2 the electricity cheap? Then you do your laundry and vacuum your house.

Is the price high between 3 and 5 hours? Then you take your car off the charger for a while.

You can view these prices in the APP. Also a day in advance. But if you want to receive a message that the prices will be very low or very high tomorrow?

Then you can build something for that with Python! But then you need API access!

And you know! Iteo takes care of that Python API access for you!

Frank energie API

Yes yes yes!!! I contacted Frank and they have an API! Okay!!! When there is an API I can work with it! Right?

Well they use GraphQL.... My colleagues are working a lot with GraphQL. I don't... in my professional life I'm not a developer anymore... never worked with GraphQL, did not need is... was not going to use it.

Until now!

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. GraphQL was developed internally by Facebook in 2012 before being publicly released in 2015.

Ah Facebook... now I know why I did not want to use it. But... as it seems more and more companies are using GraphQL for API's.

Ask for what you need, get exactly that!

Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results. Apps using GraphQL are fast and stable because they control the data they get, not the server.

Have you ever seen a GraphQL query?

{
  hero {
    name
    height
    mass
  }
}

Okay, I don't know about you, but this looks like shit! A colleague of mine said he hates YAML because it's complicated to write (and get all the tabs and spaces correct), but he does work with GraphQL... okay.....

So, I'm more happy with what comes out! A json

{
  "hero": {
      "name": "Luke Skywalker",
      "height": 1.72,
      "mass": 77
  }
}

Okay, looking back at this... it seems pretty simple after all. But now I want this in Python.

You do not need an authenticate for the market prices API. To retrieve the market prices you can make a POST call to graphcdn.frankenergie.nl with this as GraphQL query:

query MarketPrices {
    marketPricesElectricity(startDate: "2021-10-01", endDate: "2021-10-30") {
    till
    from
    marketPrice
    priceIncludingMarkup
    }
    marketPricesGas(startDate: "2021-10-01", endDate: "2021-10-30") {
    from
    till
    marketPrice
    priceIncludingMarkup
  }
}

Yes! You need all the linebreaks and stuff, so in Python we work with """ :-)

The post to graphcdn.frankenergie.nl has to go like this

{
    "query": "query MarketPrices {\n\tmarketPricesElectricity(startDate: \"2021-10-01\", endDate: \"2021-10-30\") {\n    till\n    from\n    marketPrice\n    priceIncludingMarkup\n\t}\n\tmarketPricesGas(startDate: \"2021-10-01\", endDate: \"2021-10-30\") {\n    from\n    till\n    marketPrice\n    priceIncludingMarkup\n  }\n}"
}

So the Python script would look like this

 query = "query MarketPrices {
        marketPricesElectricity(startDate: "2021-10-01", endDate: "2021-10-30") {
        till
        from
        marketPrice
        priceIncludingMarkup
        }
        marketPricesGas(startDate: "2021-10-01", endDate: "2021-10-30") {
        from
        till
        marketPrice
        priceIncludingMarkup
    }
    }"

    response = requests.post('https://graphcdn.frankenergie.nl', json={'query': query}, headers=headers)
    data = response.json()

But I guess you want variable start and end dates... You do not want to change them every time you run the script. I would want to add them with f string but that is where is a bit complicated.

You need f""" and double curly braces except where you put the variables.

Frank Energie script

It's all down below. For the script to run (python 3.8 and higher) first do a pip install requests.

And here is the script, It takes todays prices or when it's after 3 PM (15:00) it will show todays and tomorrow's prices.

It will also put the prices in to two CSV files, one for gas, one for electricity.

import requests
from datetime import datetime, timedelta
import csv

def FrankEnergy():
    now = datetime.now()

    yesterday = datetime.now() + timedelta(days=-1)
    startdate = yesterday.strftime("%Y-%m-%d")
    enddate = now.strftime("%Y-%m-%d")

    if int(now.strftime("%H")) > 15:
        tomorrow = datetime.now() + timedelta(days=2)
        startdate = now.strftime("%Y-%m-%d")
        enddate = tomorrow.strftime("%Y-%m-%d")

    headers = { "content-type":"application/json" }

    query = f"""query MarketPrices {{
        marketPricesElectricity(startDate: "{startdate}", endDate: "{enddate}") {{
        till
        from
        marketPrice
        priceIncludingMarkup
        }}
        marketPricesGas(startDate: "{startdate}", endDate: "{enddate}") {{
        from
        till
        marketPrice
        priceIncludingMarkup
    }}
    }}"""

    response = requests.post('https://graphcdn.frankenergie.nl', json={'query': query}, headers=headers)
    data = response.json()

    frank_electra_file = "frank_electra.csv"
    frank_gas_file = "frank_gas.csv"

    frank_headers = ['till', 'from', 'marketPrice', 'priceIncludingMarkup']

    with open(frank_electra_file, 'w') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames = frank_headers)
        writer.writeheader()
        writer.writerows(data['data']['marketPricesElectricity'])

    with open(frank_gas_file, 'w') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames = frank_headers)
        writer.writeheader()
        writer.writerows(data['data']['marketPricesGas'])

    for electra in data['data']['marketPricesElectricity']:
       print(electra['till'], electra['from'], electra['marketPrice'], electra['priceIncludingMarkup'])

    for gas in data['data']['marketPricesGas']:
        print(gas['till'], gas['from'],gas['marketPrice'], gas['priceIncludingMarkup'])


if __name__ == "__main__":
    FrankEnergy()

That's it!

Now you got the info to make your own message for telegram, sms or well what ever.

Questions? Shoot!

Become a Frank energie Friend!

Hi! If you become a member of Frank Energie through me, we both receive 2 trees or €10 discount every year!

You can register via this link: https://frnk.app/v54dvthe

10 reasons to join Frank too!

  1. 100% green electricity from the Netherlands
  2. CO2 compensation for gas consumption
  3. Energy at purchase price
  4. Real-time insight and control via the app
  5. No fines, no annual contracts
  6. No megalomaniac television commercials
  7. Growth through satisfied customers
  8. If you help, you get trees or a discount
  9. We help you save
  10. Every now and then happy power: then the power costs nothing!

Did you find this article valuable?

Support Theo van der Sluijs by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this