Skip to content

Instantly share code, notes, and snippets.

@tvdsluijs
Last active April 19, 2021 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tvdsluijs/a4ad638f11283a3d82763a47ae210109 to your computer and use it in GitHub Desktop.
Save tvdsluijs/a4ad638f11283a3d82763a47ae210109 to your computer and use it in GitHub Desktop.
Small script to get your energy data from the slimmemeterportal.nl
import requests
import calendar
import time
import datetime
from bs4 import BeautifulSoup
username = "" # fill in your slimmemeterportal username
password = "" # fill in your password
start_year = "" # fill in the year your started measurements
now = datetime.datetime.now()
end_year = now.year+1
months = list(range(1, 13, 1))
years = list(range(int(start_year), int(end_year), 1))
start_url = "https://slimmemeterportal.nl/login"
login_url = "https://slimmemeterportal.nl/user_session"
seconds = 2629746
def seconds_in_month(month, year):
nomatter, daysinmonth = calendar.monthrange(year, month)
return daysinmonth * 24 * 60 * 60
with requests.Session() as s:
r = s.get(start_url) # Get first page for the csrf code (generated each time)
soup = BeautifulSoup(r.content, 'html.parser')
authenticity_token = soup.find('input', {'name': 'authenticity_token'}).get('value') # get the csrf data from the input field
payload = {"utf8": "✓", "authenticity_token": authenticity_token, "user_session[email]": username,
"user_session[password]": password, "commit": "Inloggen"}
c = s.post(login_url, data=payload)
for y in years:
for m in months:
datum = datetime.date(y, m, 1)
ux = int(time.mktime(datum.timetuple()))
sim = int(seconds_in_month(m, y))
url = "https://slimmemeterportal.nl/cust/consumption/chart.xls?commodity=power&datatype=consumption&range=" \
"{}&timeslot_start={}".format(seconds, ux)
filename = "{}-{}.xls".format(y, m)
# print(url)
r = s.get(url)
with open(filename, 'wb') as f:
f.write(r.content)
time.sleep(5)
@jvhaarst
Copy link

Bedankt !

@jvhaarst
Copy link

Nog een vraagje : Hoe ga je om met de overgangen tussen zomer en wintertijd ? In mijn logs zie ik dat in het ene geval er dubbele entries zijn, en in het andere geval "mist" er een uur.

@tvdsluijs
Copy link
Author

Ja, klopt ik krijg ook wel eens dubbele data. Zelf schoon ik zo nu en dan de data wat op met dubbele records. Misschien maak ik daar nog wel eens een python scriptje voor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment