This is a rather interesting piece of Python code that took me a little to put together. I found a few pieces while searching on the internet, but failed to note the source. I typically put the URL to the source as a comment in my code, but this time I didn’t. Sorry author(s)!
This function is part of a script and setup that collects meter data from my earlier not-so-smart energy meter, an ISKRA MT171. This meter only provides energy counter information through a serial optical interface that shows the kWh usage. I have a dual meter, meaning that it switches between a high tarif (more expensive, called normal(!)) and a low tarif, typically at night and weekends.
I use an ESP8266 to collect the data (called a Telegram) from the meter through a serial IR sensor, and use MQTT to send the relevant data to a Raspberry Pi.
The Raspi acts as a server for various ESP8266 based sensors that collect temperature, pressure, air particles (polution) and humidity. The Raspi collects the data, manipulates it, stores it in various RRDTOOL databases and also sends it to a Lighttpd Webserver for presentation on my local network.
The meter switches between the two tarifs by a signal that comes from the electricity provider, over the power lines. The Telegram does not show this, so in order to display the current tarif from the meter on my website, I had to work it out myself, which is what the following code is all about.
If you like what you see, please support me by buying me a coffee: https://www.buymeacoffee.com/M9ouLVXBdw
The comments should be clear enough so that you can follow it and modify the code for your particular situation.
First of all, you need to define an import for the dateutil.easter library:
try:
from dateutil.easter import *
except ImportError, e:
print("Python module dateutil not found, run: 'sudo pip install python-dateutil'")
sys.exit(0)
# if you test it on your PC, you also need to install dateutil.easter for W10:
# in the cmd box run this : python -m pip install python-dateutil
import time
import sys
import datetime
DEBUG = True
Here is the actual function:
def set_tarif():
'''
Determine the energy tarif
Do we have a low tarif (dal tarief) or a high tarif (normaal tarief)
The tarif is set by a time during weekdays. It is always low during a
weekend and during a public holiday.
'''
global tarif
# tarif switch times in the South-Holland region:
startTime = datetime.time(23,00)
endTime = datetime.time(07,00)
now = datetime.datetime.now()
year = now.year
# determine the legal holidays, tarif is always low on these days
new_year = datetime.date(year, 1, 1) # jan 1st
good_frid = easter(year)+datetime.timedelta(days=-2)
easter_sun = easter(year)
easter_mon = easter(year)+datetime.timedelta(days=+1)
kings_day = datetime.date(year, 4, 27) # April 27th
lib_day = datetime.date(year, 5, 5) # May 5th
asc_day = easter(year)+datetime.timedelta(days=+39) # hemelvaart
pentecost = easter(year)+datetime.timedelta(days=+49) # pinksteren
pentecost_2 = easter(year)+datetime.timedelta(days=+50)
xmass = datetime.date(year, 12, 25) # dec 25th
xmass_2 = datetime.date(year, 12, 26) # dec 26th
h_days = set ([new_year, good_frid, easter_sun, easter_mon, kings_day, \
lib_day, asc_day, pentecost, pentecost_2, xmass, xmass_2])
def chk_tarif(startTime, endTime, nowTime):
'''
Determine if we are currently in the high or low tarif period
'''
if startTime < endTime :
return(nowTime >= startTime and nowTime <= endTime)
else:
return(nowTime >= startTime or nowTime <= endTime)
now = datetime.datetime.now()
if DEBUG : print ("now = %s" % now)
nowTime = now.time()
if DEBUG : print ("now_time = %s " % nowTime)
# check if we have a weekday
if now.isoweekday() in range(1, 6):
if DEBUG : print ("weekday")
# now check for a public holiday = always a low tarif
if now.date() in h_days:
if DEBUG : print ("h-day")
tarif = 1 # low
else:
if DEBUG : print ("No h-day")
# now check for weekday hi/low tarif based on tarif switch times
if chk_tarif(startTime, endTime, nowTime):
tarif = 1 # low
else:
tarif = 2 # high - normal
else:
if DEBUG : print ("weekend")
tarif = 1
if DEBUG : print ("tarif = %s" % tarif)
return
1 comment:
Post a Comment